Android Dialog用法总结

一、 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:text="User log in..."
    android:id="@+id/teBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>
Step 2: text_entry_dialog.xml

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        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" />
           
    <EditText
        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" />

    <TextView
        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" />
           
    <EditText
        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" />
</LinearLayout>


@font-face { font-family: "宋体"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "Calibri"; }@font-face { font-family: "@宋体"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0in 0in 10pt; line-height: 115%; font-size: 11pt; font-family: "Calibri","sans-serif"; }.MsoChpDefault { }.MsoPapDefault { margin-bottom: 10pt; line-height: 115%; }div.Section1 { page: Section1; } 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();
            }
        });
    }
}

你可能感兴趣的:(android,xml,.net,OS,Blog)