2011年7月3日 星期日

對話框(Dialog) - Android 新手學習筆記

在 Activity 中呼叫下面的 method
showDialog(int id)
或
showDialog(int id, Bundle args)
此時會觸發
protected Dialog onCreateDialog (int id)
或
protected Dialog onCreateDialog (int id, Bundle args)



這裡的 id 就是 showDialog 傳過來的 id,這不是元件的 id,只是一個讓你自己用以識別的數字,例如這支 Activity 可能依不同情況彈出不同的 dialog,此時要顯示 dialog 時,就呼叫 showDialg 並依不同情況傳入不同的 id 值,然後在 onCreateDialog 就能依不同的 id 值來實作不同的 Dialog

而 args (Bundle) 是用來傳遞資料的,Bundle 的用法有點像 java.util.Map,可以用一個 String key 對應一個 value,但不同的是,依不同的 value 型態,有不同的 method 可以直接使用,可以免去再轉型的動作,或也可以用原本類似 Map 的用法

最後,在 onCreateDialog 回傳你要顯示的 Dialog 實體,就可以了

Dialog 有很多種,所以產生 Dialog 實體的方法也有很多,例如(這是官網的範例)

詢問 是/否 的對話框

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
       .setCancelable(false)
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create();


List 選項的對話框

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

List 選項加上 Checkbox 或 Radio 的對話框

final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();

    上例 setSingleChoiceItems() 是單選的( 加 Radio )
    若要多選( 加 Checkbox ),則改用 setMultiChoiceItems()

自訂對話框
    新增一個 layout 檔 custom_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

    在要產生實體的地方這樣寫

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

    或是

AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                               (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

沒有留言:

張貼留言

廣告訊息會被我刪除