Android ApiDemos樣本解析(22):App->Dialog

來源:互聯網
上載者:User

這個例子的主Activity定義在AlertDialogSamples.java 主要用來介紹類AlertDialog的用法,AlertDialog提供的功能是多樣的:

顯示訊息給使用者,並可提供一到三個按鈕(OK, Cancel ,Yes ,No)用於選擇或是顯示警告。
顯示一個列表以供使用者選擇,列表中可以是Radio Button  (單選),Check button (多選)
顯示文字框來接受使用者輸入等。
建立AlertDialog一般是通過AlertDialog.Builder來構造:

 

[java] 
AlertDialog.Builder ad=new AlertDialog.Builder(context); 
AlertDialog.Builder ad=new AlertDialog.Builder(context);

 

之後可以為這個AlergDialog設定標題,顯示的資訊,需要顯示的Buttons等。然後調用 ad.show來顯示這個對話方塊。

為了避免每次顯示對話方塊都建立一個Dialog對象,Android提供兩個方法 onCreateDialog和onPrepareDialog事件來管理對話方塊的建立。

通過重載onCreateDialog,可以根據需要(如執行showDialog時)建立所需對話方塊執行個體。而在建立這個對話方塊執行個體後,在每次showDialog之前,如果需要對這個對話方塊做些修改可以重載onPrepareDialog方法來實現。 原理和Android管理Menu的方法類似。

下面給出使用AlertDialog的一般步驟。因為在onCreateDialog可能建立多個Dialog樣本,所以必須先定義一個Dialog的ID。

[java] view plaincopyprint?
private static final int DIALOG_YES_NO_MESSAGE = 1; 
private static final int DIALOG_YES_NO_MESSAGE = 1;

然後重載onCreateDialog,參數id為Dialog ID,可以根據id來建立需要的Dialog執行個體。

[java]
@Override 
protected Dialog onCreateDialog(int id) { 
 switch (id) { 
 case DIALOG_YES_NO_MESSAGE: 
 return new AlertDialog.Builder(AlertDialogSamples.this) 
 .setIcon(R.drawable.alert_dialog_icon) 
 .setTitle(R.string.alert_dialog_two_buttons_title) 
 .setPositiveButton(R.string.alert_dialog_ok, 
    new DialogInterface.OnClickListener() { 
 public void onClick(DialogInterface dialog, int whichButton) { 
  
 /* User clicked OK so do some stuff */ 
 } 
 }) 
 .setNegativeButton(R.string.alert_dialog_cancel, 
     new DialogInterface.OnClickListener() { 
 public void onClick(DialogInterface dialog, int whichButton) { 
  
 /* User clicked Cancel so do some stuff */ 
 } 
 }) 
 .create(); 
  
 ... 
@Override
protected Dialog onCreateDialog(int id) {
 switch (id) {
 case DIALOG_YES_NO_MESSAGE:
 return new AlertDialog.Builder(AlertDialogSamples.this)
 .setIcon(R.drawable.alert_dialog_icon)
 .setTitle(R.string.alert_dialog_two_buttons_title)
 .setPositiveButton(R.string.alert_dialog_ok,
    new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int whichButton) {
 
 /* User clicked OK so do some stuff */
 }
 })
 .setNegativeButton(R.string.alert_dialog_cancel,
     new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int whichButton) {
 
 /* User clicked Cancel so do some stuff */
 }
 })
 .create();
 
 ...

顯示Dialog

[java] 
showDialog(DIALOG_YES_NO_MESSAGE); 
showDialog(DIALOG_YES_NO_MESSAGE);

 

App->Dialog通過八個例子來說明AlertDialog的多種用法:

 

 

OK Cancel dialog with a message
前面顯示的代碼就是這個例子的代碼,使用AlertDialog.Builder建立一個AlertDialog樣本 ,然後通過setIcon,setTitle,setPositiveButton,setNegativeButton設定這個對話方塊的表徵圖,標題,OK和Cancel Button。 標題也不知道是什麼語言:
 

 
OK Cancel dialog with a long message
這個例子中使用setMessage來顯示一個很長的資訊(可以有捲軸),並通過setNeutralButton 添加一個中間按鈕。

 

