尝试使用Android对话框技术创建普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框,
时间: 2023-06-10 10:07:11 浏览: 158
好的,以下是使用Android对话框技术创建不同类型对话框的示例代码:
## 普通对话框
```java
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("这是一个普通对话框示例");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击确定按钮后的操作
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮后的操作
}
});
AlertDialog dialog = builder.create();
dialog.show();
```
## 单选对话框
```java
final String[] items = {"选项1", "选项2", "选项3", "选项4"};
int checkedItem = 0; // 默认选中第一个
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框");
builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击选项后的操作
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击确定按钮后的操作
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮后的操作
}
});
AlertDialog dialog = builder.create();
dialog.show();
```
## 多选对话框
```java
final String[] items = {"选项1", "选项2", "选项3", "选项4"};
boolean[] checkedItems = {false, false, false, false}; // 默认全部未选中
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选对话框");
builder.setMultiChoiceItems(items, 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) {
// 点击确定按钮后的操作
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮后的操作
}
});
AlertDialog dialog = builder.create();
dialog.show();
```
## 进度条对话框
```java
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("进度条对话框");
progressDialog.setMessage("正在加载,请稍后...");
progressDialog.setCancelable(false); // 设置不可取消
progressDialog.show();
```
## 消息对话框
```java
Toast.makeText(this, "这是一个消息对话框示例", Toast.LENGTH_SHORT).show();
```
阅读全文