AlertDialog 对话框 二或三选择项
来源:互联网 发布:knockout.js pdf 编辑:程序博客网 时间:2024/05/21 09:03
通过添加AlertDialog,我们可以在当前页面弹出一个对话框,它是在所有其他控件之前的,可以屏蔽其他控件的交互,并为用户提供选择。如果只需要两个选择(是或否),为AlertDialog添加setPositiveButton()方法和setNegativeButton()方法以及它们的点击事件即可,需要第三个选择时可以添加一个setNeutralButton()方法(三个按钮的位置请看本文最后的运行结果)。setCancelable(false),括号里设置为false,按退出键不能退出,这个地方默认为true。最后不要忘记:需要show()方法使对话框显现出来。代码如下:
(1)Java部分:
package com.example.alertdialog;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.Window;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); } public void myclick(View v){ AlertDialog.Builder dialog = new AlertDialog. Builder(MainActivity.this); dialog.setTitle("对话框");//对话框最上面的字 dialog.setMessage("重要的事");//对话框中部的字 dialog.setCancelable(false);//这里设置为false,按退出键不能退出,这个地方默认为true//以下为对话框最下面的选择项 dialog.setPositiveButton("右边", new DialogInterface. OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setNegativeButton("左边", new DialogInterface. OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); /* * 需要第三个按钮时,才添加如下的setNeutralButton() */ dialog.setNeutralButton("中间", new DialogInterface. OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show(); }}
(2)XML文件里只是放一个按钮,通过点击它,使对话框弹出:
<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:orientation="vertical" > <Button android:text="AlertDialog" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myclick" /></LinearLayout>
(3)运行结果:
1 0
- AlertDialog 对话框 二或三选择项
- Android--选择对话框--AlertDialog
- AlertDialog选择对话框
- Android中的AlertDialog使用示例三(单向选择确定对话框)
- AlertDialog的嵌套--可以选择的对话框
- AlertDialog三种对话框的实现
- Androidの自定义对话框AlertDialog(二)
- AlertDialog对话框
- 对话框 AlertDialog
- 对话框AlertDialog
- AlertDialog对话框
- AlertDialog对话框
- AlertDialog对话框
- AlertDialog对话框
- AlertDialog对话框
- AlertDialog(对话框)
- AlertDialog(对话框)
- AlertDialog对话框
- 93. Restore IP Addresses
- 高仿美团app,浮动layout滑动到顶部
- 设计模式学习(十一)————外观模式
- POJ 3784 动态求中位数
- onActivityResult()方法的执行问题
- AlertDialog 对话框 二或三选择项
- neutron的context包含哪些内容
- Vue.js结合vue-router和webpack编写单页路由项目
- 《Android 源码设计模式解析与实战》学习笔记
- 设计模式学习(十二)————建造者模式
- float,double等精度丢失问题(详)
- 吾爱破解—破解备份
- STL格斗场
- 设计模式学习(十三)————观察者模式