List Dialog
AlertDialog 可以用來顯示一組選項來擷取使用者選擇。對應靜態選項可以先定義一個Array資源:
<!– Used in app/dialog examples –>
<string-array name=”select_dialog_items”>
<item>Command one</item>
< item>Command two</item>
< item>Command three</item>
< item>Command four</item>
< /string-array>
然後調用setItems 來顯示這些選項:
[java] 
1. case DIALOG_LIST: 
2.  return new AlertDialog.Builder(AlertDialogSamples.this) 
3.  .setTitle(R.string.select_dialog) 
4.  .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { 
5.  public void onClick(DialogInterface dialog, int which) { 
6.   
7.  /* User clicked so do some stuff */ 
8.  String[] items = getResources().getStringArray(R.array.select_dialog_items); 
9.  new AlertDialog.Builder(AlertDialogSamples.this) 
10.  .setMessage("You selected: " + which + " , " + items[which]) 
11.  .show(); 
12.  } 
13.  }) 
14.  .create(); 
 
 

Progress dialog
這個例子顯示了ProgressDialog的用法,ProgressDialog為AlertDialog的子類,ProgressDialog 無需通過AlertDialog.Builder 構造,可以直接通過建構函式來建立ProgressDialog的執行個體。ProgressDialog可以顯示一個標題和一個進度條。因此比AlertDialog多了幾個方法:setProgressStyle ,setMax等來配置進度條的屬性。
 
 


-(。

註: 這個例子還使用裡Handler 來更新進度條,Handler將在後面的例子介紹。
[java] 
1. case DIALOG_PROGRESS: 
2.  mProgressDialog = new ProgressDialog(AlertDialogSamples.this); 
3.  mProgressDialog.setIcon(R.drawable.alert_dialog_icon); 
4.  mProgressDialog.setTitle(R.string.select_dialog); 
5.  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
6.  mProgressDialog.setMax(MAX_PROGRESS); 
7.  mProgressDialog.setButton(getText(R.string.alert_dialog_hide), 
8.  new DialogInterface.OnClickListener() { 
9.  public void onClick(DialogInterface dialog, int whichButton) { 
10.   
11.  /* User clicked Yes so do some stuff */ 
12.  } 
13.  }); 
14.  mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), 
15.  new DialogInterface.OnClickListener() { 
16.  public void onClick(DialogInterface dialog, int whichButton) { 
17.   
18.  /* User clicked No so do some stuff */ 
19.  } 
20.  }); 
21.  return mProgressDialog; 
 
Single choice list
AlertDialog 顯示列表時,可以指定為單選或是多選。將前面List中的setItems方法改成setSingleChoiceItems可以使用RadioButton來顯示列表:
 
 
[java] 
1. case DIALOG_SINGLE_CHOICE: 
2.  return new AlertDialog.Builder(AlertDialogSamples.this) 
3.  .setIcon(R.drawable.alert_dialog_icon) 
4.  .setTitle(R.string.alert_dialog_single_choice) 
5.  .setSingleChoiceItems(R.array.select_dialog_items2, 0, 
6.  new DialogInterface.OnClickListener() { 
7.  public void onClick(DialogInterface dialog, int whichButton) { 
8.   
9.  /* User clicked on a radio button do some stuff */ 
10.  } 
11.  }) 
12.  .setPositiveButton(R.string.alert_dialog_ok, 
13.  new DialogInterface.OnClickListener() { 
14.  public void onClick(DialogInterface dialog, int whichButton) { 
15.   
16.  /* User clicked Yes so do some stuff */ 
17.  } 
18.  }) 
19.  .setNegativeButton(R.string.alert_dialog_cancel, 
20.  new DialogInterface.OnClickListener() { 
21.  public void onClick(DialogInterface dialog, int whichButton) { 
22.   
23.  /* User clicked No so do some stuff */ 
24.  } 
25.  }) 
26.  .create(); 
 


 
Repeat alarm
這個例子調用setMultiChoiceItems 方法使用CheckButton來顯示列表,表示可以多選
[java] 
1. case DIALOG_MULTIPLE_CHOICE: 
2.  return new AlertDialog.Builder(AlertDialogSamples.this) 
3.  .setIcon(R.drawable.ic_popup_reminder) 
4.  .setTitle(R.string.alert_dialog_multi_choice) 
5.  .setMultiChoiceItems(R.array.select_dialog_items3, 
6.  new boolean[]{false, true, false, true, false, false, false}, 
7.  new DialogInterface.OnMultiChoiceClickListener() { 
8.  public void onClick(DialogInterface dialog, 
9.  int whichButton, 
10.  boolean isChecked) { 
11.   
12.  /* User clicked on a check box do some stuff */ 
13.  } 
14.  }) 
15.  .setPositiveButton(R.string.alert_dialog_ok, 
16.  new DialogInterface.OnClickListener() { 
17.  public void onClick(DialogInterface dialog, int whichButton) { 
18.   
19.  /* User clicked Yes so do some stuff */ 
20.  } 
21.  }) 
22.  .setNegativeButton(R.string.alert_dialog_cancel, 
23.  new DialogInterface.OnClickListener() { 
24.  public void onClick(DialogInterface dialog, int whichButton) { 
25.   
26.  /* User clicked No so do some stuff */ 
27.  } 
28.  }) 
29.  .create(); 
 
 
Send Call to VoiceMail
上面例子中清單項目都是通過定義Array資源來實現的,清單項目還可以使用Content Provider作為資料來源。這個例子使用通訊錄中的記錄作為資料來源,如果在模擬器運行,請先添加幾個Contacts。Content Provider介紹請參見Android ApiDemo樣本解析(10):App->Activity->QuickContactsDemo
 
