Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Android AlertDialog類的幾個簡單實例

Android AlertDialog類的幾個簡單實例

編輯:中級開發

先看看android sdk上的說明 Class OvervIEw

A subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method. If you want to display a more complex view, look up the FrameLayout called "body" and add your vIEw to it:

FrameLayout fl = (FrameLayout) findVIEwById(R.id.body);

fl.add(myVIEw, new LayoutParams(FILL_PARENT, WRAP_CONTENT));

The AlertDialog class takes care of automatically setting WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you based on whether any views in the dialog return true from VIEw.onCheckIsTextEditor(). Generally you want this set for a Dialog without text editors, so that it will be placed on top of the current input method UI. You can modify this behavior by forcing the flag to your desired mode after calling onCreate(Bundle).

類概述: Dialog的一個子類,可以顯示一到三個按鈕。如果你只是想在消息框中顯示字符串,使用setMessage()方法。如果你想顯示一個稍微復雜的視窗,查找到FrameLayout對應的body控件並把你的視窗添加上去:實例:創建對話框的時候,我們需要通過AlertDialog.Builder(Context context)生成器來構造一個Builder並生成一個AlertDialog。 AlertDialog.Builder的設置內容較多,以後跟上。
下面先創建的是比較常見的幾個dialog實例
1.創建含有OK和Cancel兩個按鈕的dialog對話框。
new AlertDialog.Builder(MyAlertDialog.this)
// MyAlertDialog.this視情況而定,這個一般是指當前顯示的Activity對應的XML視窗。
.setTitle("真的要離開?")// 設置對話框的標題
.setMessage("你確定要離開")// 設置對話框的內容
.setPositiveButton("OK",// 設置對話框的確認按鈕
    new DialogInterface.OnClickListener() {// 設置確認按鈕的事件
        public void onClick(DialogInterface dialog, int which) {
            // do something here..I end this Prograss..
            android.os.Process.killProcess(android.os.Process.myPid()); .setNegativeButton("Cancel",// 設置對話框的取消按鈕
    new DialogInterface.OnClickListener() {// 設置取消按鈕的事件
        public void onClick(DialogInterface dialog, int which) {             dialog.cancel(); ).show();2.創建含有按鈕並且含有單選列表的dialog對話框。
/ * 我們在1的基礎上創建一個含有自定義的單選列表內容的dialog *因為我在res/values中的strings.XML中已經添加了對應的固定的選擇項字符串 <string name="str_status_1">status_1</string>
<string name="str_status_2">status_2</string>
<string name="str_status_3">status_3</string>
<string name="str_status_4">status_4</string>
// 所以在Java類中我只要獲取到strings.XML中的值,如下定義:
final CharSequence[] statusChar = {
    getResources().getString(R.string.str_status_1),
    getResources().getString(R.string.str_status_2),
    getResources().getString(R.string.str_status_3),
    getResources().getString(R.string.str_status_4) };
// 如果沒有定義的話,寫為:
final CharSequence[] statusChar = { "status_1",
    "status_2",
    "status_3",
    "status_4" };
// 定義好之後,我們就可以把這個變量加載到setSingleChoiceItems這個單選列表的設置裡面
int statusId = 0;// 設置選擇的初始值
new AlertDialog.Builder(MyAlertDialog.this)
.setTitle("真的要離開?")
.setMessage("你確定要離開")
.setSingleChoiceItems(statusChar,// 把statusChar添加到dialog裡面
    -1,// -1是默認選擇為非選擇,因為列表是從0開始。
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            statusId = which + 1;// 數組是從0開始,我要顯示的是被選擇的status狀態
            Toast.makeText(MyAlertDialog.this, statusChar[which], Toast.LENGTH_LONG).show()         } .setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do something here..I end this Prograss..
            android.os.Process.killProcess(android.os.Process.myPid()); .setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel(); ).show();3.創建一個自定義vIEw的對話框。
最後,我們來創建一個包含自定義vIEw的對話框。
首先,我們要在res/layout新建一個*.xml文件。我的文件名:statusvIEw.XML
比如:
<?XML version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    XMLns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/AbsoluteLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextVIEw
    android:layout_x="20dip"
    android:layout_y="10dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="當前狀態信息" >
    </TextVIEw>
    <EditText
    android:id="@+id/etInfo"
    android:singleLine="false"
    android:lines="5"
    android:gravity="top"
    android:layout_width="200dip"
    android:layout_height="200dip"
    android:layout_x="20dip"
    android:layout_y="60dip">
    </EditText>
</AbsoluteLayout> 之後就要在Java類中調用這個statusvIEw.XML文件
像文章開始說明的一樣,我們需要得到vIEw的實例 //獲取到布局解析器的對象
inflater = LayoutInflater.from(MyAlertDialog.this);
//然後將statusvIEw.XML賦到layout(VIEw的實例)上
final View layout = inflater.inflate(R.layout.statusvIEw, null);
//最後通過AlertDialog.Builder中的setView(VIEw)加載layout即可,其他的如按鈕之類的我就不寫清楚了,文章前面已有。
new AlertDialog.Builder(LsPos.this).setVIEw(layout).create().show();
想得到更多的信息,請參照開發文檔。轉載此文,請注明出處,謝謝。
http://blog.sina.com.cn/s/blog_48b61dc70100isqg.Html
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved