android中的AlertDialog具体概述
-
AlertDialog的构造方法所有是Protected的。所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder创建对话框须要了解下面几个方法:
setTitle :为对话框设置标题
setIcon :为对话框设置图标
setMessage:为对话框设置内容
setView : 给对话框设置自己定义样式
setItems :设置对话框要显示的一个list。一般用于显示几个命令时
setMultiChoiceItems :用来设置对话框显示一系列的复选框
setNeutralButton :普通buttonsetPositiveButton :给对话框加入"Yes"button
setNegativeButton :对话框加入"No"button
create : 创建对话框
show :显示对话框
一、简单的AlertDialog以下,创建一个简单的ALertDialog并显示它:
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("对话框的标题").
setMessage("对话框的内容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("对话框的标题").
setMessage("对话框的内容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}执行结果例如以下:二、带button的AlertDialog
上面的样例非常easy,以下我们在这个AlertDialog上面加几个Button,实现删除操作的提示对话框
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("确定删除?").
setMessage("您确定删除该条信息吗?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("确定删除?").
setMessage("您确定删除该条信息吗?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}在这个样例中,我们定义了三个button,各自是"Yes"button,"No"button以及一个普通button,每一个button都有onClick事件,TODO的地方能够放点了button之后想要做的一些处理看一下执行结果:
能够看到三个button加入到了AlertDialog上,三个没有加入事件处理的button。点了仅仅是关闭对话框,没有不论什么其它操作。
三、类似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法来实现类似ListView的AlertDialog第一个參数是要显示的数据的数组。第二个參数是点击某个item的触发事件
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:四、类似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法来实现类似RadioButton的AlertDialog第一个參数是要显示的数据的数组,第二个參数是初始值(初始被选中的item),第三个參数是点击某个item的触发事件
在这个样例里面我们设了一个selectedFruitIndex用来记住选中的item的index
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:
五、类似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog
第一个參数是要显示的数据的数组,第二个參数是选中状态的数组。第三个參数是点击某个item的触发事件[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:六、自己定义View的AlertDialog
有时候我们不能满足系统自带的AlertDialog风格,就比方说我们要实现一个Login画面,实username和password,这时我们就要用到自己定义View的AlertDialog先创建Login画面的布局文件
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" ><LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" ><TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" /><EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout><LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" ><TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" /><EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout></LinearLayout>
然后在Activity里面把Login画面的布局文件加入到AlertDialog上[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自己定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用户登录").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);// 取得自己定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用户登录").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登录", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:
android中的AlertDialog具体概述的更多相关文章
- Android 中的AlertDialog使用自定义布局
Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...
- Android中的AlertDialog使用示例五(自定义对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例四(多项选择确定对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例三(单向选择确定对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例二(普通选项对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例一(警告对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中使用AlertDialog实现几种不同的对话框
场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...
- Android中的AlertDialog和ProgressDialog用法
手机APP对话框是很多APP都有的下面来看下怎么实现的吧, 打开Android studio 然他自动创建好布局和类; 下面我们修改activity_main.xml中的代码 <?xml ver ...
- Android中的线程池概述
线程池 Android里面,耗时的网络操作,都会开子线程,在程序里面直接开过多的线程会消耗过多的资源,在众多的开源框架中也总能看到线程池的踪影,所以线程池是必须要会把握的一个知识点; 线程运行机制 开 ...
随机推荐
- 异常值(outlier)
简介 在数据挖掘的过程中,我们可能会经常遇到一些偏离于预测趋势之外的数据,通常我们称之为异常值. 通常将这样的一些数据的出现归为误差.有很多情况会出现误差,具体的情况需要就对待: 传感器故障 -> ...
- Redis-server在windows下闪退
在win7下使用Redis(windows版)很简单,只需要去Git上下载一个压缩包,解压运行即可.但是前段时间发现win10下双击redis-server既然闪退.非常不解... 在观察了错误日志才 ...
- objective-c訪问控制符
objective-c中成员变量的四个訪问控制符: @private:仅仅有当前类的内部才干訪问 @public:全部人都可訪问 @protected:仅仅限当前类和它的子类可以訪问 @package ...
- spring boot系统学习(知识点笔记)
一.http的注解配置 1.@SpringBootAplication=@SpringBootConfiguration(其实就是个@Configuration)+@EnableAutoConfigu ...
- 继承QWidget的派生类控件不能设置QSS问题解决(使用style()->drawPrimitive(QStyle::PE_Widget,也就是画一个最简单最原始的QWidget,不要牵扯其它这么多东西)
自定义控件时基类用了QWidget,发现qss设置不起作用,需要重载其paintEvent函数即可: 如下代码: void CCustomWidget::paintEvent(QPaintEvent* ...
- vc应用CPictureEx类(重载CStatic类)加载gif动画
1.PictureEx.h文件: //////////////////////////////////////////////////////////////////////// PictureEx. ...
- JSP中动态include与静态include的区别介绍
转自:https://m.jb51.net/article/43304.htm 动态INCLUDE 用法:<jsp:include page="included.jsp" f ...
- swift语言点评十六-Initialization && Deinitialization
initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...
- 优动漫PAINT-朱槿花的画法
火红夺目的朱槿花,绝对是烘托画面的绝佳布景!画法发非常简单,毫无绘画基础的人也能够将它很好的呈现出来哟~ 教程是简单,呃.... 没有优动漫PAINT软件肿么办? 别着急,╭(╯^╰)╮ 小编给你送来 ...
- Github上值得关注的前端项目-转自好友trigkit4
http://microjs.com/# 该网站的资源都托管到了github,microjs.com是一个可以让你选择微型的js类库的网站,该网站里的js库都是压缩后不大于5KB的,非常实用 http ...