編輯:關於 Android 程式設計
在 Android 開發裡,自定義彈窗(AlertDialog)與 Activity 之間的數據互通,是很多初學者容易卡住的地方。本文將帶你動手實現兩個核心功能,並把背後的原理講透。
簡單來說,目標有兩個:一是把 Activity 中 TextView 顯示的文字,帶到彈窗裡的 TextView 和 EditText 上;二是當使用者在彈窗的 EditText 中修改內容後,點擊確定,把新值回寫到 Activity 的 TextView。整個過程形成一個閉環。



新手在動手之前,往往會卡在兩個問題上:
問題一:自定義的 layout 怎麼塞進 AlertDialog?
答案很直接——先透過 LayoutInflater 把 layout 轉成 View,再呼叫 AlertDialog.Builder 的 setView() 方法即可。
問題二:怎麼操作彈窗內部的控件?
思路和 Fragment 裡的操作幾乎一致:先拿到這個 layout 對應的 View,再用這個 View 去 findViewById 取得控件,就能自由操作了。
下面的代碼展示瞭如何在點擊按鈕時彈出自定義彈窗,並完成數據的雙向傳遞:
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(); } }); } }
<!--{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>
<!--{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>
掌握這套思路後,你就能把自定義彈窗應用到各種場景,無論是修改資料、填寫表單還是二次確認,都能得心應手。
九宮格手勢解鎖自定義 View 實作
效果說明這個自定義 View 的效果與手機常見的九點手勢解鎖類似。畫面中會出現九個點,使用者用手指依序滑過這些點,形成一組密碼,再進行驗證。實作思路整體實作可以拆成幾個部
手機QQ能截圖嗎?安卓開啟搖一搖截屏的完整流程
聊天時偶爾會遇到值得留存的畫面:一句讓人笑出來的對話、一張需要保存的資訊截圖,或是單純想備份某段記錄。手機QQ內建了截屏功能,只是預設關閉,需要手動開啟。安卓手機QQ開啟
【首發】在 Android Studio 中輕鬆搭建 JavaCV 開發環境
最近參加一個比賽需要用到人臉識別,但賽方限制了不能調用第三方雲服務 API,於是我想到了用 JavaCV 來自己實現。可搜索下來,絕大多數教程都是在 Eclipse 裡搭
Android Fragment 靜態加載與動態加載實戰演練
1、Fragment 的靜態使用Fragment 作為 Activity 介面的一部分,是內嵌在 Activity 之中的。多個 Fragment 可以把一個 Activ