官术网_书友最值得收藏!

4.7 對(duì)話框(Dialog)

對(duì)話框是人機(jī)交互過程中十分常見的組件,一般用于在特定條件下對(duì)用戶顯示一些信息,可以增強(qiáng)應(yīng)用的友好性。

Dialog類是對(duì)話框的基類。對(duì)話框雖然可以在界面上顯示,但是Dialog不是View類的子類,而是直接繼承自java.lang.Object類。Dialog對(duì)象也有自己的生命周期,其生命周期由創(chuàng)建它的Activity進(jìn)行管理。Activity可以調(diào)用showDialog(int id)將不同id的對(duì)話框顯示出來,也可以調(diào)用dismissDialog(int id)方法將id標(biāo)識(shí)的對(duì)話框從用戶界面中關(guān)閉掉。當(dāng)Activity調(diào)用了showDialog(id)方法,對(duì)應(yīng)id的對(duì)話框沒有被創(chuàng)建,Android系統(tǒng)會(huì)回調(diào)OnCreateDialog(id)方法來創(chuàng)建具有該id的對(duì)話框。在Activity中創(chuàng)建的對(duì)話框都會(huì)被Activity保存,下次showDialog(id)方法被調(diào)用時(shí),若該id對(duì)話框已經(jīng)不再創(chuàng)建,則系統(tǒng)不會(huì)再次調(diào)用OnCreateDialog(id)方法創(chuàng)建該對(duì)話框,而是會(huì)回調(diào)onPrepareDialog(int id, Dialog dialog)方法,該方法允許對(duì)話框在被顯示之前做一些修改。

常用的對(duì)話框有AlertDialog和ProgressDialog,本節(jié)將通過實(shí)例講解這兩種對(duì)話框的使用方法。

4.7.1 AlertDialog

AlertDialog對(duì)話框是十分常用的用于顯示信息的方式,最多可提供三個(gè)按鈕。AlertDialog不能直接通過構(gòu)造方法構(gòu)建,而要由AlertDialog.Builder類來創(chuàng)建。AlertDialog對(duì)話框的標(biāo)題、按鈕以及按鈕要響應(yīng)的事件也由AlertDialog.Builder設(shè)置。

在使用AlertDialog. Builder創(chuàng)建對(duì)話框時(shí)常用的幾個(gè)方法如下:


●setTitle():設(shè)置對(duì)話框設(shè)置標(biāo)題。

●setIcon():設(shè)置對(duì)話框設(shè)置圖標(biāo)。

●setMessage():設(shè)置對(duì)話框的提示信息。

●setPositiveButton():對(duì)話框添加yes按鈕。

●setNegativeButton():對(duì)話框添加no按鈕。

●setNeutralButton():為對(duì)話框添加第三個(gè)按鈕。


下面通過實(shí)例來學(xué)習(xí)創(chuàng)建AlertDialog的方法。

創(chuàng)建Eclipse Android工程DialogDemo,并在main.xml中添加兩個(gè)按鈕,分別為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對(duì)話框,如圖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("這是一個(gè)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)建了有三個(gè)按鈕的AlertDialog,并且為每個(gè)按鈕添加了事件處理方法,以便獲知用戶單擊了哪個(gè)按鈕。

4.7.2 ProgressDialog

ProgressDialog是一個(gè)帶有進(jìn)度條的對(duì)話框,當(dāng)應(yīng)用程序在完成比較耗時(shí)的工作時(shí),使用該對(duì)話框可以為用戶提供一個(gè)總進(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對(duì)話框,如圖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;
主站蜘蛛池模板: 贺兰县| 白银市| 错那县| 抚宁县| 荆州市| 陵水| 常宁市| 彭阳县| 辽阳县| 邵阳县| 鹤岗市| 根河市| 兴宁市| 六盘水市| 铁岭县| 荥阳市| 辽源市| 温宿县| 尚义县| 临朐县| 工布江达县| 屏南县| 云安县| 拜泉县| 高尔夫| 京山县| 祁阳县| 建平县| 苏尼特右旗| 绥阳县| 营口市| 鄂州市| 门头沟区| 广水市| 阳信县| 桂平市| 汨罗市| 手游| 大同县| 宁河县| 鞍山市|