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

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

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

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

綜合交流 / 評測 / 活動區(qū)

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

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

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

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

Android軟件開發(fā)之盤點(diǎn)所有Dialog對話框大合集(一)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:19 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
40963對話框大合集   
   
今天我用自己寫的一個Demo 和大家詳細(xì)介紹一個Android中的對話框的使用技巧。   
     
1.確定取消對話框   
   
對話框中有2個按鈕   通過調(diào)用 setPositiveButton 方法 和 setNegativeButton 方法 可以設(shè)置按鈕的顯示內(nèi)容以及按鈕的監(jiān)聽事件。   
     
我們使用AlerDialog 創(chuàng)建對話框   
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);        
復(fù)制代碼
使用builder設(shè)置對話框的title button icon 等等   
  1.             builder.setIcon(R.drawable.icon);   
                builder.setTitle("你確定要離開嗎?");   
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        //這里添加點(diǎn)擊確定后的邏輯   
                        showDialog("你選擇了確定");   
                    }   
                });   
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        //這里添加點(diǎn)擊確定后的邏輯   
                        showDialog("你選擇了取消");   
                    }   
                });   
                builder.create().show();   
復(fù)制代碼
這個dialog用于現(xiàn)實(shí)onClick后監(jiān)聽的內(nèi)容信息   
  1.     private void showDialog(String str) {   
             new AlertDialog.Builder(MainDialog.this)   
             .setMessage(str)   
             .show();   
        }
復(fù)制代碼
2.多個按鈕信息框   
     
     
  1.             AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);                       
                builder.setIcon(R.drawable.icon);   
                builder.setTitle("投票");   
                builder.setMessage("您認(rèn)為什么樣的內(nèi)容能吸引您?");   
                builder.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        showDialog("你選擇了有趣味的");   
                    }   
                });   
                builder.setNeutralButton("有思想的", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        showDialog("你選擇了有思想的");                       
                    }   
                });   
                builder.setNegativeButton("主題強(qiáng)的", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        showDialog("你選擇了主題強(qiáng)的");     
                    }   
                });   
                builder.create().show();
復(fù)制代碼
3.列表框   
     
這個數(shù)組用于列表選擇   
  1. final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};
復(fù)制代碼
  
  1.            AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);           
                builder.setTitle("列表選擇框");   
                builder.setItems(mItems, new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int which) {   
                        //點(diǎn)擊后彈出窗口選擇了第幾項(xiàng)   
                        showDialog("你選擇的id為" + which + " , " + mItems[which]);   
                    }   
                });   
                builder.create().show();   
復(fù)制代碼
4.單項(xiàng)選擇列表框   
     
     
mSingleChoice 用于記錄單選中的ID   
  1. int mSingleChoiceID = -1;
復(fù)制代碼
  
  1.          AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);           
       
             mSingleChoiceID = -1;   
             builder.setIcon(R.drawable.icon);   
                 builder.setTitle("單項(xiàng)選擇");   
                 builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {   
                     public void onClick(DialogInterface dialog, int whichButton) {   
                             mSingleChoiceID = whichButton;   
                             showDialog("你選擇的id為" + whichButton + " , " + mItems[whichButton]);   
                     }   
                 });   
                 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {   
                     public void onClick(DialogInterface dialog, int whichButton) {   
                         if(mSingleChoiceID > 0) {   
                         showDialog("你選擇的是" + mSingleChoiceID);   
                         }   
                     }   
                 });   
                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   
                     public void onClick(DialogInterface dialog, int whichButton) {   
       
                     }   
                 });   
                builder.create().show();
復(fù)制代碼
5.進(jìn)度條框   
     
點(diǎn)擊進(jìn)度條框按鈕后 開啟一個線程計(jì)算讀取的進(jìn)度 假設(shè)讀取結(jié)束為 100   
Progress在小于100的時候一直在線程中做循環(huán)++ 只到讀取結(jié)束后,停止線程。   
  1.                   mProgressDialog = new ProgressDialog(MainDialog.this);   
                        mProgressDialog.setIcon(R.drawable.icon);   
                        mProgressDialog.setTitle("進(jìn)度條窗口");   
                        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);   
                        mProgressDialog.setMax(MAX_PROGRESS);   
                        mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() {   
                            public void onClick(DialogInterface dialog, int whichButton) {   
                                //這里添加點(diǎn)擊后的邏輯   
                            }   
                        });   
                        mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {   
                            public void onClick(DialogInterface dialog, int whichButton) {   
                                //這里添加點(diǎn)擊后的邏輯   
                            }   
                        });   
                        mProgressDialog.show();   
                        new Thread(this).start();   
       
        public void run() {   
            int Progress = 0;   
            while(Progress < MAX_PROGRESS) {   
            try {   
                Thread.sleep(100);   
                Progress++;     
                mProgressDialog.incrementProgressBy(1);   
            } catch (InterruptedException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
                
            }   
          
        }
復(fù)制代碼
6.多項(xiàng)選擇列表框   
     
     
MultiChoiceID 用于記錄多選選中的id號 存在ArrayList中   
選中后 add 進(jìn)ArrayList   
取消選中后 remove 出ArrayList。   
  1. ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
復(fù)制代碼
  
  1.         AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);           
       
            MultiChoiceID.clear();   
            builder.setIcon(R.drawable.icon);   
                builder.setTitle("多項(xiàng)選擇");   
                builder.setMultiChoiceItems(mItems,   
                        new boolean[]{false, false, false, false, false, false, false},   
                        new DialogInterface.OnMultiChoiceClickListener() {   
                            public void onClick(DialogInterface dialog, int whichButton,   
                                    boolean isChecked) {   
                               if(isChecked) {   
                                   MultiChoiceID.add(whichButton);   
                                   showDialog("你選擇的id為" + whichButton + " , " + mItems[whichButton]);   
                               }else {   
                                   MultiChoiceID.remove(whichButton);   
                               }   
                                  
                            }   
                        });   
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        String str = "";   
                        int size = MultiChoiceID.size();   
                        for (int i = 0 ;i < size; i++) {   
                        str+= mItems[MultiChoiceID.get(i)] + ", ";   
                        }   
                        showDialog("你選擇的是" + str);   
                    }   
                });   
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
       
                    }   
                });   
               builder.create().show();
