Android AlterDialog 常用對話方塊使用方法詳解

codeceo發表於2015-07-25

AlterDialog在Android開發中使用也比較頻繁,本文就介紹了AlterDialog的幾種對話方塊使用方式及程式碼解析。

使用AlerDialog 建立對話方塊 :

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

1.設定簡單的對話方塊

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

2.設定列表對話方塊

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) {  
                //點選後彈出視窗選擇了第幾項  
            }  
        });  
        builder.create().show();

3.設定單選對話方塊

int 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;  
        }  
    });  
    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  
            if(mSingleChoiceID > 0) {  
          //  你選擇的是" + mSingleChoiceID;  
            }  
        }  
    });  
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  

        }  
    });  
   builder.create().show();

4.設定多選對話方塊

ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>(); 
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);  
                  //   你選擇的為: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++) {  
          <span style="white-space:pre">	</span>  str+= mItems[MultiChoiceID.get(i)] + ", ";  
            }  
        }  
    });  
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {  

        }  
    });  
   builder.create().show();

5.設定自定義佈局對話方塊

     View view =  LayoutInflater.from(this).inflate(R.layout.custom, null);  
     builder.setIcon(R.drawable.icon);  
     builder.setTitle("自定義輸入框");  
     builder.setView(<span style="font-family: 宋體, 'Arial Narrow', arial, serif;">view</span>);  
     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);  
         // userName.getText().toString()  +  password.getText().toString();  
         }  
     });  
     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
         public void onClick(DialogInterface dialog, int whichButton) {  

         }  
     });  
   builder.create().show();

相關文章