- Android 5從入門到精通
- 李波
- 1076字
- 2021-03-19 15:29:03
4.7 對話框(Dialog)
對話框是人機交互過程中十分常見的組件,一般用于在特定條件下對用戶顯示一些信息,可以增強應用的友好性。
Dialog類是對話框的基類。對話框雖然可以在界面上顯示,但是Dialog不是View類的子類,而是直接繼承自java.lang.Object類。Dialog對象也有自己的生命周期,其生命周期由創建它的Activity進行管理。Activity可以調用showDialog(int id)將不同id的對話框顯示出來,也可以調用dismissDialog(int id)方法將id標識的對話框從用戶界面中關閉掉。當Activity調用了showDialog(id)方法,對應id的對話框沒有被創建,Android系統會回調OnCreateDialog(id)方法來創建具有該id的對話框。在Activity中創建的對話框都會被Activity保存,下次showDialog(id)方法被調用時,若該id對話框已經不再創建,則系統不會再次調用OnCreateDialog(id)方法創建該對話框,而是會回調onPrepareDialog(int id, Dialog dialog)方法,該方法允許對話框在被顯示之前做一些修改。
常用的對話框有AlertDialog和ProgressDialog,本節將通過實例講解這兩種對話框的使用方法。
4.7.1 AlertDialog
AlertDialog對話框是十分常用的用于顯示信息的方式,最多可提供三個按鈕。AlertDialog不能直接通過構造方法構建,而要由AlertDialog.Builder類來創建。AlertDialog對話框的標題、按鈕以及按鈕要響應的事件也由AlertDialog.Builder設置。
在使用AlertDialog. Builder創建對話框時常用的幾個方法如下:
●setTitle():設置對話框設置標題。
●setIcon():設置對話框設置圖標。
●setMessage():設置對話框的提示信息。
●setPositiveButton():對話框添加yes按鈕。
●setNegativeButton():對話框添加no按鈕。
●setNeutralButton():為對話框添加第三個按鈕。
下面通過實例來學習創建AlertDialog的方法。
創建Eclipse Android工程DialogDemo,并在main.xml中添加兩個按鈕,分別為AlertDialog和ProcessDialog。
其main.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Dialog演示" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="AlertDialog" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ProgressDialog" /> </LinearLayout>
其運行效果如圖4.41所示。

圖4.41 AlertDialog的運行效果
處理AlertDialog按鈕單擊事件的代碼為:
btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ // TODO Auto-generated method stub showDialog(ALERT_DLG); } });
單擊AlertDialog按鈕,調用showDialog(ALERT_DLG),系統回調onCreateDialog(int id)方法,創建并彈出AlertDialog對話框,如圖4.42所示。

圖4.42 單擊AlertDialog按鈕的效果
相關代碼為:
protected Dialog onCreateDialog(int id){ // TODO Auto-generated method stub Dialog dialog=null; switch(id){ case ALERT_DLG: AlertDialog.Builder builder=new AlertDialog.Builder(DialogDemoActivity.this); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("AlertDialog"); builder.setMessage("這是一個AlertDialog"); builder.setPositiveButton("Positive",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub Log.i("DialogDemo","OK按鈕被單擊!"); } }); builder.setNegativeButton("Negative",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub Log.i("DialogDemo","Cancel按鈕被單擊!"); } }); builder.setNeutralButton("Neutral",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub Log.i("DialogDemo","Neutral按鈕被單擊!"); } }); dialog=builder.create(); break; default: break; } return dialog; }
onCreateDialog()方法中創建了有三個按鈕的AlertDialog,并且為每個按鈕添加了事件處理方法,以便獲知用戶單擊了哪個按鈕。
4.7.2 ProgressDialog
ProgressDialog是一個帶有進度條的對話框,當應用程序在完成比較耗時的工作時,使用該對話框可以為用戶提供一個總進度上的提示。
為main.xml布局中的ProgressDialog按鈕添加事件處理代碼:
progressbtn=(Button)findViewById(R.id.button2); progressbtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ // TODO Auto-generated method stub showDialog(PROGRESS_DLG); } });
單擊ProgressDialog按鈕,調用showDialog(PROGRESS_DLG),系統回調onCreateDialog(int id)方法,創建并彈出ProgressDialog對話框,如圖4.43所示。

圖4.43 單擊ProgressDialog按鈕的效果
onCreateDialog()方法中的相關代碼如下:
case PROGRESS_DLG: progressDialog=new ProgressDialog(this); //設置水平進度條 progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); //設置進度條最大值為100 progressDialog.setMax(100); //設置進度條當前值為0 progressDialog.setProgress(0); dialog=progressDialog; new Thread(new Runnable(){ int count=0; @Override public void run(){ // TODO Auto-generated method stub while(progressDialog.getProgress()<100){ count+=3; progressDialog.setProgress(count); try { Thread.sleep(1000); } catch(InterruptedException e){ // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); break;
- 微信公眾平臺與小程序開發:從零搭建整套系統
- 軟件架構設計:大型網站技術架構與業務架構融合之道
- Python 深度學習
- C語言程序設計(第2版)
- 三維圖形化C++趣味編程
- Apache Spark 2 for Beginners
- Programming ArcGIS 10.1 with Python Cookbook
- 區塊鏈:以太坊DApp開發實戰
- Scientific Computing with Scala
- Learning OpenStack Networking(Neutron)
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Java程序員面試筆試寶典(第2版)
- Fast Data Processing with Spark(Second Edition)
- Scala編程實戰
- Struts 2.x權威指南