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实现方法为建造者模式. ...
随机推荐
- sharepoint workflow不能正常使用
程序集“Microsoft.SharePoint.WorkflowServices, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce ...
- <构建之法>之第一二三章的感悟
第一章 看了第一章,第一章主要是概论,主要讲述软件是什么,是由什么组成的,然后接着陈述软件工程是什么,看了第一章之后,得知,软件工程只是实现软件的一个工具,有了工具做事情才容易.还有进行运维和维护软件 ...
- C#启动一个外部程序(1)-WinExec
C#启动一个外部程序(1)-WinExec 调用Win32 API.1. using System.Runtime.InteropServices; 2. // //#define SW ...
- ASP.NET MVC4 部分视图
ASP.NET MVC4 部分视图 2014-10-24 16:48 by 易code, 2662 阅读, 1 评论, 收藏, 编辑 [部分视图] ASP.NET MVC 里的部分视图,相当于 Web ...
- bzoj 2241: [SDOI2011]打地鼠
#include<cstdio> #include<iostream> using namespace std; ][],b[][],ans,sum; void pan(int ...
- bzoj 1911: [Apio2010]特别行动队
#include<cstdio> #include<iostream> #define M 1000009 #define ll long long using namespa ...
- treap 1296 营业额统计
有一个点答案错误,求大神指教 #include<cstdio>#include<iostream>#include<cstdlib>#include<ctim ...
- hdu 4612 Warm up
http://acm.hdu.edu.cn/showproblem.php?pid=4612 将原图进行缩点 变成一个树 树上每条边都是一个桥 然后加一条边要加在树的直径两端才最优 代码: #incl ...
- my class 2.0
www.dropbox.com www.google.com/voice www.prezi.com www.evernote.com
- 【转发】linux yum命令详解
linux yum命令详解 yum(全 称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理, ...