Android之AlertDialog详解

来源:互联网 发布:python random.choice 编辑:程序博客网 时间:2024/04/29 23:01
1,对话框是当前界面弹出的一个小窗口,用于显示重要提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等。一般情况下,用户要和对话框进行交互,然后返回到被遮盖的界面以继续当前的应用程序。
2,AlertDialog常用方法:
a)要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。
b)setTitle:为对话框设置标题。
c)setIcon:为对话框设置图标。
d)setMessage:为对话框设置内容。
e)setView:为对话话框设置自定义样式。
f ) setItems:设置对话框要显示的一个list,一般用于显示几个命令时。
g)setMultiChoiceItems:设置对话框显示一系列的复选框。
h)setSingleChoiceItems:设置单选按钮。
i)setNeutralButton:设置普通按钮。
j)setPositiveButton:给对话框添加“确定”按钮。

k)setNegativeButton:给对话框添加“取消”按钮。

l)setCancelable:设置对话框是否可以取消。

m)setCanceledOnTouchOutside:设置点击对话框外面是否取消对话框。

3,下面通过一个实例来介绍五种常见的对话框,分别是确认对话框、单选按钮对话框、多选按钮对话框、列表

对话框和自定义对话框。下面是五个按钮的布局文件:

<span style="font-size:18px;"><pre name="code" class="html"><span style="font-size:18px;"><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"    tools:context=".MainActivity" >    <Button        android:id="@+id/bt1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="普通对话框" />    <Button        android:id="@+id/bt2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="单选按钮对话框" />    <Button        android:id="@+id/bt3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="多选按钮对话框" />    <Button        android:id="@+id/bt4"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="列表对话框" />    <Button        android:id="@+id/bt5"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="自定义对话框" /></LinearLayout></span></span>

4,自定义对话框的布局文件(dialog_layout.xml):

<span style="font-size:18px;"><span style="font-size:18px;"><?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" >    <LinearLayout     android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">        <EditText        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="2"        android:hint="输入内容......" />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="提交"        android:layout_weight="1"/></LinearLayout><ImageView    android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:src="@drawable/camera"/></LinearLayout></span></span>

5,在MainActivity类中设置各个按钮的对话框:

<span style="font-size:18px;"><span style="font-size:18px;">package com.example.alertdialog2;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity {String[] single_list={"男","女"}; //用于单选按钮String[] multi_list={"篮球","爬山","游泳","乒乓球","听歌"}; //用于多选按钮String[] item_list={"项目经理","策划","美工","程序猿"}; //用于列表按钮    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initEven();    }    /*     * 初始化事件     *///初始化第一个按钮private void initEven() {findViewById(R.id.bt1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {//showDialog这个方法是在外部定义的,把这个方法引入到点击事件中showDialog1();}}); //初始化第二个按钮findViewById(R.id.bt2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubshowDialog2();}});// 初始化第三个按钮findViewById(R.id.bt3).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubshowDialog3();}});// 初始化第四个按钮findViewById(R.id.bt4).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubshowDialog4();}});//初始化第五个按钮findViewById(R.id.bt5).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubshowDialog5();}});}/** 显示确认按钮对话框*/public void showDialog1(){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("确认对话框");builder.setIcon(R.drawable.ic_launcher);builder.setMessage("确认对话框提示内容");/*这个OnClickListener引用的包为:android.content.DialogInterface.OnClickListener.*而上面的OnClickListener引用的包为:android.view.View.OnClickListener.*为了区分开,需要加在这个前面加上DialogInterface.*/builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "点击了确定按钮!", Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "点击了取消按钮!", Toast.LENGTH_SHORT).show();}});//获取dialogAlertDialog dialog=builder.create();//显示对话框dialog.show();}/**显示单选按钮对话框*/public void showDialog2(){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("选择性别");builder.setIcon(R.drawable.ic_launcher);// 单选按钮有三个参数,第一个数据源,第二个是当前选中的,第三个是监听事件builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int which) {String str=single_list[which];Toast.makeText(MainActivity.this, "性别是:"+str, Toast.LENGTH_SHORT).show();}});//获取dialogAlertDialog dialog=builder.create();//显示对话框dialog.show();} /**显示多选按钮对话框*/public void showDialog3() {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("选择爱好");builder.setIcon(R.drawable.ic_launcher);// 多选按钮有三个参数,第一个数据源,第二个是当前选中的(可以多个),第三个是监听事件builder.setMultiChoiceItems(multi_list, null,new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface arg0, int which,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {Toast.makeText(MainActivity.this,"我的爱好是:" + multi_list[which],Toast.LENGTH_SHORT).show();} else {Toast.makeText(MainActivity.this,"我不喜欢:" + multi_list[which],Toast.LENGTH_SHORT).show();}}});//隐藏对话框builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});AlertDialog dialog = builder.create();dialog.show();}/**显示列表话框*/public void showDialog4(){AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("选择职位");builder.setIcon(R.drawable.ic_launcher);builder.setItems(item_list, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int which) {Toast.makeText(MainActivity.this,"我是:" + item_list[which],Toast.LENGTH_SHORT).show();}});AlertDialog dialog=builder.create();dialog.show();}/**显示自定义按钮对话框*/public void showDialog5(){//通过inflate来获取自定义的视图LayoutInflater inflater=LayoutInflater.from(this);View view=inflater.inflate(R.layout.dialog_layout, null);AlertDialog.Builder builder=new AlertDialog.Builder(this);builder.setTitle("搜索相册");builder.setIcon(R.drawable.ic_launcher);//通过setView方法把自定义的视图添加到builderbuilder.setView(view);//获取dialogAlertDialog dialog=builder.create();//显示对话框dialog.show();}}</span></span>

0 0
原创粉丝点击