日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

信息提醒之对话框(AlertDialog + ProgressDialog)-更新中

發(fā)布時間:2025/3/21 编程问答 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 信息提醒之对话框(AlertDialog + ProgressDialog)-更新中 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

概念

Android中的對話框需要使用AlertDialog類來顯示,主要用于顯示提醒信息,不過這個對話框類可不僅僅能用來顯示一些信息,我們可以在對話框中防止任何的控件,使其成為一個復(fù)雜且功能強大的用戶接口。一個典型的例子就是使用AlertDialog做一個登錄對話框。

對話框的基本用法

通過查看AlertDialog類,我們可以發(fā)現(xiàn),該類并沒有public的構(gòu)造方法,因此我們不能直接創(chuàng)建AlertDialog對象。

為了創(chuàng)建AlertDialog對象,我們需要使用Builder類,該類是AlertDialog的內(nèi)部類。

首先,必須創(chuàng)建AlertDialog.Builder對象
然后,通過Builder的show方法顯示對話框
或者通過Builder.create方法返回AlertDialog對象,再通過AlertDiaolg.show方法顯示對話框。


帶2個按鈕(確認、取消)的對話框

顯示這樣的對話框的關(guān)鍵是如何顯示兩個按鈕以及響應(yīng)這兩個按鈕的單擊事件。 通過AlertDialog.setPostitiveButton和AlertDialog.setNegativeButton可以為對話框添加兩個按鈕。

我們來看下這兩個方法的定義

public Builder setPositiveButton(CharSequence text, final OnClickListener listener)public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener)public Builder setNegativeButton(CharSequence text, final OnClickListener listener) public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener)

從上述的源碼中可以看出,setPositiveButton和setNegativeButton方法各有兩個重載形式,實現(xiàn)的功能是一致的,區(qū)別在于 text參數(shù)可以直接指定文本或者String變量,textId參數(shù)則需要指定一個字符串資源ID(需要在res\values目錄中的xml文件中定義)。

一般來講,setPositiveButton的按鈕來添加 “確定”“Yes”等按鈕,setNegativeButton方法來添加“取消”,”cancel”等。

OnClickListener為DialogInterface.OnClickListener中的類。響應(yīng)用戶的操作。

new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_blue).setTitle("是否下載文件?").setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 提示信息AlertDialog dialog1 = new AlertDialog.Builder(DialogDemoListAct.this).setMessage("文件已下載成功").create();dialog1.show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 取消提示信息new AlertDialog.Builder(DialogDemoListAct.this).setMessage("文件下載取消").create().show();}}).show();

在使用AlertDialog類來創(chuàng)建對話框時需要注意以下幾點:

  • setPositvieButton和setNegativeButton方法的第二個參數(shù)的數(shù)據(jù)類型是android.content,DialogInterface.OnClickListener,而不是android.view.View.OnClickListener. View.OnClickListener接口使用在視圖上的,這一點在使用時要注意。
  • 使用show()方法顯示對話框是異步的,也就是說,當(dāng)調(diào)用AlertDialog.Builder.show 或者AlertDialog.show方法顯示對話框后,show方法會立即返回,并且繼續(xù)執(zhí)行后面的代碼。
  • 單擊使用setPositiveButton和setNegativeButton方法添加的按鈕后,即使單擊事件中不寫任何代碼,對話框也是會關(guān)閉的
  • 如果某個按鈕單擊后只需要關(guān)閉對話框,并不需要進行任何處理,listener參數(shù)設(shè)置null即可
  • AlertDialog類還有很多setXXX方法用于指定對話框的其他資源,可以是對話框更加豐滿美觀。

帶3個按鈕(覆蓋、忽略、取消)的對話框

用AlertDialog類創(chuàng)建的對話框最多可以添加3個按鈕,除了上面添加兩個方法,還可以使用setNeutralButton方法向?qū)υ捒蛑刑砑拥谌齻€按鈕

