1. 程式人生 > >Android開發中,dialog的實現方式

Android開發中,dialog的實現方式

在Android開發中,我們經常會需要在Android介面上彈出一些對話方塊,比如詢問使用者或者讓使用者選擇。這些功能我們叫它Android Dialog對話方塊,在我們使用Android的過程中,我歸納了一下,Android Dialog的型別無非也就7種,下面我分別向大家介紹這7種Android Dialog對話方塊的使用方法,希望對大家能有所幫助。

1.該效果是當按返回按鈕時彈出一個提示,來確保無誤操作,採用常見的對話方塊樣式。


建立dialog對話方塊方法程式碼如下:

  1. protectedvoid dialog() {   
  2.   AlertDialog.Builder builder = new
     Builder(Main.this);   
  3.   builder.setMessage("確認退出嗎?");   
  4.    builder.setTitle("提示");   
  5.    builder.setPositiveButton("確認"new OnClickListener() {   
  6.    @Override
  7.    publicvoid onClick(DialogInterface dialog, int which) {   
  8.      dialog.dismiss();   
  9.     Main.this.finish();   
  10.    }   
  11.   });   
  12.    builder.setNegativeButton("取消"
    new OnClickListener() {   
  13.    @Override
  14.     publicvoid onClick(DialogInterface dialog, int which) {   
  15.      dialog.dismiss();   
  16.    }   
  17.   });   
  18.    builder.create().show();   
  19.   }  

在onKeyDown(int keyCode, KeyEvent event)方法中呼叫此方法

  1. publicboolean onKeyDown(int keyCode, KeyEvent event) {   
  2.   if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 
    0) {   
  3.    dialog();   
  4.   }   
  5.   returnfalse;   
  6.   }  

2..改變了對話方塊的圖表,添加了三個按鈕


建立dialog的方法程式碼如下:

  1. Dialog dialog = new AlertDialog.Builder(this).setIcon(   
  2.       android.R.drawable.btn_star).setTitle("喜好調查").setMessage(   
  3.      "你喜歡李連杰的電影嗎?").setPositiveButton("很喜歡",   
  4.      new OnClickListener() {   
  5.       @Override
  6.       publicvoid onClick(DialogInterface dialog, int which) {   
  7.        // TODO Auto-generated method stub  
  8.         Toast.makeText(Main.this"我很喜歡他的電影。",   
  9.           Toast.LENGTH_LONG).show();   
  10.       }   
  11.      }).setNegativeButton("不喜歡"new OnClickListener() {   
  12.     @Override
  13.     publicvoid onClick(DialogInterface dialog, int which) {   
  14.      // TODO Auto-generated method stub  
  15.      Toast.makeText(Main.this"我不喜歡他的電影。", Toast.LENGTH_LONG)   
  16.         .show();   
  17.      }   
  18.    }).setNeutralButton("一般"new OnClickListener() {   
  19.     @Override
  20.     publicvoid onClick(DialogInterface dialog, int which) {   
  21.      // TODO Auto-generated method stub  
  22.      Toast.makeText(Main.this"談不上喜歡不喜歡。", Toast.LENGTH_LONG)   
  23.         .show();   
  24.     }   
  25.    }).create();   
  26.    dialog.show();  

3.資訊內容是一個簡單的View型別


建立dialog方法的程式碼如下:

  1. new AlertDialog.Builder(this).setTitle("請輸入").setIcon(   
  2.       android.R.drawable.ic_dialog_info).setView(   
  3.       new EditText(this)).setPositiveButton("確定"null)   
  4.      .setNegativeButton("取消"null).show();  

4.資訊內容是一組單選框


建立dialog方法的程式碼如下:

  1. new AlertDialog.Builder(this).setTitle("複選框").setMultiChoiceItems(   
  2.       new String[] { "Item1""Item2" }, nullnull)   
  3.      .setPositiveButton("確定"null)   
  4.      .setNegativeButton("取消"null).show();  

5.資訊內容是一組多選框


建立dialog方法的程式碼如下:

  1. new AlertDialog.Builder(this).setTitle("單選框").setIcon(   
  2.       android.R.drawable.ic_dialog_info).setSingleChoiceItems(   
  3.       new String[] { "Item1""Item2" }, 0,   
  4.       new DialogInterface.OnClickListener() {   
  5.        publicvoid onClick(DialogInterface dialog, int which) {   
  6.         dialog.dismiss();   
  7.        }   
  8.       }).setNegativeButton("取消"null).show();  

6.資訊內容是一組簡單列表項


建立dialog的方法程式碼如下:

  1. new AlertDialog.Builder(this).setTitle("列表框").setItems(   
  2.       new String[] { "Item1""Item2" }, null).setNegativeButton(   
  3.      "確定"null).show();  

7.資訊內容是一個自定義的佈局


dialog佈局檔案程式碼如下:

  1. <?xml version="1.0" encoding="utf-8"?>   
  2.  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.   android:layout_height="wrap_content" android:layout_width="wrap_content"
  4.   android:background="#ffffffff" android:orientation="horizontal"
  5.  android:id="@+id/dialog">   
  6.   <TextView android:layout_height="wrap_content"
  7.     android:layout_width="wrap_content"
  8.    android:id="@+id/tvname" android:text="姓名:" />   
  9.   <EditText android:layout_height="wrap_content"
  10.   android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>   
  11. </LinearLayout>  

建立dialog方法的程式碼如下:

  1. LayoutInflater inflater = getLayoutInflater();   
  2.    View layout = inflater.inflate(R.layout.dialog,   
  3.      (ViewGroup) findViewById(R.id.dialog));   
  4.    new AlertDialog.Builder(this).setTitle("自定義佈局").setView(layout)   
  5.       .setPositiveButton("確定"null)   
  6.      .setNegativeButton("取消"null).show();  

好了,以上7種Android dialog對話方塊的使用方法就介紹到這裡了,基本都全了