Android學習-各種對話方塊

來源:互聯網
上載者:User

在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();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.