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

Android 自定義 AlertDialog 與 Activity 雙向傳遞數據的完整實踐

編輯:關於 Android 程式設計

在 Android 開發裡,自定義彈窗(AlertDialog)與 Activity 之間的數據互通,是很多初學者容易卡住的地方。本文將帶你動手實現兩個核心功能,並把背後的原理講透。

我們要實現什麼

簡單來說,目標有兩個:一是把 Activity 中 TextView 顯示的文字,帶到彈窗裡的 TextView 和 EditText 上;二是當使用者在彈窗的 EditText 中修改內容後,點擊確定,把新值回寫到 Activity 的 TextView。整個過程形成一個閉環。

兩個常見困惑,先替你解答

新手在動手之前,往往會卡在兩個問題上:

問題一:自定義的 layout 怎麼塞進 AlertDialog?
答案很直接——先透過 LayoutInflater 把 layout 轉成 View,再呼叫 AlertDialog.Builder 的 setView() 方法即可。

問題二:怎麼操作彈窗內部的控件?
思路和 Fragment 裡的操作幾乎一致:先拿到這個 layout 對應的 View,再用這個 View 去 findViewById 取得控件,就能自由操作了。

MainActivity 的實現

下面的代碼展示瞭如何在點擊按鈕時彈出自定義彈窗,並完成數據的雙向傳遞:

 package com.example.myalertdialog; import android.app.Activity; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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); //設置AlertDialog中TextView和EditText顯示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) { //將Activity中的textview顯示AlertDialog中EditText中的內容 //並且用Toast顯示一下 old_name.setText(et_name_dialog.getText().toString()); Toast.makeText(MainActivity.this, "設置成功!", Toast.LENGTH_SHORT).show(); } }) //由於“取消”的button我們沒有設置點擊效果,直接設為null就可以了 .setNegativeButton("取消", null) .create() .show(); } }); } } 

activity_main.xml 布局

 <!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <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="大西瓜"> </textview></textview></linearlayout><button android:id="@+id/bt_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改用戶名"></button></linearlayout>

my_dialog.xml 彈窗布局

 <!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <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!"> </textview></textview></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!"> </edittext></textview></linearlayout> </linearlayout> 

掌握這套思路後,你就能把自定義彈窗應用到各種場景,無論是修改資料、填寫表單還是二次確認,都能得心應手。

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