直接上代码
final EditText et = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请输入...");
builder.setView(et);
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
try
{
Field field = dialog.getClass().getSuperclass().getDeclaredField( "mShowing" );
field.setAccessible( true );
field.set( dialog, false); // false - 使之不能关闭
}
catch ( Exception e )
{
e.printStackTrace();
}
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
try
{
Field field = dialog.getClass().getSuperclass().getDeclaredField( "mShowing" );
field.setAccessible( true );
field.set( dialog, true); // true- 使之能关闭
}
catch ( Exception e )
{
e.printStackTrace();
}
}
});
builder.show();