Android学习-各种对话框

在android的编程中,对话框的用处是非常多的,然而,有时对对话框的需求也是不一样的,那么现在,就总结对话框的种类,以及事件的处理形势。

1、简单的对话框

[java]  view plain copy print ?
  1. public void showDialog(){  
  2.    Dialog dialog = new AlertDialog.Builder(this)  
  3.         .setTitle("标题")  
  4.         .setMessage("消息")  
  5.         .setPositiveButton("好"new DialogInterface.OnClickListener() {  
  6.             @Override  
  7.             public void onClick(DialogInterface dialog, int which) {  
  8.                   
  9.             }  
  10.         })  
  11.         .setNegativeButton("不好"new DialogInterface.OnClickListener() {  
  12.             @Override  
  13.             public void onClick(DialogInterface dialog, int which) {  
  14.                   
  15.             }  
  16.         }).create();  
  17.     dialog.show();  
  18. }  


2、带编辑框的对话框

[java]  view plain copy print ?
  1. public void showDialog(){  
  2.     EditText et = new EditText(this);// 单独出来,方便取用户输入的数据。  
  3.     Dialog dialog = new AlertDialog.Builder(this)    
  4.         .setTitle("请输入")    
  5.         .setIcon(android.R.drawable.ic_dialog_info)    
  6.         .setView(et)    
  7.         .setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  8.             @Override  
  9.             public void onClick(DialogInterface dialog, int which) {  
  10.                   
  11.             }  
  12.               
  13.         })    
  14.         .setNegativeButton("取消"new DialogInterface.OnClickListener(){  
  15.             @Override  
  16.             public void onClick(DialogInterface dialog, int which) {  
  17.                   
  18.             }})    
  19.         .create();  
  20.     dialog.show();  
  21. }  


3、含单选钮的对话框

[java]  view plain copy print ?
  1. public void showDialog(){  
  2.     final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;  
  3.     // 单独列出来方便之后取数据  
  4.         Dialog dialog = new AlertDialog.Builder(this)  
  5.         .setTitle("单选框,请选择")  
  6.         .setSingleChoiceItems(str, 3new DialogInterface.OnClickListener() {  
  7.             // 3 代表默认选中str的index=3的那个单选框  
  8.             @Override  
  9.             public void onClick(DialogInterface dialog, int which) {  
  10.                 Log.e("DIYDialogActivity""showDialog->you select " + str[which] ) ;  
  11.             }  
  12.         })  
  13.         .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  14.             @Override  
  15.             public void onClick(DialogInterface dialog, int which) {  
  16.                   
  17.             }  
  18.         })  
  19.        .create();  
  20.     dialog.show();  
  21. }  


4、含多选框的对话框

[java]  view plain copy print ?
  1. public void showDialog(){  
  2.         final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;// 单独列出来方便之后取数据  
  3.         boolean[] bstr = new boolean[]{true,false,false,false,false,false,false};  
  4.         Dialog dialog = new AlertDialog.Builder(this)  
  5.         .setTitle("多选框,请选择")  
  6.         .setMultiChoiceItems(str, bstr, new DialogInterface.OnMultiChoiceClickListener() {  
  7.             @Override  
  8.             public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
  9.                 Log.e("DIYDialogActivity""showDialog->" +  
  10.                         "you select " + str[which]+ "vaule = " +new Boolean(isChecked).toString() ) ;  
  11.             }  
  12.         })  
  13.         .setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  14.             @Override  
  15.             public void onClick(DialogInterface dialog, int which) {  
  16.                   
  17.             }  
  18.         }).create();  
  19.         dialog.show();  
  20. }  


5、含列表的对话框

[java]  view plain copy print ?
  1. public void showDialog(){  
  2.         final String[] str = new String[]{"第一部","第二部","第二部","第三部","第四部","第五部","第六部"} ;  
  3.         // 单独列出来方便之后取数据  
  4.         Dialog dialog = new AlertDialog.Builder(this)  
  5.         .setTitle("列表框,供选择")  
  6.         .setItems(str, new DialogInterface.OnClickListener() {  
  7.             @Override  
  8.             public void onClick(DialogInterface dialog, int which) {  
  9.                 Log.e("DIYDialogActivity""showDialog---------------------you select " + which);  
  10.             }  
  11.         })  
  12.         .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  13.             @Override  
  14.             public void onClick(DialogInterface dialog, int which) {  
  15.                   
  16.             }  
  17.         })  
  18.         .create();  
  19.         dialog.show();  
  20. }  

6、自定义的对话框

[java]  view plain copy print ?
  1. public void showDialog(){  
  2.         LayoutInflater inflater = getLayoutInflater();  
  3.         View layout = inflater.inflate(R.layout.dialog_showmsg, (ViewGroup) findViewById(R.id.view_dialog));   
  4.         // 可指定对话框的背景色彩那些  
  5.           
  6.         Dialog dialog = new AlertDialog.Builder(this)  
  7.         .setView(layout)  
  8.         .create();  
  9.           
  10.         // 设置对话框的出现位置,借助于window对象  
  11.         Window win = dialog.getWindow();  
  12.         //win.setGravity(Gravity.CENTER);  
  13.         //win.setGravity(Gravity.BOTTOM);  
  14.         //win.setGravity(Gravity.TOP);  
  15.         win.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 弹出对话框时,底部窗体,不变暗。  
  16.           
  17.         WindowManager.LayoutParams lp = win.getAttributes();  
  18.         lp.x = -200;// x=0,y=0时,显示位置是屏幕中心。  
  19.         lp.y = 0 ;  
  20.         lp.alpha = 0.6f;// 对话框的透明度  
  21.         win.setAttributes(lp);  
  22.           
  23.         Button but = (Button) layout.findViewById(R.id.but_positive);  
  24.         but.requestFocus();  
  25.         dialog.show();  
  26. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值