new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_violet).setTitle("是否覆蓋文件?").setMessage("覆蓋后源文件將丟失...吧啦吧啦").setPositiveButton("覆蓋", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).setNeutralButton("忽略", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {new AlertDialog.Builder(DialogDemoListAct.this).setMessage("忽略覆蓋文件操作").create().show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();

注意事項:

  • setPositiveButton setNegativeButton setNeutralButton的調(diào)用順序可以是任意的,但是無論調(diào)用順序是什么,在2.3這些低版本中,setPositiveButton 總會在左起第一個 ,setNeutralButton位于左起第二個,setNegativeButton位于左起第三個。 在高版本中,Googel調(diào)整了顯示位置,setPositiveButton位于右起第一位,setNeutralButton位于右起第二個,setNegativeButton位于右起第三位。
  • 使用AlertDialog類創(chuàng)建的對話框最多只能有3個按鈕,因此,就算多次調(diào)用這3個設(shè)置按鈕的方法,最多也只能顯示3個。
  • 這3個設(shè)置對話框按鈕的方法雖然可以調(diào)用多次,但是系統(tǒng)只以每一個方法最后一次調(diào)用的為準。

簡單列表對話框-setItems

通過AlertDialog.Builder類的setItems方法可以創(chuàng)建簡單的列表對話框。 實際上,這種對話框相當(dāng)于將ListView控件放在對話框上,然后在ListView中添加若干簡單的文本()。

在這個實例中,選擇后顯示選中值,5S后自動關(guān)閉。

setItems方法定義如下

// items表示用于顯示在列表中的字符串?dāng)?shù)組 public Builder setItems(CharSequence[] items, final OnClickListener listener) // items標識字符串?dāng)?shù)據(jù)的資源ID public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener)

第二個參數(shù) 為 DialogInterface.OnClickListener類型。

new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_gray).setTitle("請選擇省份").setItems(proviences, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 選擇后的提示信息 記得調(diào)用show方法,否則不顯示啦final AlertDialog alertDialog = new AlertDialog.Builder(DialogDemoListAct.this).setMessage("選擇了" + proviences[which]).show();// 設(shè)置定時器,5秒后,關(guān)閉AlertDialogHandler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() { // alertDialog.cancel(); 也可以alertDialog.dismiss();}}, 5 * 1000);}}).show();

在DialogInterface接口中有兩個用于關(guān)閉對話框的方法:dismiss 和 cancel,這兩個方法的功能完全相同,都是關(guān)閉對話框。


單選列表對話框-setSingleChoiceItems

通過AlertDialog.Builder類的setSingleChoiceItems方法可以創(chuàng)建帶有單選按鈕的列表對話框。setSingleChoiceItems有4個重載形式:

// 從字符串?dāng)?shù)組中裝載數(shù)據(jù) public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) // 從資源文件中裝載數(shù)據(jù) public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,final OnClickListener listener) // 從ListAdapter中裝載數(shù)據(jù) public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) // 從數(shù)據(jù)集中裝載數(shù)據(jù) public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,final OnClickListener listener)

參數(shù)解釋:

  • 第一個參數(shù):表示單選列表對話框的數(shù)據(jù)源,目前支持4種數(shù)據(jù)源,分別是 數(shù)據(jù)資源、數(shù)據(jù)集、字符串?dāng)?shù)組和ListAdapter對象
  • checkedItem:表示默認選中的列表項。如果設(shè)置第一個列表項為選中狀態(tài),該參數(shù)值為0 。 如果該值小于0,表示所有的列表項都未被選中。
  • listener: 單擊某個列表項被觸發(fā)的事件對象
  • lableColumn:如果數(shù)據(jù)源是數(shù)據(jù)集Cursor,數(shù)據(jù)集中的某一列作為列表對話框的數(shù)據(jù)加載到列表控件中。該參數(shù)表示該列的名稱(字段名)

