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

先上代码:

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中“下次不再提示”的对话框(修改自某大神)的更多相关文章

  1. 【转】Android中通知的提示音、震动和LED灯效果小例子

    通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...

  2. Android中仿IOS提示框的实现

    前言 在Android开发中,我们有时需要实现类似IOS的对话框.今天我就来总结下,如何通过自定义的开发来实现类似的功能. 自定义Dialog 我们知道Android中最常用的对话框就是Dialog及 ...

  3. Android中,粗暴的方式,修改字体

    序 在 Android 下使用自定义字体已经是一个比较常见的需求了,最近也做了个比较深入的研究. 那么按照惯例我又要出个一篇有关 Android 修改字体相关的文章,但是写下来发现内容还挺多的,所以我 ...

  4. 【转】 Android中退出程序的提示框

    原文网址:http://blog.csdn.net/jumping_android/article/details/7571309 @Override public boolean onKeyDown ...

  5. Android中如何使用多选对话框

    final String [] ss={"A","B","C","D","E"}; boolean ...

  6. 使用原生ajax访问后台数据并将其展现在前端页面中(小菜鸟自己整理玩的,大神勿喷)

    首先你要有php的环境,关于php环境的搭建,php本地站点的搭建,此处不再重复请看这里:http://www.cnblogs.com/Gabriel-Wei/p/5950465.html我们把wam ...

  7. Android中的AutoCompleteTextView(随笔提示文本)组件的简单使用

    Android中的随笔提示文本组件AutoCompleteTextView的使用,此组件用于输入文本,然后就会在所配置的适配器中的数据进行查找显示在组件下面. 这里值得注意的是AutoComplete ...

  8. android中使用spinner组件,以key,value的方式

    接着上一篇文章的内容:android中使用spinner组件 稍做修改,以key,value的方式,在实际使用中,经常需要获取的值并不一定跟显示的内容一致. 需要先添加一个对象类,用来描述key,va ...

  9. Android中的AlertDialog使用示例五(自定义对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

随机推荐

  1. [转载]Windows 7笔记本创建wifi热点供手机上网教程

    用智能手机的朋友会发现这样一个问题,智能手机比普通手机上网更耗流量.这是因为智能手机应用(软件)丰富,而且大部分应用都会自动联网.为此,许多人每月包了上百M的流量套餐,但用的时候还是小心翼翼,生怕流量 ...

  2. mysql启动报错

    查看报错日志: 131023 15:02:59 [ERROR] Can't start server: Bind on TCP/IP port: No such file or directory13 ...

  3. ssl和https协议详解

    转自:https://cuiyongxiu.com/201102/24157.html ssl协议的起源和历史我就不再多说了,就是那个Netscape 网景公司开发的,它的作用主要是提供了一种安全传输 ...

  4. java基础之 string

    一 string public final class String 继承自java.lang.Object类. 实现了接口: java.io.Serializable, Comparable< ...

  5. [Unity3D]调用Android接口

    简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...

  6. iphone获取当前流量信息

    通过读取系统网络接口信息,获取当前iphone设备的流量相关信息,统计的是上次开机至今的流量信息. 代码 悦德财富:https://yuedecaifu.com 1 2 3 4 5 6 7 8 9 1 ...

  7. Mac下为我们开发的App制作gif动画演示(不仅仅针对开发者,想做gif图片的也可参考)

    趁着工作不忙,就闲着倒腾自己的事情,把自己写的一个完整App<丁丁印记>整理了一番,总结其中用到的技术和实现的功能,并想把一些用到的技术分享给各位工友们,因为我自学iOS开发得益于大家的分 ...

  8. ResultSet结果集判断是否为空

    目前亲测过能用的一个方法是: if(rs.next())//当前行有内容 { msg2 = "有这个活动!"; } else //rs对象为空表示查无此活动 { msg2 = &q ...

  9. 2016 - 1 -19 初学HTML5 第一天

    1.HTML COMMANDS MHTL commands called elements.Usually, an element has a start tag and an end tag e.g ...

  10. GSM Hacking:如何对GSM/GPRS网络测试进行测试

    写在前面 这里需要介绍的是GSM / GPRS网络测试的一些方法,随着现在硬件设备连网现象的普遍存在,例如智能电表.自动变速箱控制单元(TCU).POS机.报警系统等.这些设备通常需要与网络连接,GS ...