Dialog中Android应用中的使用方法
Dialog中Android应用中的使用方法?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联专业为企业提供遂溪网站建设、遂溪做网站、遂溪网站设计、遂溪网站制作等企业网站建设、网页设计与制作、遂溪企业网站模板建站服务,十载遂溪做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
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(); } });
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
分享标题:Dialog中Android应用中的使用方法
当前路径:http://pwwzsj.com/article/gcgpes.html