當前位置:首頁 » 安卓系統 » android對話

android對話

發布時間: 2025-08-09 19:01:02

❶ android中的對話框怎麼寫

Activities提供了一種方便管理的創建、保存、回復的對話框機制,例如onCreateDialog(int),onPrepareDialog(int,Dialog),showDialog(int),dismissDialog(int)等方法,如果使用這些方法的話,Activity將通過getOwnerActivity()方法返回該Activity管理的對話框(dialog).

onCreateDialog(int):當你使用這個回調函數時,Android系統會有效的設置這個Activity為每個對話框的所有者,從而自動管理每個對話框的狀態並掛靠到Activity上。這樣,每個對話框繼承這個Activity的特定屬性。比如,當一個對話框打開時,菜單鍵顯示為這個Activity定義的選項菜單,音量鍵修改Activity使用的音頻流。

showDialog(int):當你想要顯示一個對話框時,調用showDialog(intid)方法並傳遞一個唯一標識這個對話框的整數。當對話框第一次被請求時,Android從你的Activity中調用onCreateDialog(intid),你應該在這里初始化這個對話框Dialog。這個回調方法被傳以和showDialog(intid)相同的ID。當你創建這個對話框後,在Activity的最後返回這個對象。

onPrepareDialog(int,Dialog):在對話框被顯示之前,Android還調用了可選的回調函數onPrepareDialog(intid,Dialog).如果你想在每一次對話框被打開時改變它的任何屬性,你可以定義這個方法。這個方法在每次打開對話框時被調用,而onCreateDialog(int)僅在對話框第一次打開時被調用。如果你不定義onPrepareDialog(),那麼這個對話框將保持和上次打開時一樣。這個方法也被傳遞以對話框的ID,和在onCreateDialog()中創建的對話框對象。

dismissDialog(int):當你准備關閉對話框時,你可以通過對這個對話框調用dismiss()來消除它。如果需要,你還可以從這個Activity中調用dismissDialog(intid)方法,這實際上將為你對這個對話框調用dismiss()方法。如果你想使用onCreateDialog(intid)方法來管理你對話框的狀態(就如同在前面的章節討論的那樣),然後每次你的對話框消除的時候,這個對話框對象的狀態將由該Activity保留。如果你決定不再需要這個對象或者清除該狀態是重要的,那麼你應該調用removeDialog(intid)。這將刪除任何內部對象引用而且如果這個對話框正在顯示,它將被消除。

下面是幾種對話框的效果

圖一:

圖1效果:該效果是當按返回按鈕時彈出一個提示,來確保無誤操作,採用常見的對話框樣式。


代碼:
創建對話框方法dialog()

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;
}

圖2效果:改變了對話框的圖表,添加了三個按鈕

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();

圖3效果:信息內容是一個簡單的View類型

new AlertDialog.Builder(this).setTitle("請輸入").setIcon(
android.R.drawable.ic_dialog_info).setView(
new EditText(this)).setPositiveButton("確定", null)
.setNegativeButton("取消", null).show();

圖4效果:信息內容是一組單選框

new AlertDialog.Builder(this).setTitle("復選框").setMultiChoiceItems(
new String[] { "Item1", "Item2" }, null, null)
.setPositiveButton("確定", null)
.setNegativeButton("取消", null).show();

圖5效果:信息內容是一組多選框

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();

圖6效果:信息內容是一組簡單列表項

new AlertDialog.Builder(this).setTitle("列表框").setItems(
new String[] { "Item1", "Item2" }, null).setNegativeButton(
"確定", null).show();

圖7效果:信息內容是一個自定義的布局

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="horizontal"
android:id="@+id/dialog">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tvname" android:text="姓名:" />
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/></LinearLayout>

2.調用代碼

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();

❷ Android的對話框怎麼監聽觸屏事件

用我這個, 我己經給你寫好了,你可以稍做修改就能用,
title是對話框的標題
icon是resID,是一張圖片的ID,放在你res目錄下的drawable
okcmd和cancelcmd分別是確認和取消按鈕的字元串

//確認對話框的呼出
public void showDialog(String title,int icon,String msg,String okcmd,String cancelCmd){
if(okcmd==null){return;}
Log.i("setDialog", "dialogTitle="+title
+" dialogContent"+msg+
" dialogCmdOk"+okcmd
+" dialogCmdCancel"+cancelCmd
+" dialogIcon"+icon);

Builder builder=new AlertDialog.Builder(this);
builder.setTitle(title);
if(icon>0){
builder.setIcon(icon);
}else{
builder.setIcon(R.drawable.icon);
}
builder.setMessage(msg);
builder.setPositiveButton(okcmd,
new
android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int i) {
Log.i("showDialog", "onClick");
}
});
if(cancelCmd!=null && cancelCmd.length()>0){
builder.setNeutralButton(cancelCmd,
new android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int i) { }
});
}

