編輯:關於 Android 開發
本文要達成的目標:
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() 找,那是找不到的。
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();
}
});
}
}
<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>
<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() 的行為。
BaseHttpListActivity:快速打造 Android 網路列表頁
BaseHttpListActivity:快速打造 Android 網路列表頁 在 Android 開發裡,「向伺服器取得列表、顯示、刷新、載入更多、快取」幾乎是每個 A
Android 開發:數據存儲(一)——引導頁與 SharedPreferences
本章介紹 Android 開發中簡單的數據存儲,主要涉及 SharedPreferences 以及多頁面切換的 ViewPager,並透過一個「首次啟動引導頁」的實例加以
把校園資訊搬上手機桌面:即時更新校內數據的 Android 桌面部件
把校園資訊搬上手機桌面:即時更新校內數據的 Android 桌面部件 專案地址:https://github.com/hwding/AeolosXDUWidget 自己所
用 Kotlin 打造 Android 自訂視圖(KAD 06):多構造函數的優雅寫法
作者:Antonio Leiva | 時間:2016 年 12 月 27 日 | 原文:antonioleiva.com/custom-views-android-kot