Android軟體開發之盤點所有Dialog對話方塊大合集(一)

來源:互聯網
上載者:User
對話方塊大合集

雨松MOMO原創文章如轉載,請註明:轉載至我的獨立網域名稱部落格雨松MOMO程式研究院,原文地址:http://www.xuanyusong.com/archives/9

雨松MOMO帶大家盤點Android 中的對話方塊


今天我用自己寫的一個Demo 和大家詳細介紹一個Android中的對話方塊的提示。


1.確定取消對話方塊

對話方塊中有2個按鈕   通過調用 setPositiveButton 方法 和 setNegativeButton 方法 可以設定按鈕的顯示內容以及按鈕的監聽事件。

我們使用AlerDialog 建立對話方塊

AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);

使用builder設定對話方塊的title button icon 等等

    builder.setIcon(R.drawable.icon);            builder.setTitle("你確定要離開嗎?");            builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int whichButton) {                    //這裡添加點擊確定後的邏輯                    showDialog("你選擇了確定");                }            });            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int whichButton) {                    //這裡添加點擊確定後的邏輯                    showDialog("你選擇了取消");                }            });            builder.create().show();

  這個dialog用於現實onClick後監聽的內容資訊

    private void showDialog(String str) { new AlertDialog.Builder(MainDialog.this)         .setMessage(str)         .show();    }

2.多個按鈕資訊框

            AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);                builder.setIcon(R.drawable.icon);            builder.setTitle("投票");            builder.setMessage("您認為什麼樣的內容能吸引您?");            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("主題強的", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int whichButton) {                    showDialog("你選擇了主題強的");                  }            });            builder.create().show();

3.列表框

這個數組用於列表選擇

final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};
   AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);            builder.setTitle("列表選擇框");            builder.setItems(mItems, new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int which) {                    //點擊後快顯視窗選擇了第幾項                    showDialog("你選擇的id為" + which + " , " + mItems[which]);                }            });            builder.create().show();

4.單項挑選清單框

mSingleChoice 用於記錄單選中的ID

int mSingleChoiceID = -1;
         AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);         mSingleChoiceID = -1;         builder.setIcon(R.drawable.icon);             builder.setTitle("單項選擇");             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();

5.進度條框



點擊進度條框按鈕後 開啟一個線程計算讀取的進度 假設讀取結束為 100
Progress在小於100的時候一直線上程中做迴圈++ 只到讀取結束後,停止線程。

                  mProgressDialog = new ProgressDialog(MainDialog.this);            mProgressDialog.setIcon(R.drawable.icon);            mProgressDialog.setTitle("進度條視窗");            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);            mProgressDialog.setMax(MAX_PROGRESS);            mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int whichButton) {                    //這裡添加點擊後的邏輯                }            });            mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int whichButton) {                    //這裡添加點擊後的邏輯                }            });            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();        }                 }        }

6.多項挑選清單框




MultiChoiceID 用於記錄多選選中的id號 存在ArrayList中
選中後 add 進ArrayList
取消選中後 remove 出ArrayList。

 ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();

        AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);        MultiChoiceID.clear();        builder.setIcon(R.drawable.icon);            builder.setTitle("多項選擇");            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();

7.自訂布局


講到自訂布局我就得多說一說了,為什麼要多說一說呢?
其實自訂布局在Android的開發中非常重要 因為它能讓開發人員做出自己五彩繽紛的Activity 而不用去使用系統枯燥的介面。

自訂dialog有什麼好處?

比如我們在開發過長當中 要通過介紹系統發送的一個廣播彈出一個dialog . 但是dialog必需是基於activity才能呈現出來 如果沒有activity 的話 程式就會崩潰。所以我們可以寫一個自訂的 dialog 把它定義成一個activity
這樣我們收到一條開啟dialog的廣播後 直接啟動這個 activity  程式正常運行~~

這就是自訂dialog的好處。

註明:下面這個例子只是寫了自訂dialog 沒有把它單獨的寫在一個activity中 如果須要的話 可以自己改一下。

           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();

<?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"><LinearLayoutandroid: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>  <LinearLayoutandroid: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>


8.讀取進度框

顯示一個正在轉圈的進度條loading

    mProgressDialog = new ProgressDialog(this);    mProgressDialog.setTitle("讀取ing");    mProgressDialog.setMessage("正在讀取中請稍候");    mProgressDialog.setIndeterminate(true);    mProgressDialog.setCancelable(true);    mProgressDialog.show();

最後如果你還是覺得我寫的不夠詳細 不要緊我把原始碼的貼出來 歡迎大家一起討論學習 雨松MOMO希望可以和大家一起進步。

:http://www.xuanyusong.com/archives/9

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.