dialog —— 不再显示
要显示一个dialog,要实现点击dialog上“不再提示”按钮之后,下次启动程序就不会出现此对话框,怎么实现呢:
?
1下面是一个例子:
?
主Activity:
?
package hao.test.mima;
?
?
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
?
import hao.test.uitil.YyBackUpPreferences;
?
public class mima extends Activity {
? ? private Preferences pref = null;
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main);
? ? ? ? pref = Preferences.getInstance(this);
? ? ? ? //一定要做判断
? ? ? ? if (pref.isShowPwdTip()) {
? ? ? ? ? ? showDialog(0);
? ? ? ? }
? ? }
?
? ? @Override
? ? protected Dialog onCreateDialog(int id) {
? ? ? ? switch (id) {
? ? ? ? ? ? case 0: {
? ? ? ? ? ? ? ? ?return new AlertDialog.Builder(mima.this)
? ? ? ? ? ? ? ? ? ? ? ? .setOnKeyListener(new DialogInterface.OnKeyListener() {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setTitle("实验:")
? ? ? ? ? ? ? ? ? ? ? ? .setMessage("这是一个测试")
? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("确定",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? removeDialog(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("不再显示",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? removeDialog(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //在这里记录状态
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pref.closeShowPwdTip();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setNeutralButton("设置", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? removeDialog(0);
// ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?startSettingActivity();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }).create();
? ? ? ? ? ? }
? ? ? ? ? ? default: {
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
?
参数class:
?
?
?
package hao.test.uitil;
?
?
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
?
public class Preferences {
? ? private static final String KEY_IS_SHOW_PWD_TIP = "KISPT";
? ? private static SharedPreferences sharedPreferences;
? ? private static Context context;
? ? private static Preferences instance;
? ? public static Preferences getInstance(Context c) {
? ? ? ? context=c;
? ? ? ? if (instance == null) {
? ? ? ? ? ? instance = new Preferences();
? ? ? ? }
? ? ? ? return instance;
? ? }
? ? public boolean closeShowPwdTip() {
? ? ? ? Editor editor = getEditor();
? ? ? ? editor.putBoolean(KEY_IS_SHOW_PWD_TIP, false);
? ? ? ? return editor.commit();
? ? }
? ? private Editor getEditor() {
? ? ? ? SharedPreferences pref = getSharedPreferences();
? ? ? ? return pref.edit();
? ? }
? ? private SharedPreferences getSharedPreferences() {
? ? ? ? if (sharedPreferences == null)
? ? ? ? ? ? sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
? ? ? ? return sharedPreferences;
? ? }
//判断是否点击了那个“不再显示按钮”,如果按了的话,返回false。
? ? public boolean isShowPwdTip() {
? ? ? ? return getSharedPreferences().getBoolean(KEY_IS_SHOW_PWD_TIP, true);
? ? }
}
?
2下面是在一个文件中的:
?
?
package hao.test.mima;
?
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
?
public class mima extends Activity {
? ? private static final String KEY_IS_SHOW_PWD_TIP = "KISPT";
? ? private static SharedPreferences sharedPreferences;
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main);
? ? ? ? // 一定要做判断
? ? ? ? if (isShowPwdTip()) {
? ? ? ? ? ? showDialog(0);
? ? ? ? }
? ? }
?
? ? @Override
? ? protected Dialog onCreateDialog(int id) {
? ? ? ? switch (id) {
? ? ? ? ? ? case 0: {
? ? ? ? ? ? ? ? return new AlertDialog.Builder(mima.this)
? ? ? ? ? ? ? ? ? ? ? ? .setOnKeyListener(new DialogInterface.OnKeyListener() {
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }).setTitle("实验:").setMessage("这是一个测试")
? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("确定", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? removeDialog(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("不再显示", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? removeDialog(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 在这里记录状态
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? closeShowPwdTip();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }).setNeutralButton("设置", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? removeDialog(0);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // startSettingActivity();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }).create();
? ? ? ? ? ? }
? ? ? ? ? ? default: {
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? public boolean closeShowPwdTip() {
? ? ? ? if (sharedPreferences == null)
? ? ? ? sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
? ? ? ? Editor editor = sharedPreferences.edit();
? ? ? ? editor.putBoolean(KEY_IS_SHOW_PWD_TIP, false);
? ? ? ? return editor.commit();
? ? }
?
? ? public boolean isShowPwdTip() {
? ? ? ? if (sharedPreferences == null)
? ? ? ? ? ? sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
? ? ? ? return sharedPreferences.getBoolean(KEY_IS_SHOW_PWD_TIP, true);
? ? }
}
?
?