編輯:關於 Android 程式設計
Android 開發中經常需要在畫面上彈一個對話框,例如詢問使用者、讓使用者做選擇,這類元件統稱為 Dialog,而 AlertDialog 採用的是建造者模式(Builder)。
問題在於,AlertDialog 內建的樣式很難滿足所有需求。這時候的解法是用 setView() 塞入自訂版面。下面示範一個帶有帳號與密碼輸入框的登入提示框。

<?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>
密碼欄位用 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 之類的主題,效果比硬改版面更好。
踩坑實錄:新版 Android Studio 導入微信支付與支付寶 Demo 的那些問題
項目需要接入支付寶支付和微信支付。本來想用第三方聚合支付框架(ping++、BeeCloud 之類),但他們的收費模式讓我望而卻步;加上公司已經提供了完整的公鑰、私鑰和
MSM8909 + Android 5.1.1 BSP 開發:BatteryInfo.java 源碼解析
本文延續 MSM8909 平台的電池管理主題,聚焦在 Settings 應用中的 BatteryInfo 頁面——它是最直觀的電池狀態觀察窗口,也是排查底層問題的好入口。
Material Design 動畫六大主題:漣漪、揭示、路徑與向量動畫
之前談過 ActivityOptions 那套優雅的轉場動畫,但那份參考資料裡其實還藏了不少好東西。這篇就把剩下的部分一次補齊。 一、觸控回饋:漣漪效果 Materia
百思不得姐實作筆記:圖片的展示、縮放、保存與依網路狀態載入
一、本篇要完成的功能二、思路清單回顧上一篇的 cell 拆分方式 → 建立圖片專用的 view 類與 xib → 點擊圖片看大圖 → 長圖的捲動瀏覽 → 把顯示圖片的控制