Android对话框
普通Dialog(图1与图2)
列表Dialog(图3)
单选Dialog(图4)
多选Dialog(图5)
等待Dialog(图6)
进度条Dialog(图7)
编辑Dialog(图8)
自定义Dialog(图9)
1.普通对话框
最简单的
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.mipmap.icon)//设置标题的图片
.setTitle("我是对话框")//设置对话框的标题
.setMessage("我是对话框的内容")//设置对话框的内容
//设置对话框的按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定的按钮", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).create();
dialog.show();
AlertDialog类中有一个静态内部类Builder。所以可以看出对话框使用了一个建造者模式在调用函数的时候就可以一直直点点点链式调用。 需要注意的是:NegativeButton这个按钮是在对话框的左边,PositiveButton在对话框的右边;如果你还想再加一个按钮也是可以的只需要在调用.setNeutralButton(“第三个按钮”,listener)即可。
图一
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(){
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
*/
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) {
//...To-do
}
});
normalDialog.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
// 显示
normalDialog.show();
}
}
图二
/* @setNeutralButton 设置中间的按钮
* 若只需一个按钮,仅设置 setPositiveButton 即可
*/
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) {
// ...To-do
}
});
normalDialog.setNeutralButton("按钮2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
normalDialog.setNegativeButton("按钮3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ...To-do
}
});
// 创建实例并显示
normalDialog.show();
}
2. 列表对话框
当给用户的选择就那么几条路的时候,就可在对话框上放置一个列表供用户自己选择
图三
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) {
// which 下标从0开始
// ...To-do
Toast.makeText(MainActivity.this,
"你点击了" + items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
------------------------------------------------------------------------------
//案列二
final String items[] = {"我是Item一", "我是Item二", "我是Item三", "我是Item四"};
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.mipmap.icon)//设置标题的图片
.setTitle("列表对话框")//设置对话框的标题
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.show();
3.单选对话框
图四
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");
// 第二个参数是默认选项,此处设置为0
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();
}
-----------------------------------------------------------------------------
//案例二
final String items[] = {"我是Item一", "我是Item二", "我是Item三", "我是Item四"};
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.mipmap.icon)//设置标题的图片
.setTitle("单选列表对话框")//设置对话框的标题
//setSingleChoiceItems第一个参数:设置单选的资源数组;第二个参数:设置默认选中哪一项。
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.show();
4 .多选对话框
图五
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
// 设置默认选中的选项,全为false默认均未选中
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();
}
---------------------------------------------------------------------
案例二
final String items[] = {"我是Item一", "我是Item二", "我是Item三", "我是Item四"};
final boolean checkedItems[] = {true, false, true, false};
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.mipmap.icon)//设置标题的图片
.setTitle("多选对话框")//设置对话框的标题
//setMultiChoiceItems(items, checkedItems, listener)//第一个参数:设置单选的资源;第二个参数:设置默认选中哪几项(数组);
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
Toast.makeText(MainActivity.this, "选中了" + i, Toast.LENGTH_SHORT).show();
}
}
dialog.dismiss();
}
}).create();
dialog.show();
5.等待对话框
图六
private void showWaitingDialog() {
/* 等待Dialog具有屏蔽其他控件的交互能力
* @setCancelable 为使屏幕不可点击,设置为不可取消(false)
* 下载等事件完成后,主动调用函数关闭该Dialog
*/
ProgressDialog waitingDialog=
new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("我是一个等待Dialog");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
}
------------------------------------------------------------------------
//案例二:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("正在加载中");
dialog.show();
6.进度条对话框
图七
private void showProgressDialog() {
/* @setProgress 设置初始进度
* @setProgressStyle 设置样式(水平进度条)
* @setMax 设置进度最大值
*/
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();
/* 模拟进度增加的过程
* 新开一个线程,每个100ms,进度增加1
*/
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();
}
--------------------------------------------------------------------------------
//案例二
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("正在加载中");
dialog.setMax(100);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
dialog.setProgress(progress += 5);
if (progress == 100) {
timer.cancel();
}
}
}, 0, 1000);
dialog.show();
7.编辑Dialog
图八
private void showInputDialog() {
/*@setView 装入一个EditView
*/
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:自定义对话框
图九
(1)方法一
//1.自定义View
<!-- res/layout/dialog_customize.xml-->
<!-- 自定义View -->
<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>
//2.使用setView将自定的view装入AlertDialog,并设置样式
private void showCustomizeDialog() {
/* @setView 装入自定义View ==> R.layout.dialog_customize
* 由于dialog_customize.xml只放置了一个EditView,因此和图8一样
* dialog_customize.xml可自定义更复杂的View
*/
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();
}
//3.复写回调函数
/* 复写Builder的create和show函数,可以在Dialog显示前实现必要设置
* 例如初始化列表、默认选项等
* @create 第一次创建时调用
* @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) {
// ...To-do
}
});
/* @setOnDismissListener Dialog销毁时调用
* @setOnCancelListener Dialog关闭时调用
*/
listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
Toast.makeText(getApplicationContext(),
"Dialog被销毁了",
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
(2)方法二
首先我们需要自定义Dialog的style,让他自己本有的东西全部透明,然后在设置我们自己的内容就可以达到完全自定义的效果了。
<!--对话框的样式-->
<style name="NormalDialogStyle">
<!--对话框背景 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!--边框 -->
<item name="android:windowFrame">@null</item>
<!--没有标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 是否浮现在Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!--背景透明 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 是否有覆盖 -->
<item name="android:windowContentOverlay">@null</item>
<!--进出的显示动画 -->
<item name="android:windowAnimationStyle">@style/normalDialogAnim</item>
<!--背景变暗-->
<item name="android:backgroundDimEnabled">true</item>
</style>
<!--对话框动画-->
<style name="normalDialogAnim" parent="android:Animation">
<item name="@android:windowEnterAnimation">@anim/normal_dialog_enter</item>
<item name="@android:windowExitAnimation">@anim/normal_dialog_exit</item>
</style>
接下来就可以为对话框设置我们自定义的style了.
/**
* 自定义对话框
*/
private void customDialog() {
final Dialog dialog = new Dialog(this, R.style.NormalDialogStyle);
View view = View.inflate(this, R.layout.dialog_normal, null);
TextView cancel = (TextView) view.findViewById(R.id.cancel);
TextView confirm = (TextView) view.findViewById(R.id.confirm);
dialog.setContentView(view);
//使得点击对话框外部不消失对话框
dialog.setCanceledOnTouchOutside(true);
//设置对话框的大小
view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(this).getScreenHeight() * 0.23f));
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = (int) (ScreenSizeUtils.getInstance(this).getScreenWidth() * 0.75f);
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialogWindow.setAttributes(lp);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}