首頁(yè) 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場(chǎng)

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計(jì)算器

綜合交流 / 評(píng)測(cè) / 活動(dòng)區(qū)

交流區(qū) | 測(cè)硬件 | 網(wǎng)站活動(dòng) | Z幣中心

新手入門 / 進(jìn)階 / 社區(qū)互助

新手 | 你問(wèn)我答 | 免費(fèi)刷機(jī)救磚 | ROM固件

查看: 11159|回復(fù): 0
上一主題 下一主題
[教程]

第十七講:對(duì)話框 Android Dialog

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:19 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
683
本講內(nèi)容: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)容如下:
   
  1. <?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>
復(fù)制代碼
3、MainHelloAlertDialog.java的內(nèi)容如下:
   
  1. 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)聽器   
            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ū)ο?nbsp;  
                    Dialog dialog = new AlertDialog.Builder(this)   
                    .setIcon(android.R.drawable.btn_star).setTitle("喜好調(diào)查")   
                    .setMessage("你喜歡看愛德華.諾頓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)聽器   
                    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)聽器   
                    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("你喜歡看愛德華.諾頓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);   
            }   
       
    }
復(fù)制代碼
   4、運(yùn)行結(jié)果如下:     有興趣的同學(xué)可以考慮一下如何改進(jìn)關(guān)閉和移除對(duì)話框按鈕。三、自定義對(duì)話框 Custom Dialog(待續(xù))   

上一篇:Android游戲開發(fā)之地圖編輯器的使用以及繪制地圖 (四)
下一篇:第五講:用戶界面 View(一)

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號(hào) )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號(hào)

GMT+8, 2024-10-20 03:26 , Processed in 0.055239 second(s), 14 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報(bào):report#znds.com (請(qǐng)將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表