欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Android的AlertDialog详解

程序员文章站 2022-04-03 19:46:22
本文对Android的AlertDialog进行详细了解... 12-10-09...
alertdialog的构造方法全部是protected的,所以不能直接通过new一个alertdialog来创建出一个alertdialog。
要创建一个alertdialog,就要用到alertdialog.builder中的create()方法。
使用alertdialog.builder创建对话框需要了解以下几个方法:
settitle :为对话框设置标题
seticon :为对话框设置图标
setmessage:为对话框设置内容
setview : 给对话框设置自定义样式
setitems :设置对话框要显示的一个list,一般用于显示几个命令时
setmultichoiceitems :用来设置对话框显示一系列的复选框
setneutralbutton    :普通按钮
setpositivebutton   :给对话框添加"yes"按钮
setnegativebutton :对话框添加"no"按钮
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 { 
    /** called when the activity is first created. */ 
    @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 {
 /** called when the activity is first created. */
 @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();
 }
}运行结果如下:
 Android的AlertDialog详解
 

二、带按钮的alertdialog
上面的例子很简单,下面我们在这个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 { 
    /** called when the activity is first created. */ 
    @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 {
 /** called when the activity is first created. */
 @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();
 }
}在这个例子中,我们定义了三个按钮,分别是"yes"按钮,"no"按钮以及一个普通按钮,每个按钮都有onclick事件,todo的地方可以放点了按钮之后想要做的一些处理
看一下运行结果:
 Android的AlertDialog详解

可以看到三个按钮添加到了alertdialog上,三个没有添加事件处理的按钮,点了只是关闭对话框,没有任何其他操作。
 
 
 
三、类似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 { 
    /** called when the activity is first created. */ 
    @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 {
 /** called when the activity is first created. */
 @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();
 }
}运行结果如下:
 Android的AlertDialog详解
 
 
 
四、类似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; 
     
    /** called when the activity is first created. */ 
    @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;
 
 /** called when the activity is first created. */
 @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();
 }
}
运行结果如下:
 Android的AlertDialog详解
 

五、类似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 { 
    /** called when the activity is first created. */ 
    @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 {
 /** called when the activity is first created. */
 @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();
 }
}运行结果如下:
 Android的AlertDialog详解
 
 
六、自定义view的alertdialog
有时候我们不能满足系统自带的alertdialog风格,就比如说我们要实现一个login画面,有用户名和密码,这时我们就要用到自定义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 { 
    /** called when the activity is first created. */ 
    @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 {
 /** called when the activity is first created. */
 @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详解
摘自 殇雲的专栏