// 用于保存當(dāng)前列表項索引private int index;final String[] proviences = {"北京", "上海", "廣州", "深圳", "紐約", "華盛頓", "拉斯維加斯"};new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_violet).setTitle("選擇省份").setSingleChoiceItems(proviences, -1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 保存當(dāng)前選中的列表項索引index = which;}}).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 提示信息new AlertDialog.Builder(DialogDemoListAct.this)// 使用index.setMessage(index + " " + proviences[index]).show();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {new AlertDialog.Builder(DialogDemoListAct.this).setMessage("您沒有選擇").show();}}).show();

多選列表對話框-setMultiChoiceItems

通過AlertDialog.Builder.setMultiChoiceItems方法可以創(chuàng)建帶復(fù)選框的列表對話框。

setMultiChoiceItems有3個重載方法

// 從資源文件中裝載數(shù)據(jù) public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems,final OnMultiChoiceClickListener listener) // 從字符串?dāng)?shù)組中裝載數(shù)據(jù) public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,final OnMultiChoiceClickListener listener) // 從數(shù)據(jù)集中裝載數(shù)據(jù) public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,final OnMultiChoiceClickListener listener)

參數(shù)解釋:

  • 第一個參數(shù)表示多選列表對話框的數(shù)據(jù)源,目前支持3種數(shù)據(jù)源:數(shù)組資源、數(shù)據(jù)集和字符串?dāng)?shù)組
  • checkedItems:該參數(shù)的數(shù)據(jù)類型為boolean[],這個參數(shù)值的長度要和列表框中的列表項個數(shù)相同,該參數(shù)用于設(shè)置每一個列表項的默認值,默認為true,表示當(dāng)前的列表項是選中狀態(tài),否則表示未選中狀態(tài)
  • listener:表示選中某一哥列表項時被觸發(fā)的事件對象
  • isCheckedColumn:該參數(shù)只用于數(shù)據(jù)集Cursor數(shù)據(jù)源,用于指定數(shù)據(jù)集的一列(字段名);如果為0,則未被選中。也就是說,對于數(shù)據(jù)集來說,某個列表項是否被選中,是有另外一列的字段值決定的。
  • labelColumn:只用于數(shù)據(jù)集。指定用于顯示列表項的列的字段名。

// 多選對話框中的數(shù)據(jù)lvprivate ListView lv;AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.tag_red).setTitle("選擇省份").setMultiChoiceItems(proviences,new boolean[]{false, false, false, false, false, false, false},new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {}}).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {int count = lv.getCount();String s = "您選擇了:";// 遍歷for (int i = 0; i < proviences.length; i++) {if (lv.getCheckedItemPositions().get(i)) {s += i + ":" + lv.getAdapter().getItem(i) + " ";}}// 判斷數(shù)量if (lv.getCheckedItemPositions().size() > 0) {new AlertDialog.Builder(DialogDemoListAct.this).setMessage(s).show();} else {new AlertDialog.Builder(DialogDemoListAct.this).setMessage("Nothing selected").show();}}}).setNegativeButton("取消", null).create();// 獲取lvlv = ad.getListView();// 顯示AlertDialog ,show為異步方法,執(zhí)行后,會繼續(xù)執(zhí)行下面的代碼,在這里需要在最后調(diào)用ad.show();

注意事項:

  • 必須指定setMulitChoiceItems方法的單擊事件對象,也就是改方法的第三個參數(shù),該參數(shù)不能設(shè)置為null,否則默認被選中的列表項無法置成未選中狀態(tài)。對于默認未被選中的列表項沒有任何影響。
  • 由于存在“確定”按鈕的單擊事件中需要引用AlertDialog變量,因此先使用create方法返回AlertDialog對象,然后在單擊事件中使用該變量

進度對話框

查看大拿的總結(jié)

進度對話框通過android.app.ProgressDialog類實現(xiàn),該類是AlertDialog的之類,但與AlertDialog類不同,我們可以直接使用new關(guān)鍵字創(chuàng)建ProgressDialog對象。