[java] 
1. case DIALOG_MULTIPLE_CHOICE_CURSOR: 
2. String[] projection = new String[] { 
3. Contacts.People._ID, 
4. Contacts.People.NAME, 
5. Contacts.People.SEND_TO_VOICEMAIL 
6. }; 
7. Cursor cursor = managedQuery(Contacts.People.CONTENT_URI, 
8. projection, null, null, null); 
9. return new AlertDialog.Builder(AlertDialogSamples.this) 
10. .setIcon(R.drawable.ic_popup_reminder) 
11. .setTitle(R.string.alert_dialog_multi_choice_cursor) 
12. .setMultiChoiceItems(cursor, 
13. Contacts.People.SEND_TO_VOICEMAIL, 
14. Contacts.People.NAME, 
15. new DialogInterface.OnMultiChoiceClickListener() { 
16. public void onClick(DialogInterface dialog, 
17. int whichButton, 
18. boolean isChecked) { 
19. Toast.makeText(AlertDialogSamples.this, 
20. "Readonly Demo Only 
21. - Data will not be updated", 
22. Toast.LENGTH_SHORT).show(); 
23. } 
24. }) 
25. .create(); 
 

 
 

 

Text Entry Dialog
最後一個例子是使用文字框來接受使用者輸入。AlertDialog也允許自訂UI,可以使用自訂的Layout做為AlertDialog的UI。 理論上可以使用任意的Layout,這樣可以避免使用派生Dialog的方法來自訂對話方塊。本例使用alert_dialog_text_entry ,其中定義裡兩個TextView顯示Name:和Password:標籤 ,兩個EditText接受使用者輸入。 然後使用setView為AlertDialog定義自訂的UI。
[java] 
1. case DIALOG_TEXT_ENTRY: 
2. // This example shows how to add a custom layout to an AlertDialog  
3. LayoutInflater factory = LayoutInflater.from(this); 
4. final View textEntryView 
5. = factory.inflate(R.layout.alert_dialog_text_entry, null); 
6. return new AlertDialog.Builder(AlertDialogSamples.this) 
7. .setIcon(R.drawable.alert_dialog_icon) 
8. .setTitle(R.string.alert_dialog_text_entry) 
9. .setView(textEntryView) 
10. .setPositiveButton(R.string.alert_dialog_ok, 
11. new DialogInterface.OnClickListener() { 
12. public void onClick(DialogInterface dialog, int whichButton) { 
13.   
14. /* User clicked OK so do some stuff */ 
15. } 
16. }) 
17. .setNegativeButton(R.string.alert_dialog_cancel, 
18. new DialogInterface.OnClickListener() { 
19. public void onClick(DialogInterface dialog, int whichButton) { 
20.   
21. /* User clicked cancel so do some stuff */ 
22. } 
23. }) 
24. .create(); 
 

 
作者:mapdigit
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.