安卓,AlertDialog基本使用方式
第一步先进入设计模式
//Builder进入设计模式
AlertDialog.Builder builder1 = new AlertDialog.Builder(AlertDialogActivity.this);
这里的bulider1是自己设置的一个按钮用来触发弹出对话框
使用自带的三种按钮
setPositiveButton 确切的,支持的,肯定的按钮
setNeutralButton 中性的按钮
setNegativeButton 消极的按钮
AlertDialog.Builder builder1 = new AlertDialog.Builder(AlertDialogActivity.this);
AlertDialog show = builder1.setTitle("请回答")//返回的builder所以可以继续"."
.setIcon(R.drawable.icon_user1)//设置图标
.setMessage("你觉得学的怎么样")
//确切的,支持的,肯定的按钮
.setPositiveButton("棒", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(AlertDialogActivity.this, "你很厉害");
}
})//中性的按钮
.setNeutralButton("还行", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(AlertDialogActivity.this, "请继续加油");
}
})//消极的按钮
.setNegativeButton("学不会", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(AlertDialogActivity.this, "你要相信自己");
}
}).show();
这里的ToastUtil是自己封装的一个toast类,也可以直接写toast
注意 这里的onClick是DialogInterface.OnClickListener()方法,不是View.OnClickListener()方法
弹出单选对话框
final String[] arry2 = new String[]{"男","女"};
AlertDialog.Builder builder2 = new AlertDialog.Builder(AlertDialogActivity.this);
builder2.setTitle("性别").setItems(arry2, new DialogInterface.OnClickListener() {
@Override
//which 选择数组的位置,0就是男,1就是女
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(AlertDialogActivity.this,arry2[which]);
}
}).show();
弹出带图标的单选对话框
final String[] arry3 =new String[]{"男","女"};
AlertDialog.Builder builder3 = new AlertDialog.Builder(AlertDialogActivity.this);
builder3.setTitle("性别")
.setSingleChoiceItems(arry3, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(AlertDialogActivity.this,arry3[which]);
//点击后对话框消失
dialog.dismiss();
}
})//点击对话框外,对话框不消失
.setCancelable(false).show();
弹出的对话框点击对话框外时对话框会消失
这里的setCancelable(false)会使对话框不会消失
多选对话框
final String[] arry4 =new String[]{"吃饭","睡觉","写代码"};
boolean[] isSelect = new boolean[]{false,false,true};
AlertDialog.Builder builder4 = new AlertDialog.Builder(AlertDialogActivity.this);
builder4.setTitle("选择要做的事情")//多选
.setMultiChoiceItems(arry4, isSelect, new DialogInterface.OnMultiChoiceClickListener() {
@Override
//int which 表示选中第几个 boolean isChecked表示第几个选中还是没选中
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
ToastUtil.showMsg(AlertDialogActivity.this,arry4[which]+":"+isChecked);
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
自定义对话框
AlertDialog.Builder builder5 = new AlertDialog.Builder(AlertDialogActivity.this);
//找到自定义的视图
View view = LayoutInflater.from(AlertDialogActivity.this).inflate(R.layout.layout_dialog,null);
EditText editTextUsername = view.findViewById(R.id.et_username);
EditText editTextPassword = view.findViewById(R.id.et_password);
//buttnlogin 的id一定要找对否则就会空指针报错
Button buttnLogin = view.findViewById(R.id.btn_alertdialog_login);
buttnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
builder5.setTitle("请先登陆").setView(view).show();
如果报空指针错误去看看 自定义对话框中的视图和组件id有没有找对