進度條對話框除了要設(shè)置普通對話框必要的值外,還需要設(shè)置另外兩個值:進度的最大值和當(dāng)前的進度。

// 設(shè)置進度的最大值 public void setMax(int max) // 設(shè)置當(dāng)前的進度 public void setProgress(int value)

初始進度必須使用setProgress方法設(shè)置,而遞增進度除了可以使用setProgress方法設(shè)置外,還可以使用以下方法

// 設(shè)置進度值的增量 public void incrementProgressBy(int diff)

區(qū)別在于 setProgress設(shè)置的絕對值,incrementProgressBy設(shè)置的是進度的增量。

與普通的對話框一樣,進度對話框最多也只能添加3個按鈕,而且可以設(shè)置進度對話框的風(fēng)格:

// 創(chuàng)建ProgressDialog類 ProgressDialog pg = new ProgressDialog(); // 設(shè)置進度對話框為水平進度條風(fēng)格 pg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

案例說明:
本案例演示了水平和原型進度對話框的實現(xiàn)方法,其中進度條包含兩個按鈕“暫停”和 “停止”,單擊暫停后,進度對話框關(guān)閉,再此顯示進度對話框時,進度條的起始位置從上次關(guān)閉對話框的位置開始(僅限與水平進度條)。 單擊取消,關(guān)閉對話框,再此顯示時,進度從0開始。

要實現(xiàn)進度隨著時間的變化而不斷遞增,需要使用多線程及定時器來完成這個工作, 本例中使用Handler類來不斷更新進度對話框的進度值。

// 水平進度條的最大值 private static final int MAX_PROGRESS = 100; // 默認的初始值 private int progress = 0;private void showProgressBarDialog(int style) {final ProgressDialog progressDialog = new ProgressDialog(this);// 設(shè)置提示的title的圖標,默認是沒有的progressDialog.setIcon(R.drawable.flag_mark_red);progressDialog.setTitle("數(shù)據(jù)處理中...");progressDialog.setMessage("請稍后...");// 設(shè)置進度對話框的風(fēng)格 ,默認是圓形的progressDialog.setProgressStyle(style);// 設(shè)置是否可以通過點擊Back鍵取消 默認trueprogressDialog.setCancelable(false);// 設(shè)置在點擊Dialog外是否取消Dialog進度條 默認trueprogressDialog.setCanceledOnTouchOutside(false);// 設(shè)置最大值progressDialog.setMax(MAX_PROGRESS);// 設(shè)置暫停按鈕progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "暫停", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 通過刪除消息代碼的方式停止定時器progressDialogHandler.removeMessages(PROGRESSDIALOG_FLAG);}});progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "停止", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {progressDialogHandler.removeMessages(PROGRESSDIALOG_FLAG);progress = 0;progressDialog.setProgress(progress);}});// 展示progressDialog.show();progressDialogHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (progress >= MAX_PROGRESS) {// 消失 并重置初始值progressDialog.dismiss();progress = 0;} else {progress++;progressDialog.incrementProgressBy(1);// 隨機設(shè)置下一次遞增進度 (50 +毫秒)progressDialogHandler.sendEmptyMessageDelayed(1, 50 + new Random().nextInt(500));}}};// 設(shè)置進度初始值progress = (progress > 0) ? progress : 0;progressDialog.setProgress(progress);// 發(fā)送消息progressDialogHandler.sendEmptyMessage(PROGRESSDIALOG_FLAG);}

