Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂 AlertDialog 與 Activity 雙向傳值實作

編輯:關於 Android 開發

Android 自訂 AlertDialog 與 Activity 雙向傳值實作

本文要達成的目標:

1、把 Activity 中 TextView 的字串取出來,設定到 AlertDialog 的 TextView 與 EditText 上。

2、把 AlertDialog 中 EditText 使用者輸入的值,回寫到 Activity 的 TextView。

實作完成後的效果如下:

新手在自訂 AlertDialog 時,疑問通常集中在兩點:

問題一:自訂的 layout 要怎麼放進 AlertDialog?

解答:先用 LayoutInflater 把 layout 檔案 inflate 成 View 物件,再直接呼叫 AlertDialog.Builder 的 setView() 方法帶入即可。

問題二:要怎麼操作自訂 AlertDialog 裡面的控制項?

解答:思路和在 Fragment 中操作 View 幾乎一樣——先取得該 layout 的 View,再透過這個 View 去 findViewById() 找出裡面的控制項來操作。切忌直接拿去 Activity 的 findViewById() 找,那是找不到的。

MainActivity

package com.example.myalertdialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    TextView old_name;
    Button bt_change_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        old_name = (TextView) findViewById(R.id.tv_name);
        bt_change_name = (Button) findViewById(R.id.bt_name);

        bt_change_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取得自訂 AlertDialog 版面檔的 View
                LinearLayout change_name = (LinearLayout) getLayoutInflater()
                        .inflate(R.layout.my_dialog, null);

                TextView tv_name_dialog =
                        (TextView) change_name.findViewById(R.id.tv_name_dialog);

                // EditText 要在匿名內部類中使用,因此必須宣告為 final
                final EditText et_name_dialog =
                        (EditText) change_name.findViewById(R.id.et_name_dialog);

                // 把 Activity 中 TextView 的內容帶進對話框
                tv_name_dialog.setText(old_name.getText().toString());
                et_name_dialog.setText(old_name.getText().toString());

                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("修改用戶名")
                        .setView(change_name)
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 把 EditText 的內容回寫到 Activity 的 TextView
                                old_name.setText(et_name_dialog.getText().toString());
                                Toast.makeText(MainActivity.this, "設置成功!",
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        // 「取消」沒有額外行為,直接給 null 即可
                        .setNegativeButton("取消", null)
                        .create()
                        .show();
            }
        });
    }
}

activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:text="原用戶名:" />

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="大西瓜" />
    </LinearLayout>

    <Button
        android:id="@+id/bt_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="修改用戶名" />
</LinearLayout>

my_dialog.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:text="原用戶名:" />

        <TextView
            android:id="@+id/tv_name_dialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:text="新用戶名:" />

        <EditText
            android:id="@+id/et_name_dialog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </LinearLayout>
</LinearLayout>

幾個容易忽略的細節

第一,inflate() 的第二個參數傳 null 是可以的,但記得把根節點的 layout_width/layout_height 都設為 match_parent,否則可能會出現 InvalidLayoutParams 的例外。

第二,匿名內部類要存取外層的區域變數,該變數必須是 final(Java 7 及以前的版本),這也是範例中 et_name_dialog 加上 final 的原因。

第三,如果希望點擊對話框外部或按返回鍵時能收起,記得搭配 setCanceledOnTouchOutside(true),並視情況覆寫 onBackPressed() 的行為。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved