Android Dialog用法总结

来源:互联网 发布:阿里云建个人博客 编辑:程序博客网 时间:2024/05/15 23:52

一、AlertDialog.Builder
Android中的alertDialog的创建一般是通过其内嵌类AlertDialog.Builder来实现的。所以首先浏览一下这个builder所提供的方法:

setTitle():给对话框设置title.

setIcon():给对话框设置图标。

setMessage():设置对话框的提示信息

setItems():设置对话框要显示的一个list,一般用于要显示几个命令时

setSingleChoiceItems():设置对话框显示一个单选的List

setMultiChoiceItems():用来设置对话框显示一系列的复选框。

setPositiveButton():给对话框添加”Yes”按钮。

setNegativeButton():给对话框添加”No”按钮。

二、常见对话框:
在了解完这几个常用的方法之后,看一个小例子,创建一个用来提示的对话框:

 


Dialog dialog = new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(“title”)
                .setMessage(“这里是提示信息语句”)
                .setPositiveButton(“Ok”, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
   
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNeutralButton(“Cancel”, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Something so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Cancel so do some stuff */
                    }
                })
                .create();
dialog.show();//如果要显示对话框,一定要加上这句

 


另外在我的以前的一篇博客中的代码中介绍了如何创建一个包含single choice或者command list的对话框,具体请参考这里:http://blog.chinaunix.net/u/20947/showart_1962223.html

三、包含定制view的对话框:
很多时候,我们需要在对话框中显示一个特定的view,比如说用户登录对话框,就需要显示要用户输入用户名和密码的editBox等。

要想让对话框显示一个view,我们就要用到AlertDialog.Builder的setView(View view)方法来,可以看到该方法的参数是一个view,所以我们就需要先取得这个view,这个时候我们还要用到一个新的class,那就是LayoutFlater,它的作用就是将一个layout 的xml文件转化成一个view实例。

最后,还是用一个实例来演示一下如何在对话框中将定制的view作为其内容,该程序用来演示如何通过点击一个按钮来调用一个用来显示登陆的对话框界面,如图:


Step 1: main.xml



http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



Step 2: text_entry_dialog.xml



http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

   
        android:id="@+id/username_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="Username"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />
           
   
        android:id="@+id/username_edit"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

   
        android:id="@+id/password_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="Password"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />
           
   
        android:id="@+id/password_edit"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

Step 3: code

package com.zx.te;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class TextEntryDialogTest extends Activity {
    Button btnTEDlg;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        btnTEDlg = (Button)findViewById(R.id.teBtn);
        btnTEDlg.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                LayoutInflater factory = LayoutInflater.from(TextEntryDialogTest.this);
                final View textEntryView = factory.inflate(R.layout.text_entry_dialog, null);
                AlertDialog dlg = new AlertDialog.Builder(TextEntryDialogTest.this)
                .setTitle("User Login")
                .setView(textEntryView)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
   
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();
                dlg.show();
            }
        });
    }
}