Android之對話方塊

來源:互聯網
上載者:User

標籤:

一:AlertDialog

AlertDialog的構造方法全部是Protected的,所以不能直接通過new一個AlertDialog來建立出一個AlertDialog。

要建立一個AlertDialog,就要用到AlertDialog.Builder中的create()方法。

AlertDialog.Builder常用方法

   setTitle :為對話方塊設定標題
setIcon :為對話方塊設定表徵圖
setMessage:為對話方塊設定內容
setView : 給對話方塊設定自訂樣式
setItems :設定對話方塊要顯示的一個list,一般用於顯示幾個命令時
setMultiChoiceItems :用來設定對話方塊顯示一系列的複選框
setNeutralButton :普通按鈕

setPositiveButton :給對話方塊添加"Yes"按鈕
setNegativeButton :對話方塊添加"No"按鈕
create : 建立對話方塊
show :顯示對話方塊

 

import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog.Builder;  import android.app.AlertDialog; import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button=(Button)this.findViewById(R.id.button1);        button.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub              Builder adInfo=new AlertDialog.Builder(MainActivity.this);                adInfo.setTitle("簡單對話方塊");                       //設定標題                  adInfo.setMessage("test");  //設定內容                  adInfo.setIcon(R.drawable.ic_launcher);          //設定表徵圖                  adInfo.create();                adInfo.show();            }        });    }

1.AlertDialog可以顯示3個按鈕,現在我們增加顯示按鈕

1. dialog.setPositiveButton按鈕顯示在右邊,2. dialog.setNegativeButton顯示在左邊3.dialog.setNeutralButton按鈕顯示在正中

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button=(Button)this.findViewById(R.id.button1);        button.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                Builder adInfo=new AlertDialog.Builder(MainActivity.this);                  adInfo.setTitle("簡單對話方塊");                       //設定標題                    adInfo.setMessage("test");  //設定內容                    adInfo.setIcon(R.drawable.ic_launcher);                 adInfo.setPositiveButton("取消", new DialogInterface.OnClickListener() {                       @Override                       public void onClick(DialogInterface dialog, int which) {                            // 根據實際情況編寫相應代碼。                       }                  });                  //為“取消”按鈕註冊監聽事件                   adInfo.setNegativeButton("確定", new DialogInterface.OnClickListener() {                       @Override                       public void onClick(DialogInterface dialog, int which) {                            // 根據實際情況編寫相應代碼。                       }                  });                  adInfo.create();                  adInfo.show();            }        });    }

2.AlertDialog+按鈕和複選框

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button=(Button)this.findViewById(R.id.button1);        button.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                 String[] choices={"Facebook","Twitter"};                  //Check判斷數組,與選項對應                  boolean[] chsBool = {true,false};                   //包含多個選項及複選框的對話方塊                  AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)                           .setIcon(android.R.drawable.btn_star_big_on)                           .setTitle("調查")                           .setMultiChoiceItems(choices, chsBool, multiClick)                           .setPositiveButton("yes",  onclick)                         .setNegativeButton("No",  onclick).create();                  dialog.show();            }        });    }    OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){          @Override          public void onClick(DialogInterface dialog, int which, boolean isChecked) {              Toast.makeText(MainActivity.this, "第"+(which+1)+"項,選中結果:"+isChecked,Toast.LENGTH_SHORT).show();          }                };             OnClickListener onclick = new OnClickListener() {         @Override        public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub             switch (which) {               case Dialog.BUTTON_NEGATIVE:                   Toast.makeText(MainActivity.this, "No..",                           Toast.LENGTH_LONG).show();                   break;               case Dialog.BUTTON_NEUTRAL:                   Toast.makeText(MainActivity.this, "I don‘t know.",                           Toast.LENGTH_LONG).show();                   break;               case Dialog.BUTTON_POSITIVE:                   Toast.makeText(MainActivity.this, "Yes!!",                           Toast.LENGTH_LONG).show();                   break;           }       }    };
二、自訂AlertDialog

如何自訂一個對話方塊呢?

(1)修改系統預設的Dialog樣式(風格、主題)

(2)自訂Dialog布局檔案

(3)可以自己封裝一個類,繼承自Dialog或者直接使用Dialog類來實現,為了方便以後重複使用,建議自己封裝一個Dialog類。

待續。。。。

 

三、ProgressDialog1.圓形進度條的使用
public class MainActivity extends Activity {    private Button button1,button2;     //聲明進度條對話方塊        private ProgressDialog pdDialog=null;     //進度計數      int iCount  = 0;       @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1=(Button)this.findViewById(R.id.progress);        button2=(Button)this.findViewById(R.id.circle);        button1.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                  iCount  = 0;                      pdDialog=new ProgressDialog(MainActivity.this);                     //設定進度條風格,風格為圓形,旋轉的                         pdDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);                          // 設定ProgressDialog 標題                         pdDialog.setTitle("圓形進度條");                           // 設定ProgressDialog 提示資訊                         pdDialog.setMessage("正在下載中……");                         // 設定ProgressDialog 標題表徵圖                         pdDialog.setIcon(R.drawable.ic_launcher);                          // 設定ProgressDialog 進度條進度                         pdDialog.setProgress(100);                         // 設定ProgressDialog 的進度條是否不明確                         pdDialog.setIndeterminate(false);                         // 設定ProgressDialog 是否可以按退回按鍵取消                         pdDialog.setCancelable(true);                         // 設定ProgressDialog 的一個Button                     pdDialog.setButton("取消", new DialogInterface.OnClickListener() {                             public void onClick(DialogInterface dialog, int i)                             {                                 //點擊“取消”按鈕取消對話方塊                                 dialog.cancel();                             }                         });                        pdDialog.show();                                                //建立線程執行個體                         new Thread(){                                public void run(){                                    try{                                        while (iCount  <= 100) {                                            // 由線程來控制進度。                                            pdDialog.setProgress(iCount ++);                                            Thread.sleep(50);                                       }                                       pdDialog.cancel();                                    }                                    catch (InterruptedException e){                                        pdDialog.cancel();                                    }                                }                              }.start();                        }            });        button2.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View arg0) {                        // TODO Auto-generated method stub                                            }                });    }
2、環形進度條的使用

將其類型改成

pdDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

Android之對話方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.