編輯:關於 Android 開發
Android 自帶的對話框雖然能用,但樣式單調。為了更美觀,很多開發者會自定義對話框的佈局。下圖為點擊「彈出自定義對話框」按鈕後的效果:

先定義一個描述彈窗樣式的 XML,包含標題列、密碼輸入框與確定/取消兩個按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/airsign" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:gravity="center"
android:text="請輸入密碼"
android:textColor="#FFFFFFFF"
android:textSize="32px" />
</LinearLayout>
<EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/tv_background"
android:paddingLeft="10dp" />
<LinearLayout
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/background"
android:textColor="#FFFFFFFF"
android:text="確定" />
<Button
android:id="@+id/btn_delete"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:textColor="#FFFFFFFF"
android:background="@drawable/background"
android:text="取消" />
</LinearLayout>
</LinearLayout>
使用 AlertDialog.Builder 建立對話框,透過 setView() 載入自定義佈局。要特別注意:不加這行會導致自定義彈窗仍帶有原本的背景。另外,為了讓 EditText 點擊後能正常彈出輸入法,需要清除相關的 Window 旗標並設定軟輸入模式:
public class MainActivity extends Activity {
private Context context = MainActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickView(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
// 沒有這句會導致自定義對話框仍保留原有背景
builder.setView(View.inflate(this, R.layout.auto_dialog, null));
dialog.show();
// 讓 EditText 點擊後能顯示輸入法
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
// 取得自定義佈局中的控件
Window window = dialog.getWindow();
window.setContentView(R.layout.auto_dialog);
EditText et_pass = (EditText) window.findViewById(R.id.et_pass);
final Button btn_ok = (Button) window.findViewById(R.id.btn_ok);
final Button btn_delete = (Button) window.findViewById(R.id.btn_delete);
btn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(context, btn_ok.getText(), 0).show();
}
});
btn_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(context, btn_delete.getText(), 0).show();
}
});
}
}
關鍵在於用 window.setContentView() 把自定義佈局塞進對話框,再透過 window.findViewById() 取得各控件並綁定事件。掌握這個套路,就能做出各種風格化的彈窗。
Android 原始碼在線搜尋技巧:快速定位類別與方法
Android 原始碼在線搜尋技巧:快速定位類別與方法 在搜尋引擎普及之前,工程師遇到問題時常常只能直接閱讀原始碼。即使現在可以透過網路找到答案,若想理解 Android
Form 與 Control 的 KeyEvent 鍵盤事件處理
Form 與 Control 的 KeyEvent 鍵盤事件處理 在桌面應用開發中,鍵盤事件的處理經常派上用場,例如實作全域快捷鍵,或是限制輸入框只能接收特定字元。以下分
Android IPC 機制(五):用 Socket 實作跨進程聊天程式
Android IPC 機制(五):用 Socket 實作跨進程聊天程式 一、Socket 簡介 Socket 中文常譯作「插座」或「套接字」,它位於應用層與傳輸層之間,
在 Eclipse 中使用 StaggeredGridView 瀑布流
StaggeredGridView 簡介 StaggeredGridView 是一個常見的瀑布流排版元件,適合用於圖片牆、商品列表或內容卡片。雖然現在多數專案已改用 An