- 普通对话框
- 单选对话框
- 多选对话框
- 进度条对话框
- 底部弹出框
1.普通对话框
this 代表当前类 最终继承Context 相当于是子类
getApplicationContext:直接返回的是Context 相当于是父类
实际开发中就对话框特殊 对话框的上下文只能用this.
// [1]创建一个对话框实例
12. AlertDialog.Builder dialog = new Builder(this);
13. dialog.setTitle("警告");
14. dialog.setMessage("世界这么大你想去看看");
15. dialog.setPositiveButton("确定", new OnClickListener() {
16. @Override
17. public void onClick(DialogInterface dialog, int which) {
18.
19. System.out.println("点击了确定");
20. }
21. });
22. dialog.setNegativeButton("取消", new OnClickListener() {
23.
24. @Override
25. public void onClick(DialogInterface dialog, int which) {
26. System.out.println("点击了取消");
27. }
28. });
29. // 最后一步 和 土司一样 一定给我记得show出来
30. dialog.show();
2. 单选对话框
// [1]创建一个对话框实例
35. AlertDialog.Builder dialog = new Builder(this);
36. dialog.setTitle("请选择你喜欢的课程");
37.
38. final String items[] = { "Android", "ios", "javaee", "c++", "C" };
39. // [2]设置下面这个方法的参数就是单选
40. dialog.setSingleChoiceItems(items, -1, new OnClickListener() {
41. // 当点中一个条目执行这个方法 把点中条目的数据取出来
42. @Override
43. public void onClick(DialogInterface dialog, int which) {
44. String data = items[which];
45. Toast.makeText(getApplicationContext(), data, 1).show();
46. // 关闭对话框
47. dialog.dismiss();
48. }
49. });
50. // 最后一步 和 土司一样 一定给我记得show出来
51. dialog.show();
3. 多选对话框
// [1]创建一个对话框实例
57. AlertDialog.Builder dialog = new Builder(this);
58. dialog.setTitle("请选择你喜欢的课程");
59.
60. final String items[] = {"茄子","柿子","黄瓜","白菜","榴莲","苹果"};
61. final boolean checkedItems[]= {true,false,true,false,false,true};
62. //[2]让对话框变为多选对话框
63. dialog.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
64.
65. @Override
66. public void onClick(DialogInterface dialog, int which, boolean isChecked) {
67. }
68. });
69. //[3]添加确定按钮
70. dialog.setPositiveButton("确定", new OnClickListener() {
71.
72. @Override
73. public void onClick(DialogInterface dialog, int which) {
74. StringBuffer sb = new StringBuffer();
75. //把你选中的水果取出来
76. for (int i = 0; i < checkedItems.length; i++) {
77. if (checkedItems[i]) {
78. //把选中的水果取出来 数据在哪里存着就去哪里取
79. String fruit = items[i];
80. sb.append(fruit + " ");
81. }
82. }
83. Toast.makeText(getApplicationContext(), sb.toString(), 1).show();
84. //关闭对话框
85. dialog.dismiss();
86. }
87. });
88.
89. // 最后一步 和 土司一样 一定给我记得show出来
90. dialog.show();
4. 进度条对话框
//[1]创建进度条对话框
5. final ProgressDialog dialog = new ProgressDialog(this); //与进度相关控件可以在子线程更新ui
6. dialog.setMessage("正在玩命加载中");
7. //[2]改变一下对话框的样式
8. dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
9. //[3]最后一步show
10. dialog.show();
11. //[4]给进度条设置进度
12. //创建一个子线程
13. new Thread() {
14. public void run() {
15.
16. //[5]设置进度条最大值
17. dialog.setMax(100);
18. //[6]设置进度条当前进度
19. for (int i = 0; i <= 100; i++) {
20. //[7]睡眠50
21. SystemClock.sleep(50);
22. dialog.setProgress(i);
23. }
24. //[8]关闭对话框
25. dialog.dismiss();
26. }
27. }.start();
/**
* 弹出进度条对话框
*/
private void showProgressDialog() {
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条的样式,STYLE_HORIZONTAL:水平样式
progressDialog.setProgressDrawable(getResources().getDrawable(R.drawable.custom_progress_horizontal_tomoto));
progressDialog.setCancelable(false);//设置是否可以点击空白处消失
progressDialog.show();//显示操作
}
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffff"
android:centerColor="#ffffff"
android:centerY="0.75"
android:endColor="#ffffff"
android:angle="270" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#eb6100"
android:centerColor="#eb6100"
android:centerY="0.75"
android:endColor="#eb6100"
android:angle="270"/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#eb6100"
android:centerColor="#eb6100"
android:centerY="0.75"
android:endColor="#eb6100"
android:angle="270"/>
</shape>
</clip>
</item>
<!-- android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"-->
</layer-list>
5. 底部弹出框
dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
//填充对话框的布局
inflate = LayoutInflater.from(this).inflate(R.layout.item_dialog_pay, null);
// 初始化控件
TextView close = (TextView) inflate.findViewById(R.id.tv_supportpay_close);
LinearLayout weixin = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_weixin);
LinearLayout zhifubao = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_zhifubao);
close.setOnClickListener(this);
weixin.setOnClickListener(this);
zhifubao.setOnClickListener(this);
// 将布局设置给Dialog
dialog.setContentView(inflate);
// 获取当前Activity所在的窗体
Window dialogWindow = dialog.getWindow();
//最重要的一句话,一定要加上!要不然怎么设置都不行
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
// 设置Dialog从窗体底部弹出
dialogWindow.setGravity(Gravity.BOTTOM);
// 获得窗体的属性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
Display d = dialogWindow.getWindowManager().getDefaultDisplay();
//获取屏幕宽
lp.width = (int) (d.getWidth());
//宽度按屏幕大小的百分比设置,这里我设置的是全屏显示
lp.gravity = Gravity.BOTTOM;
if (lp.gravity == Gravity.BOTTOM)
lp.y = 20;
//如果是底部显示,则距离底部的距离是0
dialogWindow.setAttributes(lp);
dialog.show();//显示对话框
网友评论