AlertDialog dialog =builder.create();
dialog.show();
}

❸ Android 寮鍙 瀵硅瘽妗咲ialog dismiss鍜宧ide鏂規硶鐨勫尯鍒

  1. cancel浼氬幓璋僤ismiss鐨勶紝濡傛灉璋冪敤鐨刢ancel鐨勮瘽灝卞彲浠ョ洃鍚珼ialogInterface.OnCancelListener 錛屽備笅


❹ android如何創建帶3個按鈕的對話框

1.先在布局界面上,拖進來一個按鈕控制項,並設置顯示的文字,記得保存(Ctrl+S)

之後在代碼界面上定義該按鈕。

2.新建一個按鈕點擊的方法。
onClick(View v) :點擊之後的動作。

3.設置按鈕的點擊事件指向我們新建的點擊方法。
setOnClickListener:設置點擊之後觸發的動作。

4.在onClick里添加彈出對話框的代碼。
AlertDialog:一個對話框類。
MainActivity.this:對話框顯示的位置。
setTitle:設置標題。
setMessage:設置內容。
setPositiveButton:設置對話框的按鈕。
show():顯示對話框。

至此所有代碼已經完成,編譯並生成,在Android安卓虛擬機上顯示如下。

❺ android自定義的對話框怎麼調用

Android自定義對話框的思路就是編寫對話框的布局文件xml,然後在對話框中顯示不同的控制項。以下以顯示文本控制項為例(ImageView等都可以顯示)。
1.布局文件connect_dlg.xml(比如http://www.tiecou.com/)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#ffffffff"
android:orientation="vertical"
android:id="@+id/llToast" >
<TextView
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:textColor="#ffffffff"
android:layout_width="fill_parent"
android:gravity="center"
android:textSize="16sp"
android:background="#FF129de2"
android:id="@+id/tvTitleToast" />
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llToastContent"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:layout_width="wrap_content"
android:padding="15dip"
android:background="#FFFFFFFF" >
<TextView
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:layout_width="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#FFff6699"
android:id="@+id/tvTextToast" />
</LinearLayout>
<LinearLayout
android:id="@+id/MyLayout_ad2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="40sp">
<com.tencent.exmobwin.banner.TAdView
android:id="@+id/adview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|right" >
</com.tencent.exmobwin.banner.TAdView>
</LinearLayout>
</LinearLayout>
2.編寫顯示對話框函數。ShowConnectDialog(String textString)
private void ShowConnectDialog(String textString) {
LinearLayout loginLayout1 = (LinearLayout) getLayoutInflater().inflate(
R.layout.connect_dlg, null);
// adView.
TextView title = (TextView) loginLayout1
.findViewById(R.id.tvTitleToast);
title.setText("系統提示");
TextView text1 = (TextView) loginLayout1.findViewById(R.id.tvTextToast);
text1.setText(textString);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(loginLayout1);
builder.setPositiveButton("下載MobCtrl伺服器?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//處理確定按鈕
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 處理取消按鈕
finish();
}
});
builder.create().show();
}
3.顯示對話框。在需要顯示的地方調用即可。
ShowConnectDialog("連接超時,請檢查伺服器是否開啟及IP地址是否輸入正確。確保電腦和手機連接在同一個網路內。");

熱點內容
編譯安裝boost 發布:2025-08-31 01:45:04 瀏覽:36
sql2008數據還原 發布:2025-08-31 01:45:00 瀏覽:889
電視廣告腳本範文 發布:2025-08-31 01:33:10 瀏覽:453
p2p網貸系統源碼下載 發布:2025-08-31 01:10:34 瀏覽:905
在線聊天室源碼 發布:2025-08-31 01:10:33 瀏覽:62
python入門知乎 發布:2025-08-31 00:58:06 瀏覽:202
浪潮全快閃記憶體儲 發布:2025-08-31 00:56:35 瀏覽:741
外部存儲分析 發布:2025-08-31 00:49:36 瀏覽:252
2棟3單元會用什麼wifi密碼 發布:2025-08-31 00:47:45 瀏覽:329
線切割機床編程 發布:2025-08-31 00:42:31 瀏覽:859