android中“下次不再提示”的对话框(修改自某大神)
如图,我们要做得就是这个:
先上代码:
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中通知的提示音、震动和LED灯效果小例子
通知(Notification)是 Android 系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现.发出一条通知后,手机最上方 ...
- Android中仿IOS提示框的实现
前言 在Android开发中,我们有时需要实现类似IOS的对话框.今天我就来总结下,如何通过自定义的开发来实现类似的功能. 自定义Dialog 我们知道Android中最常用的对话框就是Dialog及 ...
- Android中,粗暴的方式,修改字体
序 在 Android 下使用自定义字体已经是一个比较常见的需求了,最近也做了个比较深入的研究. 那么按照惯例我又要出个一篇有关 Android 修改字体相关的文章,但是写下来发现内容还挺多的,所以我 ...
- 【转】 Android中退出程序的提示框
原文网址:http://blog.csdn.net/jumping_android/article/details/7571309 @Override public boolean onKeyDown ...
- Android中如何使用多选对话框
final String [] ss={"A","B","C","D","E"}; boolean ...
- 使用原生ajax访问后台数据并将其展现在前端页面中(小菜鸟自己整理玩的,大神勿喷)
首先你要有php的环境,关于php环境的搭建,php本地站点的搭建,此处不再重复请看这里:http://www.cnblogs.com/Gabriel-Wei/p/5950465.html我们把wam ...
- Android中的AutoCompleteTextView(随笔提示文本)组件的简单使用
Android中的随笔提示文本组件AutoCompleteTextView的使用,此组件用于输入文本,然后就会在所配置的适配器中的数据进行查找显示在组件下面. 这里值得注意的是AutoComplete ...
- android中使用spinner组件,以key,value的方式
接着上一篇文章的内容:android中使用spinner组件 稍做修改,以key,value的方式,在实际使用中,经常需要获取的值并不一定跟显示的内容一致. 需要先添加一个对象类,用来描述key,va ...
- Android中的AlertDialog使用示例五(自定义对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
随机推荐
- 编程思考 PetShop读后感
标准,插拔式的设计思想建立一致的标准是通向“复用”的通道.分层,使其得到的充分的独立.一个东西如果独立了[不是孤立],这个事物就具有很强大的力量,这个和一个人的成长是相同的道理.所以呢,在写程序的过程 ...
- [示例]NSPredicate基础-查询数组中负荷条件的子集
代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...
- 【转】HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- [vijos P1595] 学校网络
有生以来做的第二道IOI题目居然也是96'的,又是一道比我还老的题目. 纯属复习或者说再学一遍Tarjan算法,本题的主要算法就是Tarjan+缩点,对于两个子问题的答案,根据解题:强连通缩点为拓扑图 ...
- IntelliLock托管代码保护和许可授权管理系统软件详细介绍及下载
IntelliLock是一个能用于控件与应用程序许可授权的100%托管的先进解决方案.与.NET Reactor提供的基于源代码保护的授权许可系统不同,IntelliLock选择了以100%托管的方式 ...
- CSS实现图片变灰色及透明度
[图片变灰] 每当遇到哀悼日,很多网站快速变灰色,来看看实现方式吧: 方式一,仅支持ie) html{filter:progid:DXImageTransform.Microsoft.BasicIma ...
- Android的R.java文件
1.Android资源管理简介: Android应用程序资源可以分为两大类,分别放在assets和res文件夹下.assets目录下保存的是一些原始的文件,可以以任何方式来进行组织.这些文件最终会被原 ...
- Linux 上的常用文件传输方式介绍与比较
ftp ftp 命令使用文件传输协议(File Transfer Protocol, FTP)在本地主机和远程主机之间或者在两个远程主机之间进行文件传输. FTP 协议允许数据在不同文件系统的主机之间 ...
- 使用HackRF+GNU Radio 破解吉普车钥匙信号
引文 我最近对软件定义的无线电技术(SDR)产生了浓厚的兴趣,而我对其中一款流行的SDR平台(HackRF)也产生了兴趣,而其频率接收的范围也在1MHz ~6GHz之间(范围较广).而这里也需要提及一 ...
- UVA11624(bfs)
题意:给一张图,有火源,有障碍物,剩下的是道路,火源在下一分钟能够让上下左右四个方向的道路也着火,告诉人的位置,问最短时间能逃出去的时间是多少: 思路:一个bfs用来求出所有的火源能蔓延到的地方,另一 ...