注意事項:

  • 進度對話框默認是圓形進度條,如需要設(shè)置水平進度條,使用setProgressStyle方法進行設(shè)置
  • 調(diào)用sendEmptyMessage方法只能是handleMessage方法執(zhí)行一次,要想以一定的時間間隔循環(huán)執(zhí)行handleMessage方法,需要在handleMessage方法中調(diào)用sendEmptyMessageDelayed方法來設(shè)置hanleMessage方法下一次被調(diào)用的等待時間,這樣就可以形成一個循環(huán)調(diào)用的效果。
  • sendEmptyMessage和 sendEmptyMessageDelayed方法的第一個參數(shù)表示消息代碼,這個消息代碼用來標識消息隊列中的消息。 例如,使用sendMessageDelayed方法設(shè)置消息代碼為1的消息在(50+)毫秒后調(diào)用handleMessage方法,可以利用這個消息代碼刪除該消息,這樣系統(tǒng)就不會在(50+)毫秒之后調(diào)用handleMessage方法了。 在本例中,暫停和取消按鈕單擊事件都使用removeMessages方法刪除了消息代碼為1的消息。
  • 消息代碼可以是任意int類型的值
  • 雖然ProgressDialog.getProgress可以獲取當(dāng)前進度,但是只有在水平進度條風(fēng)格的對話框中才有效,如果是圓形進度條,該方法返回永遠是0 。 因此本案例單獨使用了一個progress變量來代替當(dāng)前進度,當(dāng)進度條風(fēng)格是圓形時,就意味著對話框永遠不會被關(guān)閉。
  • 圓形進度條對話框的進度圓圈只是一個普通的動畫,并沒有任何表示進度的功能,這種對話框一般在很難估計準確的時間和進度時使用

登錄對話框,自定義布局 -setView

我們可以直接使用布局文件或者代碼創(chuàng)建視圖對象,并將這些屬兔對象添加到對話框中。

AlertDialog.Builder.setView方法可以將視圖對象添加到當(dāng)前的對話框中,使用下面的形式將一個視圖對象添加到對話框中。

new AlertDialog.Builder(this).setIcon(R.drawable.xxx).setTitle("自定義對話框").setView(......).show();


主要代碼:

