標籤:android ui 對話方塊 設計模式
Android-對話方塊
一 實現對話方塊的步驟Dialog
1 常用對話方塊:
AlertDialog(彈出對話方塊)
DatePickerDialog(日期選擇對話方塊)
TimePickerDialog(時間選擇對話方塊)
構建方法:
AlertDialog _alertDialog = new AlertDialog.Builder(MyContentMenuActivity.this).create();
builder設計模式適用於,構造參數較多的時候
2 建立步驟:
在onCreateDialog方法中建立一個AlertDialog.Builder執行個體
AlertDialog.set相應的屬性
build執行個體.create()產生一個AlertDialog的執行個體
AlertDialog.show(),在show的時候也會create()
3 建立按鈕:
在builder的時候調用setPositiveButton() and setNegativeButton()方法
二 自訂Dialog
1 直接利用基類Dialog的對象來setContentView來呈現不同的Dialog,可以看出Dialog與
Activity非常相似
2 利用AlertDialog建立的對象來直接setView即可實現自訂
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
3 如果想要自訂對話方塊,就用活動來作為對話方塊,而不是使用對話方塊API。
簡單建立一個活動並在<activity>清單manifest元素設定主題為Theme.Holo.Dialog。
<activity android:theme="@android:style/Theme.Holo.Dialog" >
Android-對話方塊