Android學習-各種對話框

在android的編程中,對話框的用處是非常多的,然而,有時對對話框的需求也是不一樣的,那麼現在,就總結對話框的種類,以及事件的處理形勢。

1、簡單的對話框

[java]
public void showDialog(){ 
   Dialog dialog = new AlertDialog.Builder(this) 
        .setTitle("標題") 
        .setMessage("消息") 
        .setPositiveButton("好", new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            } 
        }) 
        .setNegativeButton("不好", new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            } 
        }).create(); 
    dialog.show(); 

public void showDialog(){
   Dialog dialog = new AlertDialog.Builder(this)
     .setTitle("標題")
     .setMessage("消息")
     .setPositiveButton("好", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }
  })
     .setNegativeButton("不好", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }
  }).create();
    dialog.show();
}

 

2、帶編輯框的對話框

[java]
public void showDialog(){ 
    EditText et = new EditText(this);// 單獨出來,方便取用戶輸入的數據。  
    Dialog dialog = new AlertDialog.Builder(this)   
        .setTitle("請輸入")   
        .setIcon(android.R.drawable.ic_dialog_info)   
        .setView(et)   
        .setPositiveButton("確定", new DialogInterface.OnClickListener(){ 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            } 
             
        })   
        .setNegativeButton("取消", new DialogInterface.OnClickListener(){ 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            }})   
        .create(); 
    dialog.show(); 

public void showDialog(){
    EditText et = new EditText(this);// 單獨出來,方便取用戶輸入的數據。
    Dialog dialog = new AlertDialog.Builder(this) 
     .setTitle("請輸入") 
     .setIcon(android.R.drawable.ic_dialog_info) 
     .setView(et) 
     .setPositiveButton("確定", new DialogInterface.OnClickListener(){
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }
      
     }) 
     .setNegativeButton("取消", new DialogInterface.OnClickListener(){
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }}) 
     .create();
    dialog.show();
}

 

3、含單選鈕的對話框

[java]
public void showDialog(){ 
    final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ; 
    // 單獨列出來方便之後取數據  
        Dialog dialog = new AlertDialog.Builder(this) 
        .setTitle("單選框,請選擇") 
        .setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() { 
            // 3 代表默認選中str的index=3的那個單選框  
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ; 
            } 
        }) 
        .setPositiveButton("確定", new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            } 
        }) 
       .create(); 
    dialog.show(); 

public void showDialog(){
    final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
    // 單獨列出來方便之後取數據
     Dialog dialog = new AlertDialog.Builder(this)
     .setTitle("單選框,請選擇")
     .setSingleChoiceItems(str, 3, new DialogInterface.OnClickListener() {
      // 3 代表默認選中str的index=3的那個單選框
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Log.e("DIYDialogActivity", "showDialog->you select " + str[which] ) ;
   }
  })
  .setPositiveButton("確定", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }
  })
       .create();
    dialog.show();
}

 

4、含多選框的對話框

[java]
public void showDialog(){ 
        final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 單獨列出來方便之後取數據  
        boolean[] bstr = new boolean[]{true,false,false,false,false,false,false}; 
        Dialog dialog = new AlertDialog.Builder(this) 
        .setTitle("多選框,請選擇") 
        .setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
                Log.e("DIYDialogActivity", "showDialog->" + 
                        "you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ; 
            } 
        }) 
        .setPositiveButton("確定", new DialogInterface.OnClickListener(){ 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            } 
        }).create(); 
        dialog.show(); 

public void showDialog(){
        final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 單獨列出來方便之後取數據
     boolean[] bstr = new boolean[]{true,false,false,false,false,false,false};
     Dialog dialog = new AlertDialog.Builder(this)
     .setTitle("多選框,請選擇")
     .setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    Log.e("DIYDialogActivity", "showDialog->" +
      "you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ;
   }
     })
     .setPositiveButton("確定", new DialogInterface.OnClickListener(){
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }
     }).create();
     dialog.show();
}

 

5、含列表的對話框

[java]
public void showDialog(){ 
        final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ; 
        // 單獨列出來方便之後取數據  
        Dialog dialog = new AlertDialog.Builder(this) 
        .setTitle("列表框,供選擇") 
        .setItems(str, new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                Log.e("DIYDialogActivity", "showDialog———————you select " + which); 
            } 
        }) 
        .setPositiveButton("確定", new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                 
            } 
        }) 
        .create(); 
        dialog.show(); 

public void showDialog(){
        final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;
        // 單獨列出來方便之後取數據
     Dialog dialog = new AlertDialog.Builder(this)
     .setTitle("列表框,供選擇")
     .setItems(str, new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Log.e("DIYDialogActivity", "showDialog———————you select " + which);
   }
  })
     .setPositiveButton("確定", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    
   }
  })
     .create();
     dialog.show();
}
6、自定義的對話框

[java]
public void showDialog(){ 
        LayoutInflater inflater = getLayoutInflater(); 
        View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));  
        // 可指定對話框的背景色彩那些  
         
        Dialog dialog = new AlertDialog.Builder(this) 
        .setView(layout) 
        .create(); 
         
        // 設置對話框的出現位置,借助於window對象  
        Window win = dialog.getWindow(); 
        //win.setGravity(Gravity.CENTER);  
        //win.setGravity(Gravity.BOTTOM);  
        //win.setGravity(Gravity.TOP);  
        win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 彈出對話框時,底部窗體,不變暗。  
         
        WindowManager.LayoutParams lp = win.getAttributes(); 
        lp.x = -200;// x=0,y=0時,顯示位置是屏幕中心。  
        lp.y = 0 ; 
        lp.alpha = 0.6f;// 對話框的透明度  
        win.setAttributes(lp); 
         
        Button but = (Button) layout.findViewById(R.id.but_positive); 
        but.requestFocus(); 
        dialog.show(); 

public void showDialog(){
        LayoutInflater inflater = getLayoutInflater();
     View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));
     // 可指定對話框的背景色彩那些
     
     Dialog dialog = new AlertDialog.Builder(this)
     .setView(layout)
     .create();
     
     // 設置對話框的出現位置,借助於window對象
     Window win = dialog.getWindow();
     //win.setGravity(Gravity.CENTER);
     //win.setGravity(Gravity.BOTTOM);
     //win.setGravity(Gravity.TOP);
     win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 彈出對話框時,底部窗體,不變暗。
     
     WindowManager.LayoutParams lp = win.getAttributes();
     lp.x = -200;// x=0,y=0時,顯示位置是屏幕中心。
     lp.y = 0 ;
     lp.alpha = 0.6f;// 對話框的透明度
     win.setAttributes(lp);
     
     Button but = (Button) layout.findViewById(R.id.but_positive);
     but.requestFocus();
     dialog.show();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *