首页 > 系统 > Android > 正文

Android Dialog详解及实例代码

2019-12-12 02:58:36
字体:
来源:转载
供稿:网友

 Android Dialog详解及实例代码

概述:

Android开发中最常用的就是Dialog类,除了自定义dialog布局,最多的就是用在弹出对话框、进度条、输入框、单选、复选框。

1、选择对话框:

AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("选择对话框"); dialog.setMessage("请选择确认或取消"); dialog.setCancelable(false); //设置按下返回键不能消失 dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {   @Override   public void onClick(DialogInterface dialog, int which) {     Toast.makeText(MainActivity.this, "您选择了确认", Toast.LENGTH_LONG).show();       }     });     dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         Toast.makeText(MainActivity.this, "您选择了取消", Toast.LENGTH_LONG).show();       }     }); dialog.show();//显示弹出窗口 

2、进度条

ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("显示进度条"); progressDialog.setMessage("加载中..."); progressDialog.setCancelable(true); progressDialog.show(); 

3、弹出输入框

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("弹出输入框"); builder.setMessage("输入内容"); final EditText editText = new EditText(this); builder.setView(editText); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {       @Override   public void onClick(DialogInterface dialog, int which) {     Toast.makeText(MainActivity.this, "您输入了" + editText.getText().toString(), Toast.LENGTH_SHORT).show();       }   }); builder.setNegativeButton("取消", null); builder.show(); 

4、弹出单选框

final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("弹出单选框"); final String[] choice = new String[]{"Java", "C++", "PHP"}; builder.setSingleChoiceItems(choice, 0,   new DialogInterface.OnClickListener() {     @Override     public void onClick(DialogInterface dialog, int which) {       Toast.makeText(MainActivity.this, "您选择了" + choice[which], Toast.LENGTH_LONG).show();       dialog.dismiss(); // 让窗口消失     }   }); builder.create().show(); 

5、弹出复选框

final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("弹出多选框"); final String[] choice = new String[]{"Android", "IOS", "Windos Phone"}; // 选择项 final boolean[] isCheck = new boolean[choice.length]; // 是否被选中 builder.setMultiChoiceItems(choice, null,   new DialogInterface.OnMultiChoiceClickListener() {     @Override     public void onClick(DialogInterface dialog, int which, boolean isChecked) {       if (isChecked == true)         isCheck[which] = true;       else if(isChecked == false)         isCheck[which] = false;      }   }); builder.setNegativeButton("取消", null); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {   @Override   public void onClick(DialogInterface dialog, int which) {     String isSelected = "您选择了";     for (int i = 0; i < choice.length; i++) {       if (isCheck[i] == true) {         isSelected += choice[i]+","; // 将选择的内容加上       }     }     Toast.makeText(MainActivity.this, isSelected, Toast.LENGTH_LONG).show();    } }); builder.create(); builder.show(); 


6、自定义布局的Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);  final AlertDialog dialog = builder.create();  View view = View.inflate(this, R.layout.personal_modifyhead_dialog, null);  dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题  dialog.show();   AutoRelativeLayout rl_photograph = (AutoRelativeLayout) view.findViewById(R.id.rl_photograph);  rl_photograph.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {      presenter.modiftHeadFromPhotograph();      dialog.dismiss();    }  });   AutoRelativeLayout rl_album = (AutoRelativeLayout) view.findViewById(R.id.rl_album);  rl_album.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {      presenter.modifyHeadFromAlbum();      dialog.dismiss();    }  }); 

上面就是日常开发中常用的一些,在此记录,以便日后查询。

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表