首先是一個最簡單的應用,就是彈出一個消息框,在android中能夠這樣實現android
new AlertDialog.Builder(self) .setTitle("標題" ) .setMessage("簡單消息框" ) .setPositiveButton("肯定" , null ) .show();
效果以下:ui
-------------spa
下面是帶確認和取消按鈕的對話框code
new AlertDialog.Builder(self) .setTitle("確認" ) .setMessage("肯定嗎?" ) .setPositiveButton("是" , null ) .setNegativeButton("否" , null) .show();
效果以下:blog
--------------圖片
下面是一個能夠輸入文本的對話框it
new AlertDialog.Builder(self) .setTitle("請輸入" ) .setIcon(android.R.drawable.ic_dialog_info) .setView(new EditText(self)) .setPositiveButton("肯定" , null) .setNegativeButton("取消" , null ) .show();
效果圖以下:class
------------List
下面是單選框與多選框,也是很是有用的兩種對話框im
new AlertDialog.Builder(self) .setTitle("請選擇" ) .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems(new String[] {"選項1", "選項2", "選項3" , "選項4" }, 0 , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } } ) .setNegativeButton("取消" , null ) .show();
效果圖以下:
2.多選框:
new AlertDialog.Builder(self) .setTitle("多選框" ) .setMultiChoiceItems(new String[] {"選項1", "選項2", "選項3" , "選項4" }, null , null ) .setPositiveButton("肯定" , null) .setNegativeButton("取消" , null ) .show();
--------------
列表對話框:
new AlertDialog.Builder(self) .setTitle("列表框" ) .setItems(new String[] {"列表項1", "列表項2", "列表項3" }, null ) .setNegativeButton("肯定" , null ) .show();
--------------------
在對話框中顯示圖片:
ImageView img = new ImageView(self); img.setImageResource(R.drawable.icon); new AlertDialog.Builder(self) .setTitle("圖片框" ) .setView(img) .setPositiveButton("肯定" , null ) .show();