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

自訂 AlertDialog 對話框:做一個有 username / password 欄位的登入視窗

編輯:關於 Android 程式設計

Android 開發中經常需要在畫面上彈一個對話框,例如詢問使用者、讓使用者做選擇,這類元件統稱為 Dialog,而 AlertDialog 採用的是建造者模式(Builder)。

問題在於,AlertDialog 內建的樣式很難滿足所有需求。這時候的解法是用 setView() 塞入自訂版面。下面示範一個帶有帳號與密碼輸入框的登入提示框。

觸發用的版面(alertdialog.xml)

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

     <Button
         android:text="自定義登陸"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/button5"
         android:onClick="login"/>
 </LinearLayout>

對話框內容的版面(login_layout.xml)

密碼欄位用 inputType="textPassword" 讓輸入內容以圓點遮蔽。

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

     <EditText
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:inputType="text"
         android:hint="請輸入用戶名"
         android:id="@+id/et_username"/>

     <EditText
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:inputType="textPassword"
         android:hint="請輸入密碼"
         android:id="@+id/et_password"/>
 </LinearLayout>

程式碼實作

 public class AlertDialogDemo extends AppCompatActivity {

     private EditText et_username, et_password;

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

     public void login(View v) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setTitle("登錄");

         // 透過 LayoutInflater 載入 login_layout
         View view = getLayoutInflater().inflate(R.layout.login_layout, null);

         // 取得兩個輸入框(這裡只示範取值,不做實際登入驗證)
         final EditText et_username = (EditText) view.findViewById(R.id.et_username);
         final EditText et_password = (EditText) view.findViewById(R.id.et_password);

         builder.setView(view);        // 把自訂版面設為對話框內容
         builder.setCancelable(false); // 不允許按返回鍵關閉

         // 正面按鈕
         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
                 String name = et_username.getText().toString().trim();
                 String pass = et_password.getText().toString().trim();
                 Toast.makeText(AlertDialogDemo.this, name + "正在登錄....", Toast.LENGTH_SHORT).show();
             }
         });

         // 反面按鈕
         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
                 Toast.makeText(AlertDialogDemo.this, "取消登錄", Toast.LENGTH_SHORT).show();
             }
         });

         builder.show(); // 顯示對話框
     }
 }

幾個實務上的建議

  • 自訂版面若需要動態操作(例如切換顯示密碼、改按鈕文字),記得先呼叫 builder.create() 取得 AlertDialog 物件,再用 dialog.findViewById() 去取元件,而不是在自訂 View 上取。
  • 若想自訂整體外觀(圓角、背景、字色),可以搭配 style 與 Theme.MaterialComponents.Dialog.Alert 之類的主題,效果比硬改版面更好。
  • 密碼欄位的值切勿寫進 Log,正式專案請改用 HTTPS 傳輸並在伺服器端驗證。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved