分享

AlertDialog的使用(它也可以自定义)

 一戒745zia4sgs 2016-01-14

 在Android中经常会使用到对话框Dialog。下面是实现一个基本的Dialog的代码:

Java代码  收藏代码
  1. private void showDialog(){  
  2.           
  3.         LayoutInflater layoutInflater = this.getLayoutInflater();  
  4.         View customDialog = layoutInflater.inflate(R.layout.dialog_signin, null);  
  5.         Dialog dialog = new Dialog(this);  
  6.         dialog.setTitle("I am a Dialog");  
  7.         dialog.setContentView(customDialog);  
  8.         dialog.show();  
  9.     }  

 你可以看到,首先你需要自己设计好这个对话框的布局,然后将它Inflate出来(当然,使用Java代码也是可以的),然后,通过setContentView方法加载进去,最后显示。而通常我们的使用对话框的时候都有一定的模式,比如,显示一些简短的内容,底部有确定或取消按钮的。这样写一个对话框太麻烦了,因此,系统提供了AlertDialog,AlertDialog继承自Dialog。我们先看看代码:

 

Java代码  收藏代码
  1. private void showCommonAlertDialog() {  
  2.   
  3.         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
  4.         AlertDialog dialog = builder.create();  
  5.         dialog.setTitle("AlertDialog");  
  6.         dialog.setIcon(R.drawable.ic_launcher);  
  7.         dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",  
  8.                 new DialogInterface.OnClickListener() {  
  9.   
  10.                     @Override  
  11.                     public void onClick(DialogInterface dialog, int which) {  
  12.   
  13.                         Log.e(TAG, "确定按钮:" + which);  
  14.                     }  
  15.                 });  
  16.         dialog.show();  
  17.     }  

 由于AlertDialog是protected方法,也就是你不能实例化它,而是需要通过它的内部类Builder来获得一个builder实例,通过这个实例来create一个AlertDialog,然后你就可以使用这个AlertDialog来进行各种设置了,比如你可以setButton,这个Button就默认位于对话框的底部了,方便我们使用。但是这样使用起来还是麻烦,也会你也会问为什么sdk不让我们直接实例化AlertDialog,看看下面的代码你就明白了:

 

Java代码  收藏代码
  1. private void showCommonAlertDialog2() {  
  2.   
  3.         new AlertDialog.Builder(MainActivity.this)  
  4.                 .setTitle("AlertDialog")  
  5.                 .setIcon(R.drawable.ic_launcher)  
  6.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  7.   
  8.                     @Override  
  9.                     public void onClick(DialogInterface dialog, int which) {  
  10.   
  11.                         Log.e(TAG, "showCommonAlertDialog clicked");  
  12.                     }  
  13.                 }).show();  
  14.     }  

使用Builder类进行各种设置的时候,它直接返回一个builder,可以直接进行一系列的设置。这样使用起来,较上面又更简单了。这种方法也是个人比较推荐的方法。

效果如图:

普通Dialog普通AlertDialog

 还有一个齐齐自己试验的效果,就多了一个setview(containtView)而已,可以看出来,containtView只能填充到除题目和确定取消button之外的中间部分

 

除了这种普通的对话框,AlertDialog还提供了一系列已经定义好的对话框,很方便使用。

比如下面要介绍的:列表对话框,单选对话框,多选对话框,进度条对话框,自定义对话框。

 

1.列表对话框

Java代码  收藏代码
  1. private void showListDialog() {  
  2.   
  3.         final String[] fruit = new String[] { "apple""orange""banana",  
  4.                 "black berry""lichee""pear""strawberry" };  
  5.   
  6.         new AlertDialog.Builder(MainActivity.this).setTitle("fruit")  
  7.                 .setItems(fruit, new DialogInterface.OnClickListener() {  
  8.   
  9.                     @Override  
  10.                     public void onClick(DialogInterface dialog, int which) {  
  11.   
  12.                         new AlertDialog.Builder(MainActivity.this).setMessage(  
  13.                                 "您选择了:" + which + ":" + fruit[which]).show();  
  14.                     }  
  15.                 }).show();  
  16.     }  

列表对话框 

 

2.单选对话框

Java代码  收藏代码
  1. private void showSingleChoiceListDialog() {  
  2.   
  3.         final String[] fruit = new String[] { "apple""orange""banana",  
  4.                 "black berry""lichee""pear""strawberry" };  
  5.   
  6.         // 按钮的顺序从左到右是setNegativeButton,setNeutralButton,setPositiveButton  
  7.         new AlertDialog.Builder(MainActivity.this)  
  8.                 .setTitle("fruit")  
  9.                 .setSingleChoiceItems(fruit, -1,  
  10.                         new DialogInterface.OnClickListener() {  
  11.   
  12.                             @Override  
  13.                             public void onClick(DialogInterface dialog,  
  14.                                     int which) {  
  15.   
  16.                                 Log.e(TAG, "你选择了:" + which);  
  17.                             }  
  18.                         })  
  19.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  20.   
  21.                     @Override  
  22.                     public void onClick(DialogInterface dialog, int which) {  
  23.   
  24.                         Log.e(TAG, "取消按钮:" + which);  
  25.                     }  
  26.                 })  
  27.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  28.   
  29.                     @Override  
  30.                     public void onClick(DialogInterface dialog, int which) {  
  31.   
  32.                         Log.e(TAG, "确定按钮:" + which);  
  33.                     }  
  34.                 })  
  35.                 .setNeutralButton("关闭"new DialogInterface.OnClickListener() {  
  36.   
  37.                     @Override  
  38.                     public void onClick(DialogInterface dialog, int which) {  
  39.   
  40.                         Log.e(TAG, "关闭按钮:" + which);  
  41.                     }  
  42.                 }).show();  
  43.     }  

 单选对话框

 