private void showCustomViewDialog() {// 第一種方式 將布局文件轉(zhuǎn)換為viewLayoutInflater inflater = LayoutInflater.from(this);View view = inflater.inflate(R.layout.activity_alertdialog_login, null);// 第二種方式 因為R.layout.activity_login的根布局是LinearLayout//LinearLayout view = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_alertdialog_login, null);new AlertDialog.Builder(this).setIcon(R.drawable.tag_red).setTitle("用戶登錄").setView(view).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 登錄邏輯}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 取消邏輯}}).show();}

activity_alertdialog_login.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用戶名:"android:textSize="20sp" /><EditText android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"/></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 碼:"android:textSize="20sp" /><EditText android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:password="true" /></LinearLayout></LinearLayout>

使用Activity托管對話框

Activity類提供了創(chuàng)建對話框的快捷方式。 在Activity類中有一個onCreateDialog方法。定義如下

protected Dialog onCreateDialog(int id)

當(dāng)調(diào)用Activity.showDialog方法時,系統(tǒng)會調(diào)用onCreateDialog方法來返回一個Dialog對象 (AlertDialog是Dialog的子類)。 showDialog方法和onCreateDialog方法一樣,也有一個int類型的id參數(shù)。該參數(shù)值傳入onCreateDialog方法。可以利用不同的id來建立多個對話框。

import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ListView;import com.turing.base.R;public class ActivityDialog extends Activity implements View.OnClickListener {private final int DIALOG_DELETE_FILE = 1;private final int DIALOG_SIMPLE_LIST = 2;private final int DIALOG_SINGLE_CHOICE_LIST = 3;private final int DIALOG_MULTI_CHOICE_LIST = 4;private ListView lv = null;private String[] provinces = new String[]{"遼寧省", "山東省", "河北省", "福建省", "廣東省", "黑龍江省"};private ButtonOnClick buttonOnClick = new ButtonOnClick(1);@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnDeleteFile:showDialog(DIALOG_DELETE_FILE);break;case R.id.btnSimpleList:showDialog(DIALOG_SIMPLE_LIST);break;case R.id.btnSingleChoiceList:showDialog(DIALOG_SINGLE_CHOICE_LIST);break;case R.id.btnMultiChoiceList:showDialog(DIALOG_MULTI_CHOICE_LIST);break;case R.id.btnRemoveDialog:removeDialog(DIALOG_DELETE_FILE);removeDialog(DIALOG_SIMPLE_LIST);removeDialog(DIALOG_SINGLE_CHOICE_LIST);removeDialog(DIALOG_MULTI_CHOICE_LIST);break;}}@Overrideprotected Dialog onCreateDialog(int id) {Log.d("dialog", String.valueOf(id));switch (id) {case DIALOG_DELETE_FILE:return new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_gray).setTitle("是否刪除文件").setPositiveButton("確定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {new AlertDialog.Builder(ActivityDialog.this).setMessage("文件已經(jīng)被刪除.").create().show();}}).setNegativeButton("取消",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {new AlertDialog.Builder(ActivityDialog.this).setMessage("您已經(jīng)選擇了取消按鈕,該文件未被刪除.").create().show();}}).create();case DIALOG_SIMPLE_LIST:return new AlertDialog.Builder(this).setTitle("選擇省份").setItems(provinces, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {final AlertDialog ad = new AlertDialog.Builder(ActivityDialog.this).setMessage("您已經(jīng)選擇了: " + which + ":"+ provinces[which]).show();android.os.Handler hander = new android.os.Handler();hander.postDelayed(new Runnable() {@Overridepublic void run() {ad.dismiss();}}, 5 * 1000);}}).create();case DIALOG_SINGLE_CHOICE_LIST:return new AlertDialog.Builder(this).setTitle("選擇省份").setSingleChoiceItems(provinces, 1, buttonOnClick).setPositiveButton("確定", buttonOnClick).setNegativeButton("取消", buttonOnClick).create();case DIALOG_MULTI_CHOICE_LIST:AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_blue).setTitle("選擇省份").setMultiChoiceItems(provinces, new boolean[]{false, true, false, true, false, false},new DialogInterface.OnMultiChoiceClickListener() {public void onClick(DialogInterface dialog,int whichButton, boolean isChecked) {}}).setPositiveButton("確定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {int count = lv.getCount();String s = "您選擇了:";for (int i = 0; i < provinces.length; i++) {if (lv.getCheckedItemPositions().get(i))s += i + ":"+ lv.getAdapter().getItem(i)+ " ";}if (lv.getCheckedItemPositions().size() > 0) {new AlertDialog.Builder(ActivityDialog.this).setMessage(s).show();} else {new AlertDialog.Builder(ActivityDialog.this).setMessage("您未選擇任何省份").show();}}}).setNegativeButton("取消", null).create();lv = ad.getListView();return ad;}return null;}private class ButtonOnClick implements DialogInterface.OnClickListener {private int index;public ButtonOnClick(int index) {this.index = index;}@Overridepublic void onClick(DialogInterface dialog, int whichButton) {if (whichButton >= 0) {index = whichButton;} else {if (whichButton == DialogInterface.BUTTON_POSITIVE) {new AlertDialog.Builder(ActivityDialog.this).setMessage("您已經(jīng)選擇了: " + index + ":" + provinces[index]).show();} else if (whichButton == DialogInterface.BUTTON_NEGATIVE) {new AlertDialog.Builder(ActivityDialog.this).setMessage("您什么都未選擇.").show();}}}}@Overrideprotected void onPrepareDialog(int id, Dialog dialog) {super.onPrepareDialog(id, dialog);}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_activity_dialog);Button btnDeleteFile = (Button) findViewById(R.id.btnDeleteFile);Button btnSimpleList = (Button) findViewById(R.id.btnSimpleList);Button btnSingleChoiceList = (Button) findViewById(R.id.btnSingleChoiceList);Button btnMultiChoiceList = (Button) findViewById(R.id.btnMultiChoiceList);Button btnRemoveDialog = (Button) findViewById(R.id.btnRemoveDialog);btnDeleteFile.setOnClickListener(this);btnSimpleList.setOnClickListener(this);btnSingleChoiceList.setOnClickListener(this);btnMultiChoiceList.setOnClickListener(this);btnRemoveDialog.setOnClickListener(this);} }

