Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 對話框完整教學:提示框、多按鈕、進度框、自訂版面與 Toast

對話框是 Android 裡最常出現的互動元件之一。本文把六種常見型態的做法整理成可直接複用的片段,從最簡單的提示框一路到自訂版面與模態視窗。

1. 最簡單的提示框

// 簡單提示框
private Dialog Dialog1(){
    return new AlertDialog.Builder(XXXActivity.this)
            .setTitle("提示")
            .setMessage("提示內容太短")
            .show();
}

2. 多按鈕提示框

把不同 Dialog 依 id 分派,是 Activity 管理多個對話框的傳統寫法:

@Override
protected Dialog onCreateDialog(int id) {
    if (id == 1) {          // 兩個按鈕
        return new AlertDialog.Builder(XXXActivity.this)
                .setTitle("提示").setMessage("提示內容太短")
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismissDialog(1);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismissDialog(1);
                    }
                }).create();
    } else if (id == 2) {   // 三個按鈕
        return new AlertDialog.Builder(XXXActivity.this)
                .setTitle("提示").setMessage("哈哈哈哈")
                .setIcon(R.drawable.ic_launcher)
                .setPositiveButton("注冊", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) { }
                })
                // 注意:第三顆按鈕用的是 setNeutralButton,不是 setNegativeButton
                .setNeutralButton("找回密碼", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) { }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) { }
                }).create();
    } else if (id == 4) {   // 圓形進度框
        ProgressDialog dialog = new ProgressDialog(XXXActivity.this);
        dialog.setTitle("正在下載歌曲...");
        dialog.setMessage("請稍候......");
        return dialog;
    }
    return super.onCreateDialog(id);
}

三顆按鈕時,中間那顆必須用 setNeutralButton,這是最容易搞錯的地方。要顯示時呼叫 showDialog(4) 即可。

3. 自訂對話框版面

先準備版面檔 xxxx.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="請輸入..."
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium"/>

    <EditText android:id="@+id/testdaysnum"
        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"/>

</LinearLayout>

Activity 中把它 inflate 出來,再塞進 AlertDialog:

// 自定義 Dialog
LayoutInflater factory = LayoutInflater.from(XXXActivity.this);
final View testDialogView = factory.inflate(R.layout.xxxx, null);
EditText edit_GetDayNum = (EditText) testDialogView.findViewById(R.id.testdaysnum);

new AlertDialog.Builder(XXXActivity.this)
    .setTitle("提示")
    .setView(testDialogView)
    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(XXXActivity.this, "請等待...", Toast.LENGTH_LONG).show();
        }
    }).create().show();

4. 圓形(不確定)進度框

ProgressDialog m_Dialog = ProgressDialog.show(
        XXXActivity.this, "請等待...", "請稍候....", true);

5. Toast 短訊

Toast.makeText(XXXActivity.this, "請等待...", Toast.LENGTH_LONG).show();

6. 模態對話框(背景模糊)

Dialog dialog = new Dialog(XXXActivity.this);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
dialog.show();

加上 FLAG_BLUR_BEHIND 後,對話框背後的畫面會被模糊處理,視覺焦點更集中,很適合需要使用者專注確認的場合。

Copyright © Linux教程網 All Rights Reserved