3.多选对话框

Java代码  收藏代码
  1. private void showMulChoiceListDialog() {  
  2.   
  3.         final List<Integer> selectedFruit = new ArrayList<Integer>();  
  4.         selectedFruit.add(0);  
  5.         selectedFruit.add(2);  
  6.         selectedFruit.add(6);  
  7.         final String[] fruit = new String[] { "apple""orange""banana",  
  8.                 "black berry""lichee""pear""strawberry" };  
  9.         AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)  
  10.                 .setIcon(R.drawable.ic_launcher)  
  11.                 .setTitle("fruit")  
  12.                 .setMultiChoiceItems(  
  13.                         fruit,  
  14.                         new boolean[] { truefalsetruefalsefalsefalse,  
  15.                                 true },  
  16.                         new DialogInterface.OnMultiChoiceClickListener() {  
  17.   
  18.                             @Override  
  19.                             public void onClick(DialogInterface dialog,  
  20.                                     int which, boolean isChecked) {  
  21.   
  22.                                 if (isChecked) {  
  23.   
  24.                                     selectedFruit.add(which);  
  25.                                 } else if (selectedFruit.contains(which)) {  
  26.   
  27.                                     selectedFruit.remove(Integer.valueOf(which));  
  28.                                 }  
  29.                             }  
  30.                         })  
  31.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  32.   
  33.                     @Override  
  34.                     public void onClick(DialogInterface dialog, int which) {  
  35.   
  36.                         Log.e("onclick""确定");  
  37.   
  38.                         StringBuilder sb = new StringBuilder();  
  39.                         int fruitSize = selectedFruit.size();  
  40.                         for (int i = 0; i < fruitSize; i++) {  
  41.   
  42.                             sb.append(fruit[selectedFruit.get(i)]);  
  43.                         }  
  44.                         Toast.makeText(MainActivity.this, sb, Toast.LENGTH_LONG)  
  45.                                 .show();  
  46.                     }  
  47.                 }).setNegativeButton("取消"null).create();  
  48.         alertDialog.show();  
  49.     }  

 多选对话框

 

4.进度条对话框

Java代码  收藏代码
  1. private void showProgressDialog() {  
  2.   
  3.         ProgressDialog progressDialog = new ProgressDialog(this);  
  4.         progressDialog.setIcon(R.drawable.ic_launcher);  
  5.         progressDialog.setTitle("正在处理数据...");  
  6.         progressDialog.setMessage("请骚等...");  
  7.         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  8.         progressDialog.setMax(100);  
  9.         progressDialog.setProgress(30);  
  10.         progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "暂停",  
  11.                 new DialogInterface.OnClickListener() {  
  12.   
  13.                     @Override  
  14.                     public void onClick(DialogInterface dialog, int which) {  
  15.   
  16.                         Log.e(TAG, "按钮被点击:" + which);  
  17.                     }  
  18.                 });  
  19.         progressDialog.show();  
  20.     }  

 进度条对话框

 

 5.自定义对话框

Java代码  收藏代码
  1. private void showCustomDialog() {  
  2.   
  3.         LayoutInflater layoutInflater = this.getLayoutInflater();  
  4.         View customDialog = layoutInflater  
  5.                 .inflate(R.layout.dialog_signin, null);  
  6.         new AlertDialog.Builder(this).setView(customDialog)  
  7.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  8.   
  9.                     @Override  
  10.                     public void onClick(DialogInterface dialog, int which) {  
  11.                         // TODO Auto-generated method stub  
  12.                         Log.e(TAG, "确定:" + which);  
  13.                     }  
  14.                 })  
  15.                 .setNeutralButton("取消"new DialogInterface.OnClickListener() {  
  16.   
  17.                     @Override  
  18.                     public void onClick(DialogInterface dialog, int which) {  
  19.                         // TODO Auto-generated method stub  
  20.                         Log.e(TAG, "取消:" + which);  
  21.                     }  
  22.                 }).create().show();  
  23.     }  

自定义AlertDialog 

 

 上面这些就是一些系统默认的对话框。你还可以对这些对话框设置透明度和设置对话框的位置:

 

1.设置带有透明度的对话框

Java代码  收藏代码
  1. private void showTransDialog() {  
  2.   
  3.         AlertDialog alertDialog = new AlertDialog.Builder(this)  
  4.                 .setMessage("透明对话框")  
  5.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  6.   
  7.                     @Override  
  8.                     public void onClick(DialogInterface dialog, int which) {  
  9.   
  10.                         Log.e(TAG, "确定按钮:" + which);  
  11.                     }  
  12.                 }).create();  
  13.         Window window = alertDialog.getWindow();  
  14.         WindowManager.LayoutParams lp = window.getAttributes();  
  15.         lp.alpha = 0.8f;// 这里设置透明度  
  16.         window.setAttributes(lp);  
  17.         alertDialog.show();  
  18.     }  

 半透明对话框

 

2.设置对话框的位置

Java代码  收藏代码
  1. private void showCustomLocationDialog() {  
  2.   
  3.         AlertDialog alertDialog = new AlertDialog.Builder(this)  
  4.                 .setMessage("自定义位置的对话框")  
  5.                 .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  6.   
  7.                     @Override  
  8.                     public void onClick(DialogInterface dialog, int which) {  
  9.   
  10.                         Log.e(TAG, "确定按钮:" + which);  
  11.                     }  
  12.                 }).create();  
  13.         Window window = alertDialog.getWindow();  
  14.         window.setGravity(Gravity.LEFT | Gravity.BOTTOM);// 左下角位置的对话框  
  15.         alertDialog.show();  
  16.     }  

 自定义位置对话框

 

 

 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约