Android AlertDialog类的几个简单实例

Android AlertDialog类的几个简单实例

先看看android sdk上的说明

Class Overview

A subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method. If you want to display a more complex view, look up the FrameLayout called "body" and add your view to it:

 FrameLayout fl = (FrameLayout) findViewById(R.id.body); 
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));

The AlertDialog class takes care of automatically setting WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you based on whether any views in the dialog return true from View.onCheckIsTextEditor(). Generally you want this set for a Dialog without text editors, so that it will be placed on top of the current input method UI. You can modify this behavior by forcing the flag to your desired mode after calling onCreate(Bundle).

类概述:

Dialog的一个子类,可以显示一到三个按钮。如果你只是想在消息框中显示字符串,使用setMessage()方法。如果你想显示一个稍微复杂的视窗,查找到FrameLayout对应的body控件并把你的视窗添加上去:

实例:

创建对话框的时候,我们需要通过AlertDialog.Builder(Context context)生成器来构造一个Builder并生成一个 AlertDialog。

AlertDialog.Builder的设置内容较多,以后跟上。
下面先创建的是比较常见的几个dialog实例


1.创建含有OK和Cancel两个按钮的dialog对话框。
new AlertDialog.Builder(MyAlertDialog.this)
// MyAlertDialog.this视情况而定,这个一般是指当前显示的Activity对应的xml视窗。
.setTitle("真的要离开?")// 设置对话框的标题
.setMessage("你确定要离开")// 设置对话框的内容
.setPositiveButton("OK", // 设置对话框的确认按钮
    new DialogInterface.OnClickListener() { // 设置确认按钮的事件
        public void onClick(DialogInterface dialog, int which) {
            // do something here..I end this Prograss..
            android.os.Process.killProcess(android.os.Process.myPid());
    }})
.setNegativeButton("Cancel", // 设置对话框的取消按钮
    new DialogInterface.OnClickListener() { // 设置取消按钮的事件
        public void onClick(DialogInterface dialog, int which) {
            // 如果你什么操作都不做,可以选择不写入任何代码
            dialog.cancel();
    }}
).show();

2.创建含有按钮并且含有单选列表的dialog对话框。
/ * 我们在1的基础上创建一个含有自定义的单选列表内容的dialog
*首先要申明我们添加的自定义单选列表的内容:
*因为我在res/values中的strings.xml中已经添加了对应的固定的选择项字符串
* /
<string name="str_status_1">status_1</string>
<string name="str_status_2">status_2</string>
<string name="str_status_3">status_3</string>
<string name="str_status_4">status_4</string>
// 所以在java类中我只要获取到strings.xml中的值,如下定义:
final CharSequence[] statusChar = {
    getResources().getString(R.string.str_status_1),
    getResources().getString(R.string.str_status_2),
    getResources().getString(R.string.str_status_3),
    getResources().getString(R.string.str_status_4) };
// 如果没有定义的话,写为:
final CharSequence[] statusChar = { "status_1",
    "status_2",
    "status_3",
    "status_4" };
// 定义好之后,我们就可以把这个变量加载到setSingleChoiceItems这个单选列表的设置里面
int statusId = 0;// 设置选择的初始值
new AlertDialog.Builder(MyAlertDialog.this)
.setTitle("真的要离开?")
.setMessage("你确定要离开")
.setSingleChoiceItems(statusChar, // 把statusChar添加到dialog里面
    -1, // -1是默认选择为非选择,因为列表是从0开始。
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            statusId = which + 1;// 数组是从0开始,我要显示的是被选择的status状态
            Toast.makeText(MyAlertDialog.this, statusChar[which], Toast.LENGTH_LONG).show()
            // 将刚才我们选择的状态用临时窗口显示出来。
        }
    })
.setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do something here..I end this Prograss..
            android.os.Process.killProcess(android.os.Process.myPid());
    }})
.setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
    }}
).show();

3.创建一个自定义view的对话框。
最后,我们来创建一个包含自定义view的对话框。
首先,我们要在res/layout新建一个*.xml文件。我的文件名:statusview.xml
比如:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/AbsoluteLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
    android:layout_x="20dip"
    android:layout_y="10dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="当前状态信息" >
    </TextView>
    <EditText
    android:id="@+id/etInfo"
    android:singleLine="false"
    android:lines="5"
    android:gravity="top"
    android:layout_width="200dip"
    android:layout_height="200dip"
    android:layout_x="20dip"
    android:layout_y="60dip">
    </EditText>
</AbsoluteLayout>


之后就要在java类中调用这个statusview.xml文件
像文章开始说明的一样,我们需要得到view的实例

//获取到布局解析器的对象
inflater = LayoutInflater.from(MyAlertDialog.this);
//然后将statusview.xml赋到layout(View的实例)上
final View layout = inflater.inflate(R.layout.statusview, null);
//最后通过AlertDialog.Builder中的setView(View)加载layout即可,其他的如按钮之类的我就不写清楚了,文章前面已有。
new AlertDialog.Builder(LsPos.this). setView(layout).create().show();


想得到更多的信息,请参照开发文档。

转载此文,请注明出处,谢谢。
http://blog.sina.com.cn/s/blog_48b61dc70100isqg.html

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值