- 一般的AlertDialog
//一般的AlertDialog
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.heart)
.setTitle("警告")
.setMessage("确定删除吗?")
.setPositiveButton("确定",null)
.setNegativeButton("不确定",null)
.setNeutralButton("取消",null)
.setCancelable(false)//允许取消
.show();
- 自定义EditText
//AlertDialog自定义EditText
final EditText edt = new EditText(MainActivity.this);
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.heart)
.setTitle("请输入")
.setView(edt)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"输入:" + edt.getText().toString(),Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消",null)
.show();
- 列表
//AlertDialog列表
final String [] items = {"选项一","选项二","选项三","选项④"};
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.heart)
.setTitle("列表")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"点击了:" + items[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", null)
.setNegativeButton("取消",null)
.show();
- 单选
//AlertDialog单选
final int pos = 2;
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.heart)
.setTitle("单选")
.setSingleChoiceItems(items,pos,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"点击了:" + items[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"选中:" + items[pos],Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",null)
.show();
- 多选
//AlertDialog多选
final boolean checkedItems [] = {false,true,true,false};
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.heart)
.setTitle("多选")
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Toast.makeText(MainActivity.this,(b ? "选中:" : "未选中:") + items[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String s = "选中:";
for(int j = 0;j < items.length;++j){//获取选中的选项
if(checkedItems[j]){
s = s + items[j] + "、";
}
}
Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",null)
.show();
- 自定义View
//AlertDialog自定义View
final View login = View.inflate(MainActivity.this,R.layout.login,null);
final AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.heart)
.setTitle("登录")
.setView(login)//加载自定义View
.create();//需要先create
login.findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//点击登录按钮
Toast.makeText(MainActivity.this,"用户名:" + ((EditText)login.findViewById(R.id.edtName)).getText().toString() + " 密码:" + ((EditText)login.findViewById(R.id.edtPwd)).getText().toString(),Toast.LENGTH_LONG).show();
dlg.dismiss();//login和dlg需要定义为final的
}
});
dlg.show();
代码:https://github.com/yangyang0312/AndroidTestCode/tree/master/TestProj