android中“下次不再提示”的对话框(修改自某大神)

如图,我们要做得就是这个:

  

先上代码:

1,逻辑代码

  1 package com.example.hello;
  2 
  3 import android.app.Activity;
  4 import android.app.AlertDialog;
  5 import android.app.Dialog;
  6 import android.content.DialogInterface;
  7 import android.content.SharedPreferences;
  8 import android.content.SharedPreferences.Editor;
  9 import android.os.Bundle;
 10 import android.preference.PreferenceManager;
 11 import android.view.KeyEvent;
 12 import android.view.LayoutInflater;
 13 import android.view.View;
 14 import android.view.View.OnClickListener;
 15 import android.view.ViewGroup;
 16 import android.widget.Button;
 17 import android.widget.CheckBox;
 18 import android.widget.TextView;
 19 
 20 public class mima extends Activity {
 21     private static final String KEY_IS_SHOW_PWD_TIP = "KISPT";
 22     private static SharedPreferences sharedPreferences;
 23     private View newView;
 24     private TextView tv;
 25 
 26     private CheckBox cb;
 27 
 28     @Override
 29     public void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.activity_main);
 32 
 33         // 用于填充AlertDialog的布局<<
 34         LayoutInflater inflater = LayoutInflater.from(this);
 35         newView = inflater.inflate(R.layout.dialog_item_add_quesgood, null);
 36         tv = (TextView) newView.findViewById(R.id.tv_add_quesGood);
 37         // >>
 38 
 39         cb = (CheckBox) newView.findViewById(R.id.cb_isShow);
 40         // 一定要做判断
 41         if (isShowPwdTip()) {
 42             showDialog(0);
 43         }
 44         Button bt =  (Button) findViewById(R.id.bt);
 45         bt.setOnClickListener(new OnClickListener() {
 46             
 47             @Override
 48             public void onClick(View v) {
 49                 // TODO Auto-generated method stub
 50                 if (isShowPwdTip()) {
 51                     showDialog(0);
 52                 }
 53             }
 54         });
 55     }
 56 
 57     @Override
 58     protected Dialog onCreateDialog(int id) {
 59         switch (id) {
 60         case 0: {
 61             //第二次点击弹出时将父view中存在的上一个子view删除(否则会出现java.lang.IllegalStateException: 
 62             //The specified child already has a parent. You must call removeView() on the child's parent first.)
 63             ViewGroup vp = (ViewGroup) newView.getParent();
 64             if(vp != null){
 65                 vp.removeAllViews();
 66             }
 67             
 68             return new AlertDialog.Builder(mima.this)
 69                     .setView(newView)//自定义布局
 70                     .setOnKeyListener(new DialogInterface.OnKeyListener() {
 71 
 72                         @Override
 73                         public boolean onKey(DialogInterface dialog,
 74                                 int keyCode, KeyEvent event) {
 75                             if (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) {
 76                                 return true;
 77                             }
 78                             return false;
 79                         }
 80                     })
 81                     .setTitle("实验:")
 82 
 83                     .setPositiveButton("确定",
 84                             new DialogInterface.OnClickListener() {
 85                                 @Override
 86                                 public void onClick(DialogInterface dialog,
 87                                         int which) {
 88                                     if (cb.isChecked()) {
 89                                         removeDialog(0);
 90                                         closeShowPwdTip();
 91                                     } else {
 92                                         removeDialog(0);
 93                                     }
 94                                 }
 95                             })
 96                     .setNeutralButton("取消",
 97                             new DialogInterface.OnClickListener() {
 98                                 @Override
 99                                 public void onClick(DialogInterface dialog,
100                                         int which) {
101                                     removeDialog(0);
102                                 }
103                             }).create();
104         }
105         default: {
106             return null;
107         }
108         }
109     }
110 
111     public boolean closeShowPwdTip() {
112         if (sharedPreferences == null)
113             sharedPreferences = PreferenceManager
114                     .getDefaultSharedPreferences(this);
115         Editor editor = sharedPreferences.edit();
116         editor.putBoolean(KEY_IS_SHOW_PWD_TIP, false);
117         return editor.commit();
118     }
119 
120     public boolean isShowPwdTip() {
121         if (sharedPreferences == null)
122             sharedPreferences = PreferenceManager
123                     .getDefaultSharedPreferences(this);
124         return sharedPreferences.getBoolean(KEY_IS_SHOW_PWD_TIP, true);
125     }
126 }

2,daialog布局布局

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <TextView 
 8         android:id="@+id/tv_add_quesGood"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="您好,点击不再提示将不会再次出现此对话框,是否继续?"
12         android:layout_marginTop="10dp"
13         android:layout_marginLeft="10dp"
14         />
15 
16     <CheckBox 
17         android:id="@+id/cb_isShow"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:layout_marginLeft="4dp"
21         android:text="不在提示"
22         />
23 </LinearLayout>

3,主界面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".MainActivity" >

<Button 
    android:id="@+id/bt"
    android:text="点击"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>

逻辑代码部分是我参考某大神修改的,因为忘了出处,还请见谅。

整个过程主要就是通过sharedPreferences 存储点击状态,再给Dialog自定义一个布局来完成的,代码可直接用。因为比较简单,就不浪费大家时间了。

原文地址:https://www.cnblogs.com/wangyuehome/p/3213841.html