天天看點

Android Dialog的使用方法總結

本文轉載自:http://www.oschina.net/question/54100_32486 7種形式的Android Dialog使用舉例

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

1.該效果是當按傳回按鈕時彈出一個提示,來確定無誤操作,采用常見的對話框樣式。

Android Dialog的使用方法總結

建立dialog對話框方法代碼如下:

view source print ?

01

protected

void

dialog() {

02

    AlertDialog.Builder builder =

new

Builder(Main.

this

);

03

    builder.setMessage(

"确認退出嗎?"

);

04

    builder.setTitle(

"提示"

);

05

    builder.setPositiveButton(

"确認"

,

new

OnClickListener() {

06

    

@Override

07

    

public

void

onClick(DialogInterface dialog,

int

which) {

08

      dialog.dismiss();

09

      Main.

this

.finish();

10

     }

11

    });

12

    builder.setNegativeButton(

"取消"

,

new

OnClickListener() {

13

    

@Override

14

    

public

void

onClick(DialogInterface dialog,

int

which) {

15

      dialog.dismiss();

16

     }

17

    });

18

    builder.create().show();

19

   }

在onKeyDown(int keyCode, KeyEvent event)方法中調用此方法

view source print ?

1

public

boolean

onKeyDown(

int

keyCode, KeyEvent event) {

2

   

if

(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() ==

) {

3

     dialog();

4

    }

5

   

return

false

;

6

   }

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

Android Dialog的使用方法總結

建立dialog的方法代碼如下:

view source print ?

01

Dialog dialog =

new

AlertDialog.Builder(

this

).setIcon(

02

       android.R.drawable.btn_star).setTitle(

"喜好調查"

).setMessage(

03

      

"你喜歡李連傑的電影嗎?"

).setPositiveButton(

"很喜歡"

,

04

      

new

OnClickListener() {

05

       

@Override

06

       

public

void

onClick(DialogInterface dialog,

int

which) {

07

        

// TODO Auto-generated method stub

08

         Toast.makeText(Main.

this

,

"我很喜歡他的電影。"

,

09

           Toast.LENGTH_LONG).show();

10

        }

11

       }).setNegativeButton(

"不喜歡"

,

new

OnClickListener() {

12

     

@Override

13

     

public

void

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

     

public

void

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類型

Android Dialog的使用方法總結

建立dialog方法的代碼如下:

view source print ?

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.資訊内容是一組單選框

Android Dialog的使用方法總結

建立dialog方法的代碼如下:

view source print ?

1

new

AlertDialog.Builder(

this

).setTitle(

"複選框"

).setMultiChoiceItems(

2

      

new

String[] {

"Item1"

,

"Item2"

},

null

,

null

)

3

       .setPositiveButton(

"确定"

,

null

)

4

       .setNegativeButton(

"取消"

,

null

).show();

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

Android Dialog的使用方法總結

建立dialog方法的代碼如下:

view source print ?

1

new

AlertDialog.Builder(

this

).setTitle(

"單選框"

).setIcon(

2

       android.R.drawable.ic_dialog_info).setSingleChoiceItems(

3

      

new

String[] {

"Item1"

,

"Item2"

},

,

4

      

new

DialogInterface.OnClickListener() {

5

       

public

void

onClick(DialogInterface dialog,

int

which) {

6

         dialog.dismiss();

7

        }

8

       }).setNegativeButton(

"取消"

,

null

).show();

6.資訊内容是一組簡單清單項

Android Dialog的使用方法總結

建立dialog的方法代碼如下:

view source print ?

1

new

AlertDialog.Builder(

this

).setTitle(

"清單框"

).setItems(

2

      

new

String[] {

"Item1"

,

"Item2"

},

null

).setNegativeButton(

3

      

"确定"

,

null

).show();

7.資訊内容是一個自定義的布局

Android Dialog的使用方法總結

dialog布局檔案代碼如下:

view source print ?

01

<?

xml

version

=

"1.0"

encoding

=

"utf-8"

?>

02

03

  <

LinearLayout

xmlns:android

=

"http://schemas.android.com/apk/res/android"

04

  

android:layout_height

=

"wrap_content"

android:layout_width

=

"wrap_content"

05

  

android:background

=

"#ffffffff"

android:orientation

=

"horizontal"

06

  

android:id

=

"@+id/dialog"

>

07

   <

TextView

android:layout_height

=

"wrap_content"

08

    

android:layout_width

=

"wrap_content"

09

   

android:id

=

"@+id/tvname"

android:text

=

"姓名:"

/>

10

   <

EditText

android:layout_height

=

"wrap_content"

11

   

android:layout_width

=

"wrap_content"

android:id

=

"@+id/etname"

android:minWidth

=

"100dip"

/>

12

  </

LinearLayout

>

建立dialog方法的代碼如下:

view source print ?

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對話框的使用方法就介紹到這裡了,基本都全了,如果大家在android開發過程中遇到dialog的時候就可以拿出來看看。