復(fù)制代碼
7.自定義布局   
     
講到自定義布局我就得多說一說了,為什么要多說一說呢?   
其實(shí)自定義布局在Android的開發(fā)中非常重要 因?yàn)樗茏岄_發(fā)者做出自己五彩繽紛的Activity 而不用去使用系統(tǒng)枯燥的界面。   
   
自定義dialog有什么好處?   
   
比如我們在開發(fā)過長當(dāng)中 要通過介紹系統(tǒng)發(fā)送的一個廣播彈出一個dialog . 但是dialog必需是基于activity才能呈現(xiàn)出來 如果沒有activity 的話 程序就會崩潰。所以我們可以寫一個自定義的 dialog 把它定義成一個activity   
這樣我們收到一條打開dialog的廣播后 直接啟動這個 activity  程序正常運(yùn)行~~   
   
這就是自定義dialog的好處。   
   
注明:下面這個例子只是寫了自定義dialog 沒有把它單獨(dú)的寫在一個activity中 如果須要的話 可以自己改一下。   
  1.            AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);           
                LayoutInflater factory = LayoutInflater.from(this);   
                final View textEntryView = factory.inflate(R.layout.test, null);   
                    builder.setIcon(R.drawable.icon);   
                    builder.setTitle("自定義輸入框");   
                    builder.setView(textEntryView);   
                    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int whichButton) {   
                           
                        EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);   
                        EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);   
                        showDialog("姓名 :"  + userName.getText().toString()  + "密碼:" + password.getText().toString() );   
                        }   
                    });   
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {   
                        public void onClick(DialogInterface dialog, int whichButton) {   
       
                        }   
                    });   
                  builder.create().show();
復(fù)制代碼
  
  1. <span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>   
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"   
    android:orientation="horizontal"   
    android:id="@+id/dialog">   
    <LinearLayout   
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"   
    android:orientation="horizontal"   
    android:id="@+id/dialogname">   
       
    <TextView android:layout_height="wrap_content"   
       android:layout_width="wrap_content"   
      android:id="@+id/tvUserName"   
      android:text="姓名:" />   
    <EditText android:layout_height="wrap_content"   
      android:layout_width="wrap_content"   
      android:id="@+id/etUserName"   
      android:minWidth="200dip"/>   
    </LinearLayout>     
    <LinearLayout   
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"   
    android:orientation="horizontal"   
    android:id="@+id/dialognum"   
    android:layout_below="@+id/dialogname"   
    >   
      <TextView android:layout_height="wrap_content"   
       android:layout_width="wrap_content"   
      android:id="@+id/tvPassWord"   
      android:text="密碼:" />   
    <EditText android:layout_height="wrap_content"   
      android:layout_width="wrap_content"   
      android:id="@+id/etPassWord"   
      android:minWidth="200dip"/>   
    </LinearLayout>     
      </RelativeLayout></span>
復(fù)制代碼
8.讀取進(jìn)度框   
   
顯示一個正在轉(zhuǎn)圈的進(jìn)度條loading   
  1.     mProgressDialog = new ProgressDialog(this);   
                mProgressDialog.setTitle("讀取ing");   
                mProgressDialog.setMessage("正在讀取中請稍候");   
                mProgressDialog.setIndeterminate(true);   
                mProgressDialog.setCancelable(true);   
                mProgressDialog.show();
復(fù)制代碼
最后如果你還是覺得我寫的不夠詳細(xì) 不要緊我把源代碼的下載地址貼出來 歡迎大家一起討論學(xué)習(xí) 雨松MOMO希望可以和大家一起進(jìn)步。   
Android dialog 大合集.rar(140.23 KB, 下載次數(shù): 3814)[/I]2011-9-2 19:07 上傳點(diǎn)擊文件名   下載積分: 下載豆 -2   

上一篇:第二十七講:Handler使用入門
下一篇:第二十四講:Android動畫入門(一)
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

GMT+8, 2024-10-21 03:04 , Processed in 0.065704 second(s), 13 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

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