Android基础教程之Button事件触发AlertDialog简单小窗口的应用
来源:互联网 发布:mac下windows系统截图 编辑:程序博客网 时间:2024/05/22 04:29
对话框在程序中不是必备的,但是用好对话框能对我们编写的应用增色不少。采用对话框可以大大增加应用的友好性。比较常用的背景是:用户登陆、网络正在下载、下载成功或者失败的提示,还有,比如:短信来了、电池没电了等等,只要你想到的,能提高用户体验的,你都可以使用对话框。今天就来简单介绍一下AlertDialog简单使用吧,
一、Dialog
对话框是在当前界面弹出的一个小窗口,用于显示重要提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等。一般情况下,用户要与对话框进行交互,然后返回到被只改的界面以继续运行当前的应用程序。
二、Dialog的常用方法
setTitle:为对话框设置标题setIcon:为对话框设置图标setMessage:为对话框设置内容setView:给对话框设置自定义样式setItems:设置对话框要显示的一个list,一般用于显示几个命令时setMultiChoiceItems:用来设置对话框显示一系列的复选框setSingleChoiceItems:设置单选按钮setNeutralButton:普通按钮setPositiveButton:给对话框添加“确认”按钮setNegativeButton:给对话框添加“取消”按钮
下面演示一下确认对话框,单选对话框,多选对话框,列表对话框,自定义对话框的做法。
先上布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jp.logdemo.MainActivity" tools:ignore="MergeRootFrame" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/toast_common" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="普通Toast" android:textSize="15sp" /> <Button android:id="@+id/toast_seat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="位置偏移Toast" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/toast_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="带图片的Toast" android:textSize="15sp" /> <Button android:id="@+id/toast_div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="自定义的Toast" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alertdialog_sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="确认对话框" android:textSize="15sp" /> <Button android:id="@+id/alertdialog_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="单选对话框" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alertdialog_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="多选对话框" android:textSize="15sp" /> <Button android:id="@+id/alertdialog_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="列表对话框" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alertdialog_div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="自定义对话框" android:textSize="15sp" /> <Button android:id="@+id/notification_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shapemore" android:text="发送消息到通知栏" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/notification_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shapemore" android:text="取消消息到通知栏" android:textSize="15sp" /> </LinearLayout></LinearLayout>
首先要获取布局中空间的id,并创建点击事件
// 确认对话框,单选对话框,多选对话框,列表对话框,自定义对话框 Button alertdialog_sure, alertdialog_one, alertdialog_two, alertdialog_list, alertdialog_div;
// 获取控件idprivate void initView() { // AlertDialogalertdialog_sure = (Button) findViewById(R.id.alertdialog_sure);alertdialog_one = (Button) findViewById(R.id.alertdialog_one);alertdialog_two = (Button) findViewById(R.id.alertdialog_two);alertdialog_list = (Button) findViewById(R.id.alertdialog_list);alertdialog_div = (Button) findViewById(R.id.alertdialog_div); }
// 创建点击事件 private void initEvent() { // AlertDialog点击事件 alertdialog_div.setOnClickListener(this); alertdialog_list.setOnClickListener(this); alertdialog_one.setOnClickListener(this); alertdialog_sure.setOnClickListener(this); alertdialog_two.setOnClickListener(this); }
public void onClick(View v) { switch (v.getId()) { case R.id.alertdialog_sure: sendSureAlertDialog(); break; case R.id.alertdialog_one: sendOneAlertdialog(); break; case R.id.alertdialog_two: sendTwoAlertdialog(); break; case R.id.alertdialog_list: sendListAlertdialog(); break; case R.id.alertdialog_div: sendDivAlertdialog(); break; default: break; } }
1、确认对话框
效果图
/** * 确认对话框 */ private void sendSureAlertDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置标题 builder.setTitle("确认对话框"); // 设置图标 builder.setIcon(R.drawable.ic_launcher); // 设置内容 builder.setMessage("你是程序员么?"); // 设置确认按钮, // 注:Dialog中按钮的点击事件中,OnClickListener是引用的DialogInterfere的。需要于Button的点击事件(引用View的)加以区分。 builder.setPositiveButton("是的", new android.content.DialogInterface.OnClickListener() { @Overridepublic void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "是程序员", Toast.LENGTH_LONG).show(); } }); // 取消对话框builder.setNegativeButton("不是", new android.content.DialogInterface .OnClickListener() { @Overridepublic void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "不是程序员", Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); // 获取dialog AlertDialog dialog = builder.create(); // 将标题显示出来 dialog.show(); }
二、单选对话框
/** * 设置单选对话框 */ private void sendOneAlertdialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置标题 builder.setTitle("单选对话框"); // 设置图标 builder.setIcon(R.drawable.ic_launcher); // 设置选项:第二个参数0表示默认选中第一个选项 builder.setSingleChoiceItems(color, 0, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你喜欢" + color[which], Toast.LENGTH_LONG).show(); } }); // 获取dialog AlertDialog dialog = builder.create(); // 将标题显示出来 dialog.show(); } /** * 确认对话框 */ private void sendSureAlertDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置标题 builder.setTitle("确认对话框"); // 设置图标 builder.setIcon(R.drawable.ic_launcher); // 设置内容 builder.setMessage("你是程序员么?"); // 设置确认按钮, // 注:Dialog中按钮的点击事件中,OnClickListener是引用的DialogInterfere的。需要于Button的点击事件(引用View的)加以区分。 builder.setPositiveButton("是的", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "是程序员", Toast.LENGTH_LONG).show(); } }); // 取消对话框 builder.setNegativeButton("不是", new android.content.DialogInterface .OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "不是程序员", Toast.LENGTH_LONG) .show(); dialog.dismiss(); } }); // 获取dialog AlertDialog dialog = builder.create(); // 将标题显示出来 dialog.show(); }
三、多选对话框
/** * 设置复选框 */ private void sendTwoAlertdialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置标题 builder.setTitle("多选对话框"); // 设置图标 builder.setIcon(R.drawable.ic_launcher); // 设置选项 builder.setMultiChoiceItems( color, null, new android.content.DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(MainActivity.this, "你喜欢" + color[which], Toast.LENGTH_LONG) .show(); } else { Toast.makeText(MainActivity.this, "你不喜欢" + color[which], Toast.LENGTH_LONG) .show(); } } }); // 取消对话框 builder.setNegativeButton("取消", new android.content.DialogInterface .OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你退出了选项", Toast.LENGTH_LONG) .show(); dialog.dismiss(); } }); // 获取dialog AlertDialog dialog = builder.create(); // 将标题显示出来 dialog.show(); }
四、列表对话框
/** * 设置列表框 */ private void sendListAlertdialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置标题 builder.setTitle("列表对话框"); // 设置图标 builder.setIcon(R.drawable.ic_launcher); // 设置选项 builder.setItems(color, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你喜欢" + color[which], Toast.LENGTH_LONG).show(); } }); // 取消对话框 builder.setNegativeButton("取消", new android.content.DialogInterface .OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你退出了选项", Toast.LENGTH_LONG) .show(); dialog.dismiss(); } }); // 获取dialog AlertDialog dialog = builder.create(); // 将标题显示出来 dialog.show(); }
五、自定义的对话框
自定义对话框的基本步骤:
第一步:创建布局文件;
第二步:获取布局:定义LayoutInflater inflater=LayoutInflater.from(this);创建View对象并赋值为inflater.inflate(R.layout….,null);
第三步:显示对话框并调用setView(view)来加载样式;
引用的布局文件
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="自定义的AlertDialog" android:textSize="20sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher" /></LinearLayout>
/** * 设置自定义对话框 */ private void sendDivAlertdialog() { // 将layout布局转换成View对象 LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.divalertdialog_main, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); // 设置标题 builder.setTitle("自定义对话框"); // 设置图标 builder.setIcon(R.drawable.ic_launcher); builder.setView(view); // 取消对话框 builder.setNegativeButton("取消", new android.content.DialogInterface .OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你退出了选项", Toast.LENGTH_LONG) .show(); dialog.dismiss(); } }); builder.show(); }
关于AlertDialog的基本应用用法就介绍到这里,希望对于新手有帮助~
0 0
- Android基础教程之Button事件触发AlertDialog简单小窗口的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- Android基础教程之简单的Button事件响应综合提示控件Toast的应用
- Button的触发事件
- Button和Button事件的简单应用
- 回车触发button的事件
- Android使用LayoutInflator遇到的Button无触发事件问题
- android 触发Button按钮事件的三种方式
- Android Button的触发事件中的一个问题
- Android的Button按钮,ACTION_UP事件不触发解决方案
- Android的Button按钮,ACTION_UP事件不触发解决方案
- Android的Button按钮,ACTION_UP事件不触发解决方案
- Android中widget窗口小部件的简单应用
- 杂乱之android的AlertDialog应用
- BUAA 1193
- Linux内核配置编译
- nginx+tomcat+memcached集群、负载均衡
- 黑马程序员——C语言关键字、标识符、注释、数据类型、常量
- eclipse常用快捷键
- Android基础教程之Button事件触发AlertDialog简单小窗口的应用
- 泛型
- 黑马程序员----Java网络编程
- IOS之UITextField与键盘
- c++字符串的包含问题
- SDNU 1089.拓扑排序【找入度为0的节点 拓扑排序】
- memcached与Redis比较
- 【华为oj】字符串分割
- AutoComplectView英语词典