Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android資訊 >> Android AlterDialog 常用對話框使用方法詳解

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

編輯:Android資訊

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 >	</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 >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();
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved