Android Dialog 對(duì)話框詳解及示例代碼
Android Dialog 對(duì)話框
1、Dialog介紹
2、AlertDialog的基本使用
3、自定義對(duì)話框 Custom Dialog
一、Dialog介紹
Dialog也是Android中常用的用戶界面元素,他同Menu一樣也不是View的子類。讓我們看一下它的繼承關(guān)系:
這里要留意一下他的直接子類 AlertDialog,和間接子類 DatePickerDialog,ProgressDialog,TimePickerDialog,其中后三個(gè)我們?cè)谇懊娴恼鹿?jié)已經(jīng)講過(guò),今天我們把重點(diǎn)放在AlertDialog上。
二、AlertDialog的基本使用
AlertDialog對(duì)話框是Dialog的子類,它提供一個(gè)圖標(biāo),一個(gè)標(biāo)題,一個(gè)文本和3個(gè)按鈕。我們?cè)贏ctivity里可以自行創(chuàng)建和顯示Dialog,也可以通過(guò)Activity的方法對(duì)其進(jìn)行管理。我們可以通過(guò)下面的例子學(xué)習(xí)它的使用方法,同樣請(qǐng)注意代碼中的注釋。
1、創(chuàng)建一個(gè)項(xiàng)目 Lesson17_HelloAlertDialog,Activity的文件名叫 MainHelloAlertDialog.java
2、res/layout/main.xml 的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01" android:text="對(duì)話框示例" android:textsize="20sp" android:layout_margintop="5dp"> </textview> <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button01" android:text="顯示對(duì)話框|ShowDialog()" android:textsize="20sp" android:layout_margintop="5dp"> </button> <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button02" android:text="關(guān)閉對(duì)話框|dismissDialog()" android:textsize="20sp" android:layout_margintop="5dp"> </button> <button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button03" android:text="移除對(duì)話框|removeDialog()" android:textsize="20sp" android:layout_margintop="5dp"> </button> </linearlayout>
3、MainHelloAlertDialog.java的內(nèi)容如下:
package android.basic.lesson17; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainHelloAlertDialog extends Activity { //定義一個(gè)對(duì)話框的ID int Edward_Movie_Dialog = 1; // 對(duì)話框按鈕點(diǎn)擊事件監(jiān)聽(tīng)器 OnClickListener ocl = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: Toast.makeText(MainHelloAlertDialog.this, "我不喜歡他的電影。", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_NEUTRAL: Toast.makeText(MainHelloAlertDialog.this, "說(shuō)不上喜歡不喜歡。", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_POSITIVE: Toast.makeText(MainHelloAlertDialog.this, "我很喜歡他的電影。", Toast.LENGTH_LONG).show(); break; } } }; @Override /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 定義對(duì)話框?qū)ο? Dialog dialog = new AlertDialog.Builder(this) .setIcon(android.R.drawable.btn_star).setTitle("喜好調(diào)查") .setMessage("你喜歡看愛(ài)德華.諾頓Edward Norton的電影嗎?") .setNegativeButton("不喜歡", ocl).setNeutralButton("一般般", ocl) .setPositiveButton("很喜歡", ocl).create(); //顯示對(duì)話框 dialog.show(); //定義 按鈕 UI組件 Button b1 = (Button) findViewById(R.id.Button01); Button b2 = (Button) findViewById(R.id.Button02); Button b3 = (Button) findViewById(R.id.Button03); //定義按鈕的單擊事件監(jiān)聽(tīng)器 View.OnClickListener b_ocl= new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()){ case R.id.Button01: //顯示對(duì)話框 showDialog(Edward_Movie_Dialog); break; case R.id.Button02: //關(guān)閉對(duì)話框 這個(gè)功能好傻X,根本點(diǎn)不到的按鈕 dismissDialog(Edward_Movie_Dialog); break; case R.id.Button03: //移除對(duì)話框 這個(gè)功能好傻X,根本點(diǎn)不到的按鈕 removeDialog(Edward_Movie_Dialog); break; } } }; //綁定按鈕監(jiān)聽(tīng)器 b1.setOnClickListener(b_ocl); b2.setOnClickListener(b_ocl); b3.setOnClickListener(b_ocl); } // 創(chuàng)建會(huì)話框時(shí)調(diào)用 @Override public Dialog onCreateDialog(int id) { Toast.makeText(this, "onCreateDialog方法被調(diào)用", Toast.LENGTH_LONG).show(); return new AlertDialog.Builder(this) .setIcon(android.R.drawable.btn_star).setTitle("喜好調(diào)查") .setMessage("你喜歡看愛(ài)德華.諾頓Edward Norton的電影嗎?") .setNegativeButton("不喜歡", ocl).setNeutralButton("一般般", ocl) .setPositiveButton("很喜歡", ocl).create(); } // 每次顯示對(duì)話框之前會(huì)被調(diào)用 @Override public void onPrepareDialog(int id, Dialog dialog){ Toast.makeText(this, "onPrepareDialog方法被調(diào)用", Toast.LENGTH_LONG).show(); super.onPrepareDialog(id, dialog); } }
4、運(yùn)行結(jié)果如下:
有興趣的同學(xué)可以考慮一下如何改進(jìn)關(guān)閉和移除對(duì)話框按鈕。
以上就是對(duì)Android Dialog 資料的整理,后續(xù)繼續(xù)補(bǔ)充,謝謝大家對(duì)本站的支持!
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- 8種android 對(duì)話框(Dialog)使用方法詳解
- 屬于自己的Android對(duì)話框(Dialog)自定義集合
- Android中制作自定義dialog對(duì)話框的實(shí)例分享
- Android Dialog對(duì)話框詳解
- Android 對(duì)話框 Dialog使用實(shí)例講解
- Android Dialog對(duì)話框用法實(shí)例詳解
- Android 對(duì)話框(Dialog)大全示例(建立你自己的對(duì)話框)
- Android中Dialog對(duì)話框的使用小結(jié)
相關(guān)文章
Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Android 接收微信、QQ其他應(yīng)用打開(kāi)第三方分享功能
這篇文章主要介紹了Android 接收微信、QQ其他應(yīng)用打開(kāi),第三方分享 ,思路很簡(jiǎn)單通過(guò)在AndroidManifest.xml注冊(cè)ACTION事件,在用于接收分享的Activity里面加接收代碼,感興趣的朋友可以一起學(xué)習(xí)下2022-11-11Android利用LitePal操作數(shù)據(jù)庫(kù)存取圖片
這篇文章主要為大家詳細(xì)介紹了Android利用LitePal操作數(shù)據(jù)庫(kù)存取圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08源碼淺析Android中內(nèi)存泄漏檢測(cè)工具Leakcanary的使用
大名鼎鼎的 Leakcanary 想必作為 Android 開(kāi)發(fā)都多多少少接觸過(guò),新版本的 Leakcanary 也用 Kotlin 重寫了一遍,最近詳細(xì)查看了下源碼,就來(lái)和大家簡(jiǎn)單分享一下2023-04-04Android 解決WebView多進(jìn)程崩潰的方法
這篇文章主要介紹了Android 解決WebView多進(jìn)程崩潰的方法,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-03-03詳解Xamarin.Android 利用Fragment實(shí)現(xiàn)底部菜單
這篇文章主要介紹了詳解Xamarin.Android 利用Fragment實(shí)現(xiàn)底部菜單,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Android編程開(kāi)發(fā)中的正則匹配操作示例
這篇文章主要介紹了Android編程開(kāi)發(fā)中的正則匹配操作,結(jié)合具體實(shí)例形式分析了Android針對(duì)手機(jī)號(hào)、郵箱及IP的正則匹配操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06Android ListView position詳解及實(shí)例代碼
這篇文章主要介紹了Android ListView position的相關(guān)資料,在開(kāi)發(fā)Android 應(yīng)用的時(shí)候你真的用對(duì)了嗎?這里給大家徹底解釋下,需要的朋友可以參考下2016-10-10RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07