信息提醒之对话框(AlertDialog + ProgressDialog)-更新中
概念
Android中的對(duì)話框需要使用AlertDialog類來顯示,主要用于顯示提醒信息,不過這個(gè)對(duì)話框類可不僅僅能用來顯示一些信息,我們可以在對(duì)話框中防止任何的控件,使其成為一個(gè)復(fù)雜且功能強(qiáng)大的用戶接口。一個(gè)典型的例子就是使用AlertDialog做一個(gè)登錄對(duì)話框。
對(duì)話框的基本用法
通過查看AlertDialog類,我們可以發(fā)現(xiàn),該類并沒有public的構(gòu)造方法,因此我們不能直接創(chuàng)建AlertDialog對(duì)象。
為了創(chuàng)建AlertDialog對(duì)象,我們需要使用Builder類,該類是AlertDialog的內(nèi)部類。
首先,必須創(chuàng)建AlertDialog.Builder對(duì)象
然后,通過Builder的show方法顯示對(duì)話框
或者通過Builder.create方法返回AlertDialog對(duì)象,再通過AlertDiaolg.show方法顯示對(duì)話框。
帶2個(gè)按鈕(確認(rèn)、取消)的對(duì)話框
顯示這樣的對(duì)話框的關(guān)鍵是如何顯示兩個(gè)按鈕以及響應(yīng)這兩個(gè)按鈕的單擊事件。 通過AlertDialog.setPostitiveButton和AlertDialog.setNegativeButton可以為對(duì)話框添加兩個(gè)按鈕。
我們來看下這兩個(gè)方法的定義
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方法各有兩個(gè)重載形式,實(shí)現(xiàn)的功能是一致的,區(qū)別在于 text參數(shù)可以直接指定文本或者String變量,textId參數(shù)則需要指定一個(gè)字符串資源ID(需要在res\values目錄中的xml文件中定義)。
一般來講,setPositiveButton的按鈕來添加 “確定”“Yes”等按鈕,setNegativeButton方法來添加“取消”,”cancel”等。
OnClickListener為DialogInterface.OnClickListener中的類。響應(yīng)用戶的操作。
在使用AlertDialog類來創(chuàng)建對(duì)話框時(shí)需要注意以下幾點(diǎn):
- setPositvieButton和setNegativeButton方法的第二個(gè)參數(shù)的數(shù)據(jù)類型是android.content,DialogInterface.OnClickListener,而不是android.view.View.OnClickListener. View.OnClickListener接口使用在視圖上的,這一點(diǎn)在使用時(shí)要注意。
- 使用show()方法顯示對(duì)話框是異步的,也就是說,當(dāng)調(diào)用AlertDialog.Builder.show 或者AlertDialog.show方法顯示對(duì)話框后,show方法會(huì)立即返回,并且繼續(xù)執(zhí)行后面的代碼。
- 單擊使用setPositiveButton和setNegativeButton方法添加的按鈕后,即使單擊事件中不寫任何代碼,對(duì)話框也是會(huì)關(guān)閉的
- 如果某個(gè)按鈕單擊后只需要關(guān)閉對(duì)話框,并不需要進(jìn)行任何處理,listener參數(shù)設(shè)置null即可
- AlertDialog類還有很多setXXX方法用于指定對(duì)話框的其他資源,可以是對(duì)話框更加豐滿美觀。
帶3個(gè)按鈕(覆蓋、忽略、取消)的對(duì)話框
用AlertDialog類創(chuàng)建的對(duì)話框最多可以添加3個(gè)按鈕,除了上面添加兩個(gè)方法,還可以使用setNeutralButton方法向?qū)υ捒蛑刑砑拥谌齻€(gè)按鈕
注意事項(xiàng):
- setPositiveButton setNegativeButton setNeutralButton的調(diào)用順序可以是任意的,但是無論調(diào)用順序是什么,在2.3這些低版本中,setPositiveButton 總會(huì)在左起第一個(gè) ,setNeutralButton位于左起第二個(gè),setNegativeButton位于左起第三個(gè)。 在高版本中,Googel調(diào)整了顯示位置,setPositiveButton位于右起第一位,setNeutralButton位于右起第二個(gè),setNegativeButton位于右起第三位。
- 使用AlertDialog類創(chuàng)建的對(duì)話框最多只能有3個(gè)按鈕,因此,就算多次調(diào)用這3個(gè)設(shè)置按鈕的方法,最多也只能顯示3個(gè)。
- 這3個(gè)設(shè)置對(duì)話框按鈕的方法雖然可以調(diào)用多次,但是系統(tǒng)只以每一個(gè)方法最后一次調(diào)用的為準(zhǔn)。
簡(jiǎn)單列表對(duì)話框-setItems
通過AlertDialog.Builder類的setItems方法可以創(chuàng)建簡(jiǎn)單的列表對(duì)話框。 實(shí)際上,這種對(duì)話框相當(dāng)于將ListView控件放在對(duì)話框上,然后在ListView中添加若干簡(jiǎn)單的文本()。
在這個(gè)實(shí)例中,選擇后顯示選中值,5S后自動(dòng)關(guān)閉。
setItems方法定義如下
// items表示用于顯示在列表中的字符串?dāng)?shù)組 public Builder setItems(CharSequence[] items, final OnClickListener listener) // items標(biāo)識(shí)字符串?dāng)?shù)據(jù)的資源ID public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener)第二個(gè)參數(shù) 為 DialogInterface.OnClickListener類型。
new AlertDialog.Builder(this).setIcon(R.drawable.flag_mark_gray).setTitle("請(qǐng)選擇省份").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è)置定時(shí)器,5秒后,關(guān)閉AlertDialogHandler handler = new Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() { // alertDialog.cancel(); 也可以alertDialog.dismiss();}}, 5 * 1000);}}).show();在DialogInterface接口中有兩個(gè)用于關(guān)閉對(duì)話框的方法:dismiss 和 cancel,這兩個(gè)方法的功能完全相同,都是關(guān)閉對(duì)話框。
單選列表對(duì)話框-setSingleChoiceItems
通過AlertDialog.Builder類的setSingleChoiceItems方法可以創(chuàng)建帶有單選按鈕的列表對(duì)話框。setSingleChoiceItems有4個(gè)重載形式:
// 從字符串?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ù)解釋:
- 第一個(gè)參數(shù):表示單選列表對(duì)話框的數(shù)據(jù)源,目前支持4種數(shù)據(jù)源,分別是 數(shù)據(jù)資源、數(shù)據(jù)集、字符串?dāng)?shù)組和ListAdapter對(duì)象
- checkedItem:表示默認(rèn)選中的列表項(xiàng)。如果設(shè)置第一個(gè)列表項(xiàng)為選中狀態(tài),該參數(shù)值為0 。 如果該值小于0,表示所有的列表項(xiàng)都未被選中。
- listener: 單擊某個(gè)列表項(xiàng)被觸發(fā)的事件對(duì)象
- lableColumn:如果數(shù)據(jù)源是數(shù)據(jù)集Cursor,數(shù)據(jù)集中的某一列作為列表對(duì)話框的數(shù)據(jù)加載到列表控件中。該參數(shù)表示該列的名稱(字段名)
多選列表對(duì)話框-setMultiChoiceItems
通過AlertDialog.Builder.setMultiChoiceItems方法可以創(chuàng)建帶復(fù)選框的列表對(duì)話框。
setMultiChoiceItems有3個(gè)重載方法
// 從資源文件中裝載數(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ù)解釋:
- 第一個(gè)參數(shù)表示多選列表對(duì)話框的數(shù)據(jù)源,目前支持3種數(shù)據(jù)源:數(shù)組資源、數(shù)據(jù)集和字符串?dāng)?shù)組
- checkedItems:該參數(shù)的數(shù)據(jù)類型為boolean[],這個(gè)參數(shù)值的長(zhǎng)度要和列表框中的列表項(xiàng)個(gè)數(shù)相同,該參數(shù)用于設(shè)置每一個(gè)列表項(xiàng)的默認(rèn)值,默認(rèn)為true,表示當(dāng)前的列表項(xiàng)是選中狀態(tài),否則表示未選中狀態(tài)
- listener:表示選中某一哥列表項(xiàng)時(shí)被觸發(fā)的事件對(duì)象
- isCheckedColumn:該參數(shù)只用于數(shù)據(jù)集Cursor數(shù)據(jù)源,用于指定數(shù)據(jù)集的一列(字段名);如果為0,則未被選中。也就是說,對(duì)于數(shù)據(jù)集來說,某個(gè)列表項(xiàng)是否被選中,是有另外一列的字段值決定的。
- labelColumn:只用于數(shù)據(jù)集。指定用于顯示列表項(xiàng)的列的字段名。
注意事項(xiàng):
- 必須指定setMulitChoiceItems方法的單擊事件對(duì)象,也就是改方法的第三個(gè)參數(shù),該參數(shù)不能設(shè)置為null,否則默認(rèn)被選中的列表項(xiàng)無法置成未選中狀態(tài)。對(duì)于默認(rèn)未被選中的列表項(xiàng)沒有任何影響。
- 由于存在“確定”按鈕的單擊事件中需要引用AlertDialog變量,因此先使用create方法返回AlertDialog對(duì)象,然后在單擊事件中使用該變量
進(jìn)度對(duì)話框
查看大拿的總結(jié)
進(jìn)度對(duì)話框通過android.app.ProgressDialog類實(shí)現(xiàn),該類是AlertDialog的之類,但與AlertDialog類不同,我們可以直接使用new關(guān)鍵字創(chuàng)建ProgressDialog對(duì)象。
進(jìn)度條對(duì)話框除了要設(shè)置普通對(duì)話框必要的值外,還需要設(shè)置另外兩個(gè)值:進(jìn)度的最大值和當(dāng)前的進(jìn)度。
// 設(shè)置進(jìn)度的最大值 public void setMax(int max) // 設(shè)置當(dāng)前的進(jìn)度 public void setProgress(int value)初始進(jìn)度必須使用setProgress方法設(shè)置,而遞增進(jìn)度除了可以使用setProgress方法設(shè)置外,還可以使用以下方法
// 設(shè)置進(jìn)度值的增量 public void incrementProgressBy(int diff)區(qū)別在于 setProgress設(shè)置的絕對(duì)值,incrementProgressBy設(shè)置的是進(jìn)度的增量。
與普通的對(duì)話框一樣,進(jìn)度對(duì)話框最多也只能添加3個(gè)按鈕,而且可以設(shè)置進(jìn)度對(duì)話框的風(fēng)格:
// 創(chuàng)建ProgressDialog類 ProgressDialog pg = new ProgressDialog(); // 設(shè)置進(jìn)度對(duì)話框?yàn)樗竭M(jìn)度條風(fēng)格 pg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);案例說明:
本案例演示了水平和原型進(jìn)度對(duì)話框的實(shí)現(xiàn)方法,其中進(jìn)度條包含兩個(gè)按鈕“暫?!焙?“停止”,單擊暫停后,進(jìn)度對(duì)話框關(guān)閉,再此顯示進(jìn)度對(duì)話框時(shí),進(jìn)度條的起始位置從上次關(guān)閉對(duì)話框的位置開始(僅限與水平進(jìn)度條)。 單擊取消,關(guān)閉對(duì)話框,再此顯示時(shí),進(jìn)度從0開始。
要實(shí)現(xiàn)進(jìn)度隨著時(shí)間的變化而不斷遞增,需要使用多線程及定時(shí)器來完成這個(gè)工作, 本例中使用Handler類來不斷更新進(jìn)度對(duì)話框的進(jìn)度值。
注意事項(xiàng):
- 進(jìn)度對(duì)話框默認(rèn)是圓形進(jìn)度條,如需要設(shè)置水平進(jìn)度條,使用setProgressStyle方法進(jìn)行設(shè)置
- 調(diào)用sendEmptyMessage方法只能是handleMessage方法執(zhí)行一次,要想以一定的時(shí)間間隔循環(huán)執(zhí)行handleMessage方法,需要在handleMessage方法中調(diào)用sendEmptyMessageDelayed方法來設(shè)置hanleMessage方法下一次被調(diào)用的等待時(shí)間,這樣就可以形成一個(gè)循環(huán)調(diào)用的效果。
- sendEmptyMessage和 sendEmptyMessageDelayed方法的第一個(gè)參數(shù)表示消息代碼,這個(gè)消息代碼用來標(biāo)識(shí)消息隊(duì)列中的消息。 例如,使用sendMessageDelayed方法設(shè)置消息代碼為1的消息在(50+)毫秒后調(diào)用handleMessage方法,可以利用這個(gè)消息代碼刪除該消息,這樣系統(tǒng)就不會(huì)在(50+)毫秒之后調(diào)用handleMessage方法了。 在本例中,暫停和取消按鈕單擊事件都使用removeMessages方法刪除了消息代碼為1的消息。
- 消息代碼可以是任意int類型的值
- 雖然ProgressDialog.getProgress可以獲取當(dāng)前進(jìn)度,但是只有在水平進(jìn)度條風(fēng)格的對(duì)話框中才有效,如果是圓形進(jìn)度條,該方法返回永遠(yuǎn)是0 。 因此本案例單獨(dú)使用了一個(gè)progress變量來代替當(dāng)前進(jìn)度,當(dāng)進(jìn)度條風(fēng)格是圓形時(shí),就意味著對(duì)話框永遠(yuǎn)不會(huì)被關(guān)閉。
- 圓形進(jìn)度條對(duì)話框的進(jìn)度圓圈只是一個(gè)普通的動(dòng)畫,并沒有任何表示進(jìn)度的功能,這種對(duì)話框一般在很難估計(jì)準(zhǔn)確的時(shí)間和進(jìn)度時(shí)使用
登錄對(duì)話框,自定義布局 -setView
我們可以直接使用布局文件或者代碼創(chuàng)建視圖對(duì)象,并將這些屬兔對(duì)象添加到對(duì)話框中。
AlertDialog.Builder.setView方法可以將視圖對(duì)象添加到當(dāng)前的對(duì)話框中,使用下面的形式將一個(gè)視圖對(duì)象添加到對(duì)話框中。
new AlertDialog.Builder(this).setIcon(R.drawable.xxx).setTitle("自定義對(duì)話框").setView(......).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托管對(duì)話框
Activity類提供了創(chuàng)建對(duì)話框的快捷方式。 在Activity類中有一個(gè)onCreateDialog方法。定義如下
protected Dialog onCreateDialog(int id)當(dāng)調(diào)用Activity.showDialog方法時(shí),系統(tǒng)會(huì)調(diào)用onCreateDialog方法來返回一個(gè)Dialog對(duì)象 (AlertDialog是Dialog的子類)。 showDialog方法和onCreateDialog方法一樣,也有一個(gè)int類型的id參數(shù)。該參數(shù)值傳入onCreateDialog方法??梢岳貌煌膇d來建立多個(gè)對(duì)話框。
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);} }對(duì)話框的高級(jí)應(yīng)用
改變對(duì)話框的顯示位置
默認(rèn)對(duì)話框的位置都是位于屏幕的中央,其實(shí)可以根據(jù)需要位于屏幕的上下左右甚至是任意位置,
要控制對(duì)話框的顯示位置,需要獲得對(duì)話框的Window對(duì)象,并通過Window對(duì)象的一些方法來控制對(duì)話框的顯示位置。
case 12: // 改變對(duì)話框的顯示位置 // 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();}在對(duì)話框按鈕和內(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();改變對(duì)話框的透明度
通過WindowManager.LayoutParams.alpha可以設(shè)置對(duì)話框的透明度。
Alpha的取值范圍為0.0f ~ 1.0f之間,f表示float類型的數(shù)字。 默認(rèn)1.0f ,完全不透明。 0.0f表示全透明,此時(shí)就看不到對(duì)話框了。
0.7f
private void showTransparency_dialog(float v) {// 創(chuàng)建對(duì)話框AlertDialog ad = new AlertDialog.Builder(this).setTitle("改變對(duì)話框的透明度").setIcon(R.drawable.tag_red).setMessage("Alpha的取值范圍 0~1 ,默認(rèn) 1 ,我的透明度是" + v).setPositiveButton("確定",null).create();// 設(shè)置透明度Window window = ad.getWindow();WindowManager.LayoutParams lp = window.getAttributes();lp.alpha = v ;window.setAttributes(lp);// 展示對(duì)話框ad.show();}總結(jié)
以上是生活随笔為你收集整理的信息提醒之对话框(AlertDialog + ProgressDialog)-更新中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信息提醒之Toast-更新中
- 下一篇: 信息提醒之Notification,兼容