Android AlertDialog六種創建方式案例詳解

創建AlertDialog的步驟:

  1. 創建AlertDialog.Builder對象
  2. 調用Builder對象的setTitle方法設置標題,setIcon方法設置圖標
  3. 調用Builder相關方法如setMessage方法、setItems方法、setSingleChoiceItems方法、setMultiChoiceItems方法、setAdapter方法、setView方法設置不同類型的對話框內容。
  4. 調用setPositiveButton、setNegativeButton、setNeutralButton設置多個按鈕
  5. 調用Builder對象的create()方法創建AlertDialog對象
  6. 調用AlertDialog對象的show()方法將對話框顯示出來

一、setMessage:設置對話框內容為簡單文本內容

// 創建構建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 設置參數
        builder.setTitle("請做出選擇").setIcon(R.drawable.ic_launcher)
                .setMessage("我美不美")
                .setPositiveButton("美", new OnClickListener() {// 積極

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, "恭喜你答對瞭", 0)
                                        .show();
                            }
                        }).setNegativeButton("不美", new OnClickListener() {// 消極

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, "一點也不老實", 0)
                                        .show();
                            }
                        }).setNeutralButton("不知道", new OnClickListener() {// 中間級

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, "快睜開眼瞅瞅", 0)
                                        .show();
                            }
                        });
        builder.create().show();

運行截圖:

二、setItem:設置文本框內容為簡單列表項

// 創建數據
        final String[] items = new String[] { "北京", "上海", "廣州", "深圳" };
        // 創建對話框構建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 設置參數
        builder.setIcon(R.drawable.ic_launcher).setTitle("提示")
                .setItems(items, new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, items[which],
                                Toast.LENGTH_SHORT).show();
                    }
                });
        builder.create().show();

運行截圖:

三、setSingleChoiceItems()設置對話框內容為單選列表項

// 創建數據
        final String[] items = new String[] { "北京", "上海", "廣州", "深圳" };
        // 創建對話框構建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 設置參數
        builder.setIcon(R.drawable.ic_launcher).setTitle("提示")
                .setSingleChoiceItems(items, 0, new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, items[which],
                                Toast.LENGTH_SHORT).show();
                    }
                });
        builder.create().show();

運行截圖:

四、setMultiChoiceItems()設置對話框內容為多選項列表

// 創建數據
        final String[] items = new String[] { "北京", "上海", "廣州", "深圳" };
        // 創建對話框構建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle("提示")
                .setMultiChoiceItems(items,
                        new boolean[] { true, true, false, false, false },
                        new OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked) {
                                // TODO Auto-generated method stub
                                if (isChecked) {
                                    Toast.makeText(MainActivity.this,
                                            items[which], 0).show();
                                }
                            }
                        });
        builder.create().show();

運行截圖:

五、setAdapter()設置對話框內容為自定義列表項(這裡是一個佈局)

// 創建數據
        final String[] items = new String[] { "北京", "上海", "廣州", "深圳" };
        // 創建對話框構建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setIcon(R.drawable.ic_launcher)
                .setAdapter(
                        new ArrayAdapter<String>(MainActivity.this,
                                R.layout.item, R.id.tv, items),
                        new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, items[which],
                                        0).show();
                            }
                        });
        builder.create().show();

運行截圖:

六、setView()設置對話框為自定義View

// 創建對話框構建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // 獲取佈局
        View view2 = View.inflate(MainActivity.this, R.layout.login, null);
        // 獲取佈局中的控件
        final EditText username = (EditText) view2.findViewById(R.id.username);
        final EditText password = (EditText) view2.findViewById(R.id.password);
        final Button button = (Button) view2.findViewById(R.id.btn_login);
        // 設置參數
        builder.setTitle("Login").setIcon(R.drawable.ic_launcher)
                .setView(view2);
        // 創建對話框
        final AlertDialog alertDialog = builder.create();
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String uname = username.getText().toString().trim();
                String psd = password.getText().toString().trim();
                if (uname.equals("shenhua") && psd.equals("123456")) {
                    Toast.makeText(MainActivity.this, "登錄成功", 0).show();
                }
                Toast.makeText(MainActivity.this, "登錄失敗", 0).show();
                alertDialog.dismiss();// 對話框消失
            }

        });
        alertDialog.show();

運行截圖:

到此這篇關於Android AlertDialog六種創建方式案例詳解的文章就介紹到這瞭,更多相關Android AlertDialog六種創建方式內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: