在Android中经常会使用到对话框Dialog。下面是实现一个基本的Dialog的代码:
private void showDialog(){
LayoutInflater layoutInflater = this.getLayoutInflater();
View customDialog = layoutInflater.inflate(R.layout.dialog_signin, null);
Dialog dialog = new Dialog(this);
dialog.setTitle("I am a Dialog");
dialog.setContentView(customDialog);
dialog.show();
}
你可以看到,首先你需要自己设计好这个对话框的布局,然后将它Inflate出来(当然,使用Java代码也是可以的),然后,通过setContentView方法加载进去,最后显示。而通常我们的使用对话框的时候都有一定的模式,比如,显示一些简短的内容,底部有确定或取消按钮的。这样写一个对话框太麻烦了,因此,系统提供了AlertDialog,AlertDialog继承自Dialog。我们先看看代码:
private void showCommonAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog dialog = builder.create();
dialog.setTitle("AlertDialog");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "确定按钮:" + which);
}
});
dialog.show();
}
由于AlertDialog是protected方法,也就是你不能实例化它,而是需要通过它的内部类Builder来获得一个builder实例,通过这个实例来create一个AlertDialog,然后你就可以使用这个AlertDialog来进行各种设置了,比如你可以setButton,这个Button就默认位于对话框的底部了,方便我们使用。但是这样使用起来还是麻烦,也会你也会问为什么sdk不让我们直接实例化AlertDialog,看看下面的代码你就明白了:
private void showCommonAlertDialog2() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("AlertDialog")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "showCommonAlertDialog clicked");
}
}).show();
}
使用Builder类进行各种设置的时候,它直接返回一个builder,可以直接进行一系列的设置。这样使用起来,较上面又更简单了。这种方法也是个人比较推荐的方法。
效果如图:
除了这种普通的对话框,AlertDialog还提供了一系列已经定义好的对话框,很方便使用。
比如下面要介绍的:列表对话框,单选对话框,多选对话框,进度条对话框,自定义对话框。
1.列表对话框
private void showListDialog() {
final String[] fruit = new String[] { "apple", "orange", "banana",
"black berry", "lichee", "pear", "strawberry" };
new AlertDialog.Builder(MainActivity.this).setTitle("fruit")
.setItems(fruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(MainActivity.this).setMessage(
"您选择了:" + which + ":" + fruit[which]).show();
}
}).show();
}
2.单选对话框
private void showSingleChoiceListDialog() {
final String[] fruit = new String[] { "apple", "orange", "banana",
"black berry", "lichee", "pear", "strawberry" };
// 按钮的顺序从左到右是setNegativeButton,setNeutralButton,setPositiveButton
new AlertDialog.Builder(MainActivity.this)
.setTitle("fruit")
.setSingleChoiceItems(fruit, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Log.e(TAG, "你选择了:" + which);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "取消按钮:" + which);
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "确定按钮:" + which);
}
})
.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "关闭按钮:" + which);
}
}).show();
}
3.多选对话框
private void showMulChoiceListDialog() {
final List<Integer> selectedFruit = new ArrayList<Integer>();
selectedFruit.add(0);
selectedFruit.add(2);
selectedFruit.add(6);
final String[] fruit = new String[] { "apple", "orange", "banana",
"black berry", "lichee", "pear", "strawberry" };
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("fruit")
.setMultiChoiceItems(
fruit,
new boolean[] { true, false, true, false, false, false,
true },
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if (isChecked) {
selectedFruit.add(which);
} else if (selectedFruit.contains(which)) {
selectedFruit.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e("onclick", "确定");
StringBuilder sb = new StringBuilder();
int fruitSize = selectedFruit.size();
for (int i = 0; i < fruitSize; i++) {
sb.append(fruit[selectedFruit.get(i)]);
}
Toast.makeText(MainActivity.this, sb, Toast.LENGTH_LONG)
.show();
}
}).setNegativeButton("取消", null).create();
alertDialog.show();
}
4.进度条对话框
private void showProgressDialog() {
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("正在处理数据...");
progressDialog.setMessage("请骚等...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setProgress(30);
progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "暂停",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "按钮被点击:" + which);
}
});
progressDialog.show();
}
5.自定义对话框
private void showCustomDialog() {
LayoutInflater layoutInflater = this.getLayoutInflater();
View customDialog = layoutInflater
.inflate(R.layout.dialog_signin, null);
new AlertDialog.Builder(this).setView(customDialog)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.e(TAG, "确定:" + which);
}
})
.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.e(TAG, "取消:" + which);
}
}).create().show();
}
上面这些就是一些系统默认的对话框。你还可以对这些对话框设置透明度和设置对话框的位置:
1.设置带有透明度的对话框
private void showTransDialog() {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setMessage("透明对话框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "确定按钮:" + which);
}
}).create();
Window window = alertDialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.alpha = 0.8f;// 这里设置透明度
window.setAttributes(lp);
alertDialog.show();
}
2.设置对话框的位置
private void showCustomLocationDialog() {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setMessage("自定义位置的对话框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.e(TAG, "确定按钮:" + which);
}
}).create();
Window window = alertDialog.getWindow();
window.setGravity(Gravity.LEFT | Gravity.BOTTOM);// 左下角位置的对话框
alertDialog.show();
}