Android AlertDialog 各种弹框代码
Posted 一叶孤城
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android AlertDialog 各种弹框代码相关的知识,希望对你有一定的参考价值。
1、消息框
btn01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("消息标题");
ab.setMessage("提示内容提示内容提示内容");
ab.setPositiveButton("确定", null);
ab.show();
}
});
2、确认框
btn02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("消息标题");
ab.setMessage("提示内容提示内容提示内容");
ab.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "点击了确认", Toast.LENGTH_SHORT).show();
}
});
ab.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
}
});
ab.show();
}
});
3、输入框
btn03.setOnClickListener(new View.OnClickListener() {
EditText editText = new EditText(BtnActivity.this);
String Getstr = "";
@Override
public void onClick(View v) {
editText = new EditText(BtnActivity.this);
editText.setText(Getstr);
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("消息标题");
ab.setIcon(android.R.mipmap.sym_def_app_icon);
ab.setView(editText);
ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Getstr = editText.getText().toString();
Toast.makeText(BtnActivity.this, Getstr, Toast.LENGTH_SHORT).show();
}
});
ab.setNegativeButton("取消", null);
ab.show();
}
});
4、单选框
btn04.setOnClickListener(new View.OnClickListener() {
String GetSelect = "";
int index = 0;
String[] Items = new String[]{"选项1", "选项2", "选项3", "选项4"};
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("单选框");
ab.setIcon(android.R.mipmap.sym_def_app_icon);
ab.setSingleChoiceItems(Items, index,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
index = which;
Toast.makeText(BtnActivity.this, Items[which].toString(), Toast.LENGTH_SHORT).show();
GetSelect = Items[which].toString();
// dialog.dismiss();//关闭对话框
}
}
);
ab.setNegativeButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "你选择了:" + GetSelect, Toast.LENGTH_SHORT).show();
}
});
ab.show();
}
});
5、复选框
btn05.setOnClickListener(new View.OnClickListener() {
String GetSelect = "";
String[] Items = new String[]{"选项1", "选项2", "选项3", "选项4"};
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("复选框");
ab.setIcon(android.R.mipmap.sym_def_app_icon);
ab.setMultiChoiceItems(Items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(BtnActivity.this, "你选择了:" + Items[which].toString(), Toast.LENGTH_SHORT).show();
GetSelect += Items[which].toString() + "-";
} else {
Toast.makeText(BtnActivity.this, "你取消了:" + Items[which].toString(), Toast.LENGTH_SHORT).show();
}
}
});
ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this, "你选择了:" + GetSelect, Toast.LENGTH_SHORT).show();
}
});
ab.setNegativeButton("取消", null);
ab.show();
}
});
6、列表框
btn06.setOnClickListener(new View.OnClickListener() {
String GetSelect = "";
int index = 0;
String[] Items = new String[]{"列表项1", "列表项2", "列表项3", "列表项4", "列表项4", "列表项4", "列表项4", "列表项2", "列表项3", "列表项4", "列表项2", "列表项3", "列表项4"};
@Override
public void onClick(View v) {
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("列表框");
ab.setIcon(android.R.mipmap.sym_def_app_icon);
ab.setItems(Items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
index = which;
Toast.makeText(BtnActivity.this, Items[which].toString(), Toast.LENGTH_SHORT).show();
GetSelect = Items[which].toString();
}
});
/* ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BtnActivity.this,GetSelect,Toast.LENGTH_SHORT).show();
}
});
ab.setNegativeButton("取消", null);*/
ab.show();
}
});
7、图片框
btn07.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView img = new ImageView(BtnActivity.this);
img.setImageResource(R.mipmap.ic_launcher);
AlertDialog.Builder ab = new AlertDialog.Builder(BtnActivity.this);
ab.setTitle("图片框");
ab.setIcon(android.R.mipmap.sym_def_app_icon);
ab.setView(img);
ab.setPositiveButton("确定", null);
ab.setNegativeButton("取消", null);
ab.show();
}
});
以上是关于Android AlertDialog 各种弹框代码的主要内容,如果未能解决你的问题,请参考以下文章
Android:如何设置AlertDialog的宽度和高度,以及AlertDialog风格的按钮?