Android AlertDialog對(duì)話框詳解及實(shí)例
Android AlertDialog
關(guān)系圖如下:
Android主要提供四種對(duì)話框:
1:AlertDialog:功能最豐富,實(shí)際應(yīng)用最廣的對(duì)話框。
2:ProgressDialog:進(jìn)度條對(duì)話框
3:DatePickerDialog:日期選擇器對(duì)話框
4:TimePickerDialog:時(shí)間選擇器對(duì)話框
創(chuàng)建一個(gè)對(duì)話框的步驟:
AlertDialog.Builder builder = new AlertDialog.Builder(this) // 1:設(shè)置對(duì)話框標(biāo)題 .setTitle("自定義列表項(xiàng)對(duì)話框") // 2:設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 3:設(shè)置內(nèi)容 .setMessage("對(duì)話框的測試內(nèi)容\n第二行內(nèi)容"); // 為AlertDialog.Builder添加“確定”按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加“取消”按鈕 setNegativeButton(builder) .create() .show();
代碼區(qū):
main.xml代碼區(qū):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <!-- 顯示一個(gè)普通的文本編輯框組件 --> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false"/> <!-- 定義一個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="簡單對(duì)話框" android:onClick="simple" /> <!-- 定義一個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="簡單列表項(xiàng)對(duì)話框" android:onClick="simpleList" /> <!-- 定義一個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="單選列表項(xiàng)對(duì)話框" android:onClick="singleChoice" /> <!-- 定義一個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多選列表項(xiàng)對(duì)話框" android:onClick="multiChoice" /> <!-- 定義一個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義列表項(xiàng)對(duì)話框" android:onClick="customList" /> <!-- 定義一個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義View對(duì)話框" android:onClick="customView" /> </LinearLayout>
Activity代碼區(qū):
public class MainActivity extends Activity { TextView show; String[] items = new String[] { "aserbao", "Android", " Java", "IOS" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); show = (TextView) findViewById(R.id.show); } public void simple(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對(duì)話框標(biāo)題 .setTitle("簡單對(duì)話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) .setMessage("對(duì)話框的測試內(nèi)容\n第二行內(nèi)容"); // 為AlertDialog.Builder添加“確定”按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加“取消”按鈕 setNegativeButton(builder) .create() .show(); } public void simpleList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對(duì)話框標(biāo)題 .setTitle("簡單列表對(duì)話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置簡單的列表項(xiàng)內(nèi)容 .setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你選中了《" + items[which] + "》"); } }); // 為AlertDialog.Builder添加“確定”按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加“取消”按鈕 setNegativeButton(builder) .create() .show(); } public void singleChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對(duì)話框標(biāo)題 .setTitle("單選列表項(xiàng)對(duì)話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置單選列表項(xiàng),默認(rèn)選中第二項(xiàng)(索引為1) .setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你選中了《" + items[which] + "》"); } }); // 為AlertDialog.Builder添加“確定”按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加“取消”按鈕 setNegativeButton(builder) .create() .show(); } public void multiChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對(duì)話框標(biāo)題 .setTitle("多選列表項(xiàng)對(duì)話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置多選列表項(xiàng),設(shè)置勾選第2項(xiàng)、第4項(xiàng) .setMultiChoiceItems(items , new boolean[]{false , true ,false ,true}, null); // 為AlertDialog.Builder添加“確定”按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加“取消”按鈕 setNegativeButton(builder) .create() .show(); } public void customList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對(duì)話框標(biāo)題 .setTitle("自定義列表項(xiàng)對(duì)話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置自定義列表項(xiàng) .setAdapter(new ArrayAdapter<String>(this , R.layout.array_item , items), null); // 為AlertDialog.Builder添加“確定”按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加“取消”按鈕 setNegativeButton(builder) .create() .show(); } public void customView(View source) { // 裝載app\src\main\res\layout\login.xml界面布局文件 TableLayout loginForm = (TableLayout)getLayoutInflater() .inflate( R.layout.login, null); new AlertDialog.Builder(this) // 設(shè)置對(duì)話框的圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置對(duì)話框的標(biāo)題 .setTitle("自定義View對(duì)話框") // 設(shè)置對(duì)話框顯示的View對(duì)象 .setView(loginForm) // 為對(duì)話框設(shè)置一個(gè)“確定”按鈕 .setPositiveButton("登錄", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 此處可執(zhí)行登錄處理 } }) // 為對(duì)話框設(shè)置一個(gè)“取消”按鈕 .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消登錄,不做任何事情 } }) // 創(chuàng)建并顯示對(duì)話框 .create() .show(); } private AlertDialog.Builder setPositiveButton( AlertDialog.Builder builder) { // 調(diào)用setPositiveButton方法添加“確定”按鈕 return builder.setPositiveButton("確定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("單擊了【確定】按鈕!"); } }); } private AlertDialog.Builder setNegativeButton( AlertDialog.Builder builder) { // 調(diào)用setNegativeButton方法添加“取消”按鈕 return builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("單擊了【取消】按鈕!"); } }); } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問題
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- 簡析Android多種AlertDialog對(duì)話框效果
- Android AlertDialog對(duì)話框用法示例
- Android AlertDialog實(shí)現(xiàn)分享對(duì)話框/退出對(duì)話框/下載對(duì)話框
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- Android Alertdialog(實(shí)現(xiàn)警告對(duì)話框)
- Android常用的AlertDialog對(duì)話框及自定義對(duì)話框
- Android使用AlertDialog實(shí)現(xiàn)對(duì)話框
- Android編程實(shí)現(xiàn)AlertDialog自定義彈出對(duì)話框的方法示例
相關(guān)文章
Android SharedPreferences四種操作模式使用詳解
這篇文章主要介紹了Android SharedPreferences四種操作模式使用詳解的相關(guān)資料,這里介紹了獲取Android SharedPreferences的兩種方法及比較,和操作模式的介紹,需要的朋友可以參考下2017-07-07Android Loop機(jī)制中Looper與handler詳細(xì)分析
Handler是Android線程之間的消息機(jī)制,主要的作用是將一個(gè)任務(wù)切換到指定的線程中去執(zhí)行,準(zhǔn)確的說是切換到構(gòu)成Handler的looper所在的線程中去出處理。本文將詳細(xì)介紹Android Handler機(jī)制和Looper Handler Message關(guān)系2022-11-11Spinner在Dialog中的使用效果實(shí)例代碼詳解
這篇文章主要介紹了Spinner在Dialog中的使用效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小
本文主要介紹了Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小以適應(yīng)TextView的大小的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03Android學(xué)習(xí)筆記——Menu介紹(三)
今天繼續(xù)昨天沒有講完的Menu的學(xué)習(xí),主要是Popup Menu的學(xué)習(xí),需要的朋友可以參考下2014-10-10Android編程實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)帶有單選按鈕的dialog對(duì)話框及帶有復(fù)選按鈕的dialog對(duì)話框相關(guān)操作技巧,需要的朋友可以參考下2017-09-09Android如何禁止向EditText控件中輸入內(nèi)容詳解
EditText是接受用戶輸入信息的最重要控件。下面這篇文章主要給大家介紹了關(guān)于Android如何禁止向EditText控件中輸入內(nèi)容的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09