android入門 — AlertDialog對話方塊

來源:互聯網
上載者:User

標籤:標題   bool   create   選擇   方式   建立   button   width   ini   

  常見的對話方塊主要分為訊息提示對話方塊、確認對話方塊、列表對話方塊、單選對話方塊、多選對話方塊和自訂對話方塊。

  對話方塊可以阻礙當前的UI線程,常用於退出確認等方面。

  在這裡主要的步驟可以總結為:
  1.建立AlertDialog.Builder對象;

  2.調用setTitle()或setCustomTitle()方法設定標題;

  3.調用setIcon()設定表徵圖;

  4.調用setPositiveButton()、setNegativeButton()或setNeturalButton()添加按鈕;

  5.調用AlertDialog.Builder的create()方法來建立AlertDialog對象;

  6.調用AlertDialog.Builder的show()方法將對話方塊顯示出來。

 

  這一部分,主要使用的是設計模式中的建造者模式,將東西提供給builder,然後會組裝成一個完整的對話方塊。

 

①顯示提示訊息的對話方塊

  

                                        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id)                                        {                                            new AlertDialog.Builder(this)                                            .setIcon(R.drawable.ic_4)                                            .setTitle("提示")                                            .setMessage("這是一個提示資訊")                                            //處理對話方塊的時候往往是在一個事件中處理,所以此時使用內部類的方式去做                                            .setPositiveButton("確定", new DialogInterface.OnClickListener() {                                                @Override                                                public void onClick(DialogInterface dialog, int which) {                                                    //注意這裡的this必須要修改,因為是在內部類中,所以直接使用this指的是這個內部類,所以需要修改                                                    Toast.makeText(MainActivity.this, "點擊了確認", Toast.LENGTH_SHORT).show();                                                }                                            })                                            .show();                                            return true;                                        }

設定了表徵圖、標題和提示資訊等屬性。

②確認對話方塊

         public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id)                            {                                new AlertDialog.Builder(this)                                        .setIcon(R.drawable.ic_4)                                        .setTitle("提示")                                        //可以設定null,表示在點擊之後什麼都不做,沒有後續的處理                                        //只是使得對話方塊消失                                        .setNegativeButton("取消", null)                                        //處理對話方塊的時候往往是在一個事件中處理,所以此時使用內部類的方式去做                                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {                                            @Override                                            public void onClick(DialogInterface dialog, int which) {                                                //注意這裡的this必須要修改,因為是在內部類中,所以直接使用this指的是這個內部類,所以需要修改                                                //Toast.makeText(MainActivity.this, "點擊了確認", Toast.LENGTH_SHORT).show();                                                //也可以使用finish()結束當前activity的生命週期,變為不可見,之後還可以使用這個activity的資源                                                //如果當前的activity是主介面,那麼activity棧就變成空的,                                                finish();                                                //如果調用exit()則是                                                //System.exit(0);                                            }                                        })                                        .show();                                return true;                            }

  在這裡添加了表徵圖、標題。

  主要的改變是增加了setNegativeButton()方法和setPositiveButton()方法,用來點擊確認或者取消。

③列表對話方塊

 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id)                                    {                                        final String[] arr = {"水可載舟,亦可賽艇。", "不要總想搞大新聞!", "西方哪個國家我沒去過?", "too young,too simple!"};                                        new AlertDialog.Builder(this)                                                .setIcon(R.drawable.ic_4)                                                .setTitle("提示")                                                .setItems(arr, new DialogInterface.OnClickListener() {                                                    @Override                                                    public void onClick(DialogInterface dialog, int which) {                                                        Toast.makeText(MainActivity.this, "您選擇了: " + arr[which], Toast.LENGTH_SHORT).show();                                                    }                                                })                                                .setPositiveButton("確定", new DialogInterface.OnClickListener() {                                                    @Override                                                    public void onClick(DialogInterface dialog, int which) {                                                    }                                                })                                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                                                    @Override                                                    public void onClick(DialogInterface dialog, int which) {                                                    }                                                })                                                .show();                                        return true;                                    }

  首先會定義一個字元數組,然後在onClick()中將字元數組與之綁定。

  

 

android入門 — AlertDialog對話方塊

聯繫我們

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