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

2023-05-25,,

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

先上代码:

1,逻辑代码

 package com.example.hello;

 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;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView; public class mima extends Activity {
private static final String KEY_IS_SHOW_PWD_TIP = "KISPT";
private static SharedPreferences sharedPreferences;
private View newView;
private TextView tv; private CheckBox cb; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 用于填充AlertDialog的布局<<
LayoutInflater inflater = LayoutInflater.from(this);
newView = inflater.inflate(R.layout.dialog_item_add_quesgood, null);
tv = (TextView) newView.findViewById(R.id.tv_add_quesGood);
// >> cb = (CheckBox) newView.findViewById(R.id.cb_isShow);
// 一定要做判断
if (isShowPwdTip()) {
showDialog(0);
}
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isShowPwdTip()) {
showDialog(0);
}
}
});
} @Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0: {
//第二次点击弹出时将父view中存在的上一个子view删除(否则会出现java.lang.IllegalStateException:
//The specified child already has a parent. You must call removeView() on the child's parent first.)
ViewGroup vp = (ViewGroup) newView.getParent();
if(vp != null){
vp.removeAllViews();
} return new AlertDialog.Builder(mima.this)
.setView(newView)//自定义布局
.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("实验:") .setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (cb.isChecked()) {
removeDialog(0);
closeShowPwdTip();
} else {
removeDialog(0);
}
}
})
.setNeutralButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
removeDialog(0);
}
}).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);
}
}

2,daialog布局布局

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/tv_add_quesGood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您好,点击不再提示将不会再次出现此对话框,是否继续?"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/> <CheckBox
android:id="@+id/cb_isShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="不在提示"
/>
</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自定义一个布局来完成的,代码可直接用。因为比较简单,就不浪费大家时间了。

android中“下次不再提示”的对话框(修改自某大神)的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。