AlertDialog的使用


1.Alertdialog的幾種形式:

 

2.第一種:簡單對話框

 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setTitle("簡單對話框");
        localBuilder.setIcon(R.mipmap.ic_launcher);
        localBuilder.setMessage("提示信息?");
        localBuilder.setPositiveButton("確定", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
               /**
                * 確定操作
                * */
            }
        });
        localBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 * 確定操作
                 * */
            }
        });

        /***
         * 設置點擊返回鍵不會消失
         * */
       localBuilder.setCancelable(false).create();

        localBuilder.show();

  

3.第二種:列表式對話框

 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = { "選項1", "選項2",  "選項3", "選項4", "選項5", "選項6" };
        localBuilder.setTitle("簡單列表對話框").setIcon(R.mipmap.ic_launcher).setItems(arrayOfString, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 * 操作
                 * */
                Toast.makeText(MainActivity.this, "你選擇了: " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();
                /**
                 * 列表對話框不加這句,點擊選擇后也后不會消失
                 * */
                paramAnonymousDialogInterface.dismiss();
            }
        }).create().show();

  

4.第三種形式:單選對話框

 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = {  "1", "2", "3", "4", "5", "6"};
        localBuilder.setTitle("單選對話框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setSingleChoiceItems(arrayOfString, 3, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 * 操作
                 * */
                Toast.makeText(MainActivity.this, "你選擇了: " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();

                paramAnonymousDialogInterface.dismiss();
            }
        }).setCancelable(false).create().show();

  

5.第四種形式:多選對話框

  AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = { "0", "1", "2", "3", "4" };
        localBuilder.setTitle("多選對話框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setMultiChoiceItems(arrayOfString, new boolean[] { true, true, true, false, true }, new DialogInterface.OnMultiChoiceClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt, boolean paramAnonymousBoolean)
            {
                if (paramAnonymousBoolean) {
                    Toast.makeText(MainActivity.this, "你選擇了: " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();

                }
            }
        }).setPositiveButton("提交", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                paramAnonymousDialogInterface.dismiss();
            }
        }).create().show();

6.第五種形式:自定義對話框

 

 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setTitle("自定義列表對話框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setView(getLayoutInflater().inflate(R.layout.layout, null));
        localBuilder.setPositiveButton("確定", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 *
                 * 操作
                 * */
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 *
                 * 操作
                 * */
            }
        }).create().show();

  

自定義列表對話框:

 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = { "0", "1", "2", "3", "4", "5", "6", "7", "8" };
        localBuilder.setTitle("自定義列表對話框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setAdapter(new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item, arrayOfString), new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                Toast.makeText(MainActivity.this, "你選擇了 :  " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();
            }
        }).create().show();

  

 <AlertDialog 主題顏色>

  傳統主題:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_TRADITIONAL)

  

     深黑色主題:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_DARK);

  

 藍色主題:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_LIGHT);

  

  深色主題:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);

  

 淺藍主題:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM