一.普通对话框
演示图片:
demo代码:
class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_dialog1:
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("请回答")
.setMessage("你觉得Android是一种什么样的语言")
.setIcon(R.mipmap.username)
.setPositiveButton("神奇的语言", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
ToastUtil.showMsg(DialogActivity.this,"很好呀");
}
}).setNeutralButton("还行", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,"在思考一下~");
}
}).setNegativeButton("不神奇", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,"怎么可能");
}
}).show();
break;
}
}
}
二.单选对话框
运行图片
demo代码:
case R.id.btn_dialog2:
final String[] array = new String[]{"男","女"};
AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
builder1.setTitle("选择性别")
.setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,array[i]);
}
}).show();
复选按钮形式展示:
demo代码展示:
case R.id.btn_dialog3:
final String[] array3 = new String[]{"男","女"};
AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
builder3.setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ToastUtil.showMsg(DialogActivity.this,array3[i]);
}
}).show();
break;
三.多选弹窗:
demo代码:
case R.id.btn_dialog4:
final String[] array4 = new String[]{"唱歌","跳舞","写代码"};
final boolean[] isSelected = new boolean[]{false,false,true};
AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
builder4.setTitle("选择兴趣")
.setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean isChecked) {
ToastUtil.showMsg(DialogActivity.this,array4[i]+":"+isChecked);
}
}).show();
四.自定义dialog
demo代码:
case R.id.btn_dialog5:
AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
View view= LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
EditText etUsername = view.findViewById(R.id.et_username);
EditText etPassword = view.findViewById(R.id.et_password);
Button btnLogin = view.findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//相应操作
}
});
builder5.setTitle("请先登录")
.setView(view).show();
break;
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