ZetCode

对话框

最后修改:2012 年 11 月 26 日

在本章 Android 开发教程中,我们将讨论对话框。我们将讨论 Android 的 AlertDialog

对话框被定义为两人或多人之间的对话。在计算机应用程序中,对话框是一个用于与应用程序“交谈”的窗口。对话框用于输入数据、修改数据、更改应用程序设置等。

AlertDialog 是一个用于显示信息或接收数据的对话框。它可以显示一个、两个或三个按钮。它是使用 Builder 子类创建的。

显示消息

我们使用 AlertDialog 来显示消息。在此示例中,我们无需修改清单文件。

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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:onClick="onClicked"
        android:text="@string/btn_label" />
        
</LinearLayout>

main.xml 布局文件中,我们有一个 Button 小部件。此按钮将显示一个 AlertDialog

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">DisplaySize</string>
    <string name="btn_label">Show</string>
</resources>

这是 strings.xml 文件。

MainActivity.java
package com.zetcode.displaysize;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.graphics.Point;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.WindowManager;
import android.view.Display;

public class MainActivity extends Activity
{    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClicked(View view)
    {
        Point p = getDisplaySize();

        AlertDialog ad = new AlertDialog.Builder(this).create();

        ad.setTitle("Display size");
        String msg = String.format("Width:%d, Height:%d", p.x, p.y);
        ad.setMessage(msg);
        ad.setIcon(android.R.drawable.ic_dialog_info);

        ad.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        ad.show();     
    }

    public Point getDisplaySize()
    {
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display ds = wm.getDefaultDisplay();

        Point p = new Point();
        ds.getSize(p);

        return p;
    }
}

我们使用 AlertDialog 来显示显示器的大小。

Point p = getDisplaySize();

在自定义的 getDisplaySize() 方法中,我们确定了显示器的大小。

AlertDialog ad = new AlertDialog.Builder(this).create();

创建了一个 AlertDialog 实例。

ad.setTitle("Display size");
String msg = String.format("Width:%d, Height:%d", p.x, p.y);
ad.setMessage(msg);
ad.setIcon(android.R.drawable.ic_dialog_info);

我们为对话框设置了标题、消息和图标。

ad.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});

我们在对话框中添加了一个按钮。当我们单击“确定”按钮时,对话框将关闭。

ad.show();

show() 方法显示对话框。

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display ds = wm.getDefaultDisplay();

我们获取默认显示。

Point p = new Point();
ds.getSize(p);

我们使用 getSize() 方法找出显示器的大小。

AlertDialog showing the size of the display
图: AlertDialog 显示显示器大小

接收数据

第二个示例使用 AlertDialog 从用户那里接收数据。对话框将询问用户的姓名。然后,它将在 TextView 小部件中显示输入的内容。

清单文件未被修改。

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:id="@+id/btnId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:onClick="onClicked"
        android:text="@string/btn_label" />   
        
    <TextView
        android:id="@+id/tvId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
        
</LinearLayout>

这是 main.xml 文件。我们有一个 Button 小部件和一个 TextView 小部件。该按钮将显示对话框窗口。TextView 将接收来自对话框的输入文本。

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">InputDialog</string>
    <string name="btn_label">Show dialog</string>
</resources>

这是 strings.xml 资源文件。

MainActivity.java
package com.zetcode.input;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.content.DialogInterface;

public class MainActivity extends Activity
{
    private TextView tv;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView) findViewById(R.id.tvId);
    }

    public void onClicked(View view)
    {
        AlertDialog.Builder ad = new AlertDialog.Builder(this);

        ad.setTitle("Input");
        ad.setMessage("Enter your name");

        final EditText input = new EditText(this);
        ad.setView(input);

        ad.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int which) {
            String val = input.getText().toString();
            String msg = String.format("Hello %s!", val);
            tv.setText(msg);
          }
        });

        ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dlg, int which) {
            dlg.cancel();
          }
        });

        ad.show();
    }
}

单击按钮小部件将显示 AlertDialog。它有一个 EditText 来接收用户的输入。

AlertDialog.Builder ad = new AlertDialog.Builder(this);

ad.setTitle("Input");
ad.setMessage("Enter your name");

我们为对话框设置了标题和消息。

final EditText input = new EditText(this);
ad.setView(input);

我们将 EditText 小部件添加到对话框中。

ad.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) {
    String val = input.getText().toString();
    String msg = String.format("Hello %s!", val);
    tv.setText(msg);
    }
});

通过单击对话框的“确定”按钮,我们从 EditText 小部件获取文本。该文本用于格式化问候语,该问候语设置为 TextView

ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dlg, int which) {
    dlg.cancel();
    }
});

单击“取消”按钮将关闭对话框。

Receiving input
图:接收输入

在本章 Android 开发教程中,我们展示了 AlertDialog 的两种情况。