- Android 5從入門到精通
- 李波
- 1076字
- 2021-03-19 15:29:03
4.7 對話框(Dialog)
對話框是人機(jī)交互過程中十分常見的組件,一般用于在特定條件下對用戶顯示一些信息,可以增強(qiáng)應(yīng)用的友好性。
Dialog類是對話框的基類。對話框雖然可以在界面上顯示,但是Dialog不是View類的子類,而是直接繼承自java.lang.Object類。Dialog對象也有自己的生命周期,其生命周期由創(chuàng)建它的Activity進(jìn)行管理。Activity可以調(diào)用showDialog(int id)將不同id的對話框顯示出來,也可以調(diào)用dismissDialog(int id)方法將id標(biāo)識的對話框從用戶界面中關(guān)閉掉。當(dāng)Activity調(diào)用了showDialog(id)方法,對應(yīng)id的對話框沒有被創(chuàng)建,Android系統(tǒng)會回調(diào)OnCreateDialog(id)方法來創(chuàng)建具有該id的對話框。在Activity中創(chuàng)建的對話框都會被Activity保存,下次showDialog(id)方法被調(diào)用時,若該id對話框已經(jīng)不再創(chuàng)建,則系統(tǒng)不會再次調(diào)用OnCreateDialog(id)方法創(chuàng)建該對話框,而是會回調(diào)onPrepareDialog(int id, Dialog dialog)方法,該方法允許對話框在被顯示之前做一些修改。
常用的對話框有AlertDialog和ProgressDialog,本節(jié)將通過實例講解這兩種對話框的使用方法。
4.7.1 AlertDialog
AlertDialog對話框是十分常用的用于顯示信息的方式,最多可提供三個按鈕。AlertDialog不能直接通過構(gòu)造方法構(gòu)建,而要由AlertDialog.Builder類來創(chuàng)建。AlertDialog對話框的標(biāo)題、按鈕以及按鈕要響應(yīng)的事件也由AlertDialog.Builder設(shè)置。
在使用AlertDialog. Builder創(chuàng)建對話框時常用的幾個方法如下:
●setTitle():設(shè)置對話框設(shè)置標(biāo)題。
●setIcon():設(shè)置對話框設(shè)置圖標(biāo)。
●setMessage():設(shè)置對話框的提示信息。
●setPositiveButton():對話框添加yes按鈕。
●setNegativeButton():對話框添加no按鈕。
●setNeutralButton():為對話框添加第三個按鈕。
下面通過實例來學(xué)習(xí)創(chuàng)建AlertDialog的方法。
創(chuàng)建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>
其運(yùn)行效果如圖4.41所示。

圖4.41 AlertDialog的運(yùn)行效果
處理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按鈕,調(diào)用showDialog(ALERT_DLG),系統(tǒng)回調(diào)onCreateDialog(int id)方法,創(chuàng)建并彈出AlertDialog對話框,如圖4.42所示。

圖4.42 單擊AlertDialog按鈕的效果
相關(guān)代碼為:
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()方法中創(chuàng)建了有三個按鈕的AlertDialog,并且為每個按鈕添加了事件處理方法,以便獲知用戶單擊了哪個按鈕。
4.7.2 ProgressDialog
ProgressDialog是一個帶有進(jìn)度條的對話框,當(dāng)應(yīng)用程序在完成比較耗時的工作時,使用該對話框可以為用戶提供一個總進(jìn)度上的提示。
為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按鈕,調(diào)用showDialog(PROGRESS_DLG),系統(tǒng)回調(diào)onCreateDialog(int id)方法,創(chuàng)建并彈出ProgressDialog對話框,如圖4.43所示。

圖4.43 單擊ProgressDialog按鈕的效果
onCreateDialog()方法中的相關(guān)代碼如下:
case PROGRESS_DLG: progressDialog=new ProgressDialog(this); //設(shè)置水平進(jìn)度條 progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL); //設(shè)置進(jìn)度條最大值為100 progressDialog.setMax(100); //設(shè)置進(jìn)度條當(dāng)前值為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;
- 移動UI設(shè)計(微課版)
- Beginning C++ Game Programming
- 數(shù)據(jù)結(jié)構(gòu)簡明教程(第2版)微課版
- Django:Web Development with Python
- Bootstrap 4:Responsive Web Design
- Python機(jī)器學(xué)習(xí)算法與實戰(zhàn)
- Building an RPG with Unity 2018
- FPGA Verilog開發(fā)實戰(zhàn)指南:基于Intel Cyclone IV(進(jìn)階篇)
- 動手學(xué)數(shù)據(jù)結(jié)構(gòu)與算法
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- Web前端應(yīng)用開發(fā)技術(shù)
- Canvas Cookbook
- Maven for Eclipse
- Spring Boot從入門到實戰(zhàn)
- 從零開始學(xué)算法:基于Python