Android对话框
- 警告对话框 AlertDialog: 一个可以有0到3个按钮, 一个单选框或复选框的列表的对话框. 警告对话框可以创建大多数的交互界面, 是推荐的类型。
- 进度对话框 ProgressDialog: 显示一个进度环或者一个进度条. 由于它是AlertDialog的扩展, 所以它也支持按钮。
- 日期选择对话框 DatePickerDialog: 让用户选择一个日期。
- 时间选择对话框 TimePickerDialog: 让用户选择一个时间。
1.比较正常的AlertDialog的创建和使用(图一与图二)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonNormal = (Button) findViewById(R.id.button_normal);
buttonNormal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showNormalDialog();
}
});
}
private void showNormalDialog(){
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog")
normalDialog.setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
normalDialog.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
normalDialog.show();
}
}
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("按钮1",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
normalDialog.setNeutralButton("按钮2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
normalDialog.setNegativeButton("按钮3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
normalDialog.show();
}
2.列表Dialog(图3)
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("我是一个列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
"你点击了" + items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
3.单选Dialog(图4)
int yourChoice;
private void showSingleChoiceDialog(){
final String[] items = { "我是1","我是2","我是3","我是4" };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("我是一个单选Dialog");
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
Toast.makeText(MainActivity.this,
"你选择了" + items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}
4.多选Dialog(图5)
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
final boolean initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
multiChoiceDialog.setTitle("我是一个多选Dialog");
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
} else {
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = "";
for (int i = 0; i < size; i++) {
str += items[yourChoices.get(i)] + " ";
}
Toast.makeText(MainActivity.this,
"你选中了" + str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
}
5.等待Dialog(图6)
private void showWaitingDialog() {
ProgressDialog waitingDialog=
new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("我是一个等待Dialog");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
}
6.进度条Dialog(图7)
private void showProgressDialog() {
final int MAX_PROGRESS = 100;
final ProgressDialog progressDialog =
new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle("我是一个进度条Dialog");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
int progress= 0;
while (progress < MAX_PROGRESS){
try {
Thread.sleep(100);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e){
e.printStackTrace();
}
}
progressDialog.cancel();
}
}).start();
}
7.编辑Dialog(图8)
private void showInputDialog() {
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity.this);
inputDialog.setTitle("我是一个输入Dialog").setView(editText);
inputDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
}
8.自定义Dialog(图9)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
private void showCustomizeDialog() {
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity.this)
final View dialogView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null)
customizeDialog.setTitle("我是一个自定义Dialog")
customizeDialog.setView(dialogView)
customizeDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 获取EditView中的输入内容
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text)
Toast.makeText(MainActivity.this,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show()
}
})
customizeDialog.show()
}
复写回调函数
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this){
@Override
public AlertDialog create() {
items[0] = "我是No.1";
return super.create();
}
@Override
public AlertDialog show() {
items[1] = "我是No.2";
return super.show();
}
};
listDialog.setTitle("我是一个列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
Toast.makeText(getApplicationContext(),
"Dialog被销毁了",
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}