介紹一些關於AlertDialog的基本知識:
一、AlertDialog簡介:AlertDialog的構造方法被聲明為protected,所以不能直接使用new關鍵字來創建 AlertDialog類的對象實例。要想創建AlertDialog對話框,需要使用Builder類,該類是AlertDialog類中定義的一個內 嵌類。因此必須創建AlertDialog.Builder類的對象實例,然后再調用show()來顯示對話框。
二、使用AlertDialog創建對話框的種類:
1. 最多帶3個按鈕的對話框:setPositiveButton(...)--確認、setNegativeButton(...)--取消、setNeutralButton(...)--忽略
2.簡單列表對話框:通過AlertDialog.Builder類的setItems(...)方法可以創建簡單的列表對話框。其實,這種類型的對話框相當於將ListView組件放在對話框上,然后再在ListView中添加若干簡單的文本。
3.單選列表對話框:通過AlertDialog.Builder類的setSingleChoiceItems(...)來創建。目前支持4種數據源(數組資源、數據集、字符串數組、ListAdapter)
4.多選列表對話框:通過AlertDialog.Builder類的setMultiChoiceItems(...)創建。目前支持3種數據源(數組資源、數據集、字符串數組)
5.水平進度或圓形對話框(默認是:圓形):該類型的對話框是通過ProgressDialog來實現,該類是AlertDialog的子類,它不需要用create()方法來返回實例對象,只需要new即可。
ProgressDialog.STYLE_HORIZONTAL //水平進度樣式
ProgressDialog.STYLE_SPINNER //圓形樣式
6.自定義對話框:直接使用XML布局文件或以編寫JAVA代碼方式來創建視圖,並將這些視圖對象添加到對話框中去。
7.使用Activity托管對話框:Activity類中也提供了創建對話框的方式,有個onCreateDialog(int id)的方法,其返回類型是Dialog,通過是當調用Activity類的showDialog(int id)方法時,系統會調用該方法來返回一個Dialog對象。showDialog和onCreateDialog都有一個int類型的id參數,該參數 值將傳遞給onCreateDialog方法。因此,我們可以利用不同的id創建多個對話框。
***注意***:對於表示某一個對話框的ID,系統只在第1次調用showDialog方法時調用onCreateDialog方法。在第1次創建 Dialog對象時系統會將該對象保存在Activity的緩存里,相當於一個Map對象,對話框的ID作為Map的Key,而Dialog對象作為 Map的Value。下次再調用時,會先根據這個ID從Map中獲得第1次創建的Dialog對象。除非該ID已經被刪除。
8.懸浮對話框和觸摸任何位置都可以關閉的對話框:
1).懸浮對話框:android:theme="@android:style/Theme.Dialog";對於該類型的對話框,觸摸屏幕任何位置都會觸發Activity的OnTouchEvent事件。
2).觸摸任何位置都可以關閉的對話框:首先必須要繼承AlertDialog類,並重寫OnTouchEvent事件。
第一種:
Java代碼
- /**
- * 自定義AlertDialog
- *
- * @author chenjianli 2011-05-10
- */
- public void alert(){
- WindowManager manager = getWindowManager();
- Display display = manager.getDefaultDisplay();
- int width = display.getWidth();
- int height = display.getHeight();
- LayoutInflater inflater = getLayoutInflater();
- View view = inflater.inflate(R.layout.alert, null);
- TextView text = (TextView)view.findViewById(R.id.text);
- text.setText("自定義AlertDialog");
- AlertDialog alert = new AlertDialog.Builder(this).create();
- alert.show();
- alert.getWindow().setLayout(width/2, height/4);
- alert.setTitle("測試");
- alert.getWindow().setContentView(R.layout.alert);
- }
第二種:
Java代碼
- /**
- * 自定義AlertDialog
- *
- * @author chenjianli 2011-05-10
- */
- AlertDialog zidongbofangDialog = new AlertDialog.Builder(ManHuaActivity.this).create();
- zidongbofangDialog.show();
- zidongbofangDialog.getWindow().setGravity(Gravity.CENTER);
- zidongbofangDialog.getWindow().setLayout(
- android.view.WindowManager.LayoutParams.FILL_PARENT,
- android.view.WindowManager.LayoutParams.WRAP_CONTENT);
- zidongbofangDialog.getWindow().setContentView(R.layout.manhua_dialog_zidongbofang);
第三種:
/**
* 自定義AlertDialog
*
* @author chenjianli 2011-05-10
*/
如果我們setView(),中的View是帶EditText的,此時,我們必須在show()之前加上這么一句話,才可以在點擊EditText時彈出鍵盤,否則將很杯具!鍵盤是彈不出來的。
Java代碼
- AlertDialog tiaozhuanDialog= new AlertDialog.Builder(ManHuaActivity.this).create();
- tiaozhuanDialog.setView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
- tiaozhuanDialog.show();
- tiaozhuanDialog.getWindow().setGravity(Gravity.CENTER);
- tiaozhuanDialog.getWindow().setLayout(
- android.view.WindowManager.LayoutParams.FILL_PARENT,
- android.view.WindowManager.LayoutParams.WRAP_CONTENT);
- tiaozhuanDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.manhua_dialog_tiaozhuan, null));
這里還有一個地方需要注意一下,如果我們在show這個AlertDialog之前,需要設置該AlertDialog顯示的View中的EditText的內容,則我們應該這么去findViewById():
Java代碼
- EditText editText = (EditText)tiaozhuanDialog.findViewById(R.id.myEditText);
- editText.setText("Who are you ? I am android Developer ");
否 則會報ERROR/AndroidRuntime(1032): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.錯誤!!
1 1)更改AlertDialog窗口大小的方法:
2 AlertDialog dialog = new AlertDialog.Builder(this).create();
3 dialog.show();
4 WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
5 params.width = 200;
6 params.height = 200 ;
7 dialog.getWindow().setAttributes(params);
8
9 2)去除邊框
10 AlertDialog.setView(view,0,0,0,0);
- AlertDialog dialog = builder.setTitle("消息列表")
- .setView(layout)
- .create();
- dialog.show();
- //設置窗口的大小
- dialog.getWindow().setLayout(300, 200);
dialog.show();一定要放在dialog.getWindow().setLayout(300, 200);的前面,否則不起作用。
網上有一種方法是
- WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
- params.width = 300;
- params.height = 200;
- dialog.getWindow().setAttributes(params);
但是dialog.getWindow().setLayout(300, 200);實際上封裝了這個方法,setLayout()的源代碼如下:
- final WindowManager.LayoutParams attrs = getAttributes();
- attrs.width = width;
- attrs.height = height;
- if (mCallback != null) {
- mCallback.onWindowAttributesChanged(attrs);
- }
所以這兩個方法的作用本質上是一樣的,都是為AlertDialog設置大小