對話框的高級應(yīng)用

改變對話框的顯示位置

默認對話框的位置都是位于屏幕的中央,其實可以根據(jù)需要位于屏幕的上下左右甚至是任意位置,

要控制對話框的顯示位置,需要獲得對話框的Window對象,并通過Window對象的一些方法來控制對話框的顯示位置。

case 12: // 改變對話框的顯示位置 // changePostionOfDialog(Gravity.TOP); // changePostionOfDialog(Gravity.BOTTOM); // changePostionOfDialog(Gravity.LEFT); // changePostionOfDialog(Gravity.RIGHT);// 右上方 // changePostionOfDialog(Gravity.RIGHT | Gravity.TOP);// 顯示在任意位置showAnyPostionOfDilaog();break; private void changePostionOfDialog(int postion) {/**AlertDialog alertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_blue).setTitle("改變位置的AlertDiaolog").setMessage("我在" + postion).create();alertDialog.getWindow().setGravity(postion);alertDialog.show();**/ProgressDialog progressDialog = new ProgressDialog(this);progressDialog.setIcon(R.drawable.flag_mark_yellow);progressDialog.setTitle("ProgressDialog改變位置");progressDialog.setMessage("where am I ?");progressDialog.getWindow().setGravity(postion);progressDialog.show();} private void showAnyPostionOfDilaog() {AlertDialog ad = new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_blue).setTitle("改變位置的AlertDiaolog").setMessage("我在自定義的任意位置").create();Window window = ad.getWindow();WindowManager.LayoutParams lp = window.getAttributes();// 設(shè)置水平偏移量lp.x = -20;// 設(shè)置垂直偏移量lp.y = -120;window.setAttributes(lp);ad.show();}

在對話框按鈕和內(nèi)容文本中插入圖像

給TextView控件中插入圖像的方法同樣也適用。

AlertDialog alertDialog = new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_blue).setTitle("問候").setMessage(Html.fromHtml("哈哈,<img src=''/>你好.", new Html.ImageGetter() {@Overridepublic Drawable getDrawable(String source) {Drawable drawable = getResources().getDrawable(R.drawable.face);drawable.setBounds(0, 0, 32, 32);return drawable;}}, null)).setPositiveButton(Html.fromHtml("<img src=''/>確定", new Html.ImageGetter() {@Overridepublic Drawable getDrawable(String source) {Drawable drawable = getResources().getDrawable(R.drawable.ok);drawable.setBounds(0, 0, 20, 20);return drawable;}}, null), null).setNegativeButton(Html.fromHtml("<img src=''/>取消", new Html.ImageGetter() {@Overridepublic Drawable getDrawable(String source) {Drawable drawable = getResources().getDrawable(R.drawable.cancel);drawable.setBounds(0, 0, 20, 20);return drawable;}}, null), null).create();alertDialog.show();

改變對話框的透明度

通過WindowManager.LayoutParams.alpha可以設(shè)置對話框的透明度。
Alpha的取值范圍為0.0f ~ 1.0f之間,f表示float類型的數(shù)字。 默認1.0f ,完全不透明。 0.0f表示全透明,此時就看不到對話框了。

0.7f

private void showTransparency_dialog(float v) {// 創(chuàng)建對話框AlertDialog ad = new AlertDialog.Builder(this).setTitle("改變對話框的透明度").setIcon(R.drawable.tag_red).setMessage("Alpha的取值范圍 0~1 ,默認 1 ,我的透明度是" + v).setPositiveButton("確定",null).create();// 設(shè)置透明度Window window = ad.getWindow();WindowManager.LayoutParams lp = window.getAttributes();lp.alpha = v ;window.setAttributes(lp);// 展示對話框ad.show();}

總結(jié)

以上是生活随笔為你收集整理的信息提醒之对话框(AlertDialog + ProgressDialog)-更新中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。