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实现方法为建造者模式. ...
随机推荐
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...
- 深入理解Redis:底层数据结构
简介 redis[1]是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- 如果解决ubuntu tab键不能提示命令
/bin/sh is symlinked to /bin/dashTo change it, do:sudo rm /bin/shsudo ln -s /bin/bash /bin/sh 原文:htt ...
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...
- MySql 分组排序取时间最大的一条记录
SELECT A.* FROM digital_asset A, (SELECT name, max(last_updated) max_day FROM digital_asset GROUP BY ...
- POJ 3522 Slim Span 最小生成树,暴力 难度:0
kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...
- POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3
Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...
- js用正则表达式控制价格输入
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- CRF条件随机场简介
CRF(Conditional Random Field) 条件随机场是近几年自然语言处理领域常用的算法之一,常用于句法分析.命名实体识别.词性标注等.在我看来,CRF就像一个反向的隐马尔可夫模型(H ...
- Linux - gcc和g++的区别
一般linux系统都自带了gcc编译器的,你可以用你的安装光盘去安装,如果你是觉得自带的gcc版本太低了,可以去gcc的官方网站可以下载到,编译需要很长的时间,如果你只编译C或者C++可以只下载gcc ...