編輯:關於Android編程
本文實例講述了Android Dialog對話框用法。分享給大家供大家參考,具體如下:
Activities提供了一種方便管理的創建、保存、回復的對話框機制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int),dismissDialog(int)等方法,如果使用這些方法的話,Activity將通過getOwnerActivity()方法返回該Activity管理的對話框(dialog).
1. onCreateDialog(int):當你使用這個回調函數時,Android系統會有效的設置這個Activity為每個對話框的所有者,從而自動管理每個對話框的狀態並掛靠到Activity上。這樣,每個對話框繼承這個Activity的特定屬性。比如,當一個對話框打開時,菜單鍵顯示為這個Activity定義的選項菜單,音量鍵修改Activity使用的音頻流。
2. showDialog(int): 當你想要顯示一個對話框時,調用showDialog(int id) 方法並傳遞一個唯一標識這個對話框的整數。當對話框第一次被請求時,Android從你的Activity中調用onCreateDialog(int id),你應該在這裡初始化這個對話框Dialog。這個回調方法被傳以和showDialog(int id)相同的ID。當你創建這個對話框後,在Activity的最後返回這個對象。
3. onPrepareDialog(int, Dialog):在對話框被顯示之前,Android還調用了可選的回調函數onPrepareDialog(int id, Dialog). 如果你想在每一次對話框被打開時改變它的任何屬性,你可以定義這個方法。這個方法在每次打開對話框時被調用,而onCreateDialog(int) 僅在對話框第一次打開時被調用。如果你不定義onPrepareDialog(),那麼這個對話框將保持和上次打開時一樣。這個方法也被傳遞以對話框的ID,和在onCreateDialog()中創建的對話框對象。
4. dismissDialog(int):當你准備關閉對話框時,你可以通過對這個對話框調用dismiss()來消除它。如果需要,你還可以從這個Activity中調用dismissDialog(int id) 方法,這實際上將為你對這個對話框調用dismiss() 方法。 如果你想使用onCreateDialog(int id) 方法來管理你對話框的狀態(就如同在前面的章節討論的那樣),然後每次你的對話框消除的時候,這個對話框對象的狀態將由該Activity保留。如果你決定不再需要這個對象或者清除該狀態是重要的,那麼你應該調用removeDialog(int id)。這將刪除任何內部對象引用而且如果這個對話框正在顯示,它將被消除。
使用方法一:當按返回按鈕時彈出一個提示,來確保無誤操作,采用常見的對話框樣式
protected void dialog() {
AlertDialog.Builder builder = new Builder(Main.this);
builder.setMessage("確認退出嗎?");
builder.setTitle("提示");
builder.setPositiveButton("確認", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Main.this.finish();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
在onKeyDown(int keyCode, KeyEvent event)方法中調用此方法
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
dialog();
}
return false;
}
使用方法二:改變對話框的圖表,添加了三個按鈕
Dialog dialog = new AlertDialog.Builder(this)
.setIcon( android.R.drawable.btn_star)
.setTitle("喜好調查")
.setMessage("你喜歡李連傑的電影嗎?")
.setPositiveButton("很喜歡",new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(Main.this, "我很喜歡他的電影。",Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("不喜歡", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(Main.this, "我不喜歡他的電影。", Toast.LENGTH_LONG).show();
}
})
.setNeutralButton("一般", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(Main.this, "談不上喜歡不喜歡。", Toast.LENGTH_LONG).show();
}
})
.create();
dialog.show();
使用方法三:信息內容是一個簡單的View類型
new AlertDialog.Builder(this)
.setTitle("請輸入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(new EditText(this))
.setPositiveButton("確定", null)
.setNegativeButton("取消", null)
.show();
使用方法四:信息內容是一組單選框
new AlertDialog.Builder(this)
.setTitle("復選框")
.setMultiChoiceItems(new String[] { "Item1", "Item2" }, null, null)
.setPositiveButton("確定", null)
.setNegativeButton("取消", null)
.show();
使用方法五:信息內容是一組多選框
new AlertDialog.Builder(this)
.setTitle("單選框")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[] { "Item1", "Item2" }, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNegativeButton("取消", null).show();
使用方法六:信息內容是一組簡單列表項
new AlertDialog.Builder(this)
.setTitle("列表框")
.setItems(new String[] { "Item1", "Item2" }, null)
.setNegativeButton("確定", null)
.show();
使用方法七:信息內容是一個自定義的布局
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:" />
<EditText
android:id="@+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dip" />
</LinearLayout>
調用代碼:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog,(ViewGroup) findViewById(R.id.dialog));
new AlertDialog.Builder(this)
.setTitle("自定義布局")
.setView(layout)
.setPositiveButton("確定", null)
.setNegativeButton("取消", null)
.show();
使用方法七:fragment中的使用方法,只顯示確認按鈕
new AlertDialog.Builder(getActivity())
.setMessage(m)
.setPositiveButton("確定",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface, int i){}
})
.show();
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android視圖View技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
android中wifi原理及流程分析
wifi相關的文件位置:WIFI Settings應用程序位於packages/apps/Settings/src/com/android/settings/wifi/J
手機qq不顯示圖標怎麼回事 手機qq不顯示圖標怎麼辦
安卓系統手機的通知欄可以顯示QQ圖標,但沒有退出QQ的狀態下,手機qq不顯示圖標怎麼回事?手機qq不顯示圖標怎麼辦?下面我們就一起來看看吧!手機qq不顯示圖
[HyBrid]HyBrid混編初嘗:原生和第三方JsBridge的使用
最近研究HyBrid的兩種方式:一、直接原生WebView1)初始化WebView: //啟動javascript webView = (WebView)
Android 多種簡單的彈出框樣式設置代碼
簡介這是一個基於AlertDialog和Dialog這兩個類封裝的多種彈出框樣式,其中提供各種簡單樣式的彈出框使用說明。同時也可自定義彈出框。項目地址:http://ww