在对话框中,上下文只能使用this,不能使用getApplicationContext()
[1]普通对话框
实现:
//点击按钮,弹出普通对话框
public void click1(View v){
//通过builder构建器来构造
AlertDialog.Builder builder=new AlertDialog.Builder(this);//在对话框中,上下文只能使用this
builder.setTitle("警告!");
builder.setMessage("世界上最遥远的距离就是没有网络!");
//设置按钮及监听
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了确定按钮!");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("点击了取消按钮!");
}
});
//记得要show出来
builder.show();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
[2]单选对话框
实现:
//点击按钮,弹出单选对话框
public void click2(View v){
//通过builder构建器来构造
AlertDialog.Builder builder=new AlertDialog.Builder(this);//在对话框中,上下文只能使用this
//定义展示的内容
builder.setTitle("请选择你最喜欢的水果!");
final String itemId[]={"香蕉","哈密瓜","西瓜","柚子"};
builder.setSingleChoiceItems(itemId, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取出获取的数据
Toast.makeText(getApplicationContext(), itemId[which], Toast.LENGTH_LONG).show();
//关闭对话框
dialog.dismiss();
}
});
//记得要show出来
builder.show();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
[3]多选对话框
实现:
//点击按钮,弹出多选对话框
public void click3(View v){
//通过builder构建器来构造
AlertDialog.Builder builder=new AlertDialog.Builder(this);//在对话框中,上下文只能使用this
builder.setTitle("请选择你学习的语言!");
//定义展示的内容
final String itemId[]={"C","C++","Java","Android","C#","Html"};
//默认的选择情况
final boolean checkedItems[]={false,false,true,false,false,false};
//多选框的设置
builder.setMultiChoiceItems(itemId, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
//设置选择时的响应事件
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//设置响应事件
@Override
public void onClick(DialogInterface dialog, int which) {
//读取选择的数据
StringBuffer sb = new StringBuffer();
for (int i = 0; i < itemId.length; i++) {
if (checkedItems[i]) {
String item = itemId[i] + " ";
sb.append(item);
}
}
//显示数据
Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();
//关闭对话框
dialog.dismiss();
}
});
//记得要show出来
builder.show();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
[4]进度条对话框
注意:1.与进度有关的控件可以在子线程中可以更新UI
实现:
//点击按钮,弹出进度条对话框
public void click4(View v){
final ProgressDialog progressDialog = new ProgressDialog(this);
//设置标题
progressDialog.setTitle("玩命加载中...");
//设置对话框的样式为水平
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
//模拟进度条的进度
new Thread(){
@Override
//与进度有关的控件可以在子线程中可以更新UI
public void run() {
//设置进度条的最大值
progressDialog.setMax(100);
for (int i=0; i<100;i++) {
//设置当前进度
progressDialog.setProgress(i);
//设置等待事件
SystemClock.sleep(50);
}
//关闭对话框
progressDialog.dismiss();
}
}.start();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26