Android带编辑框的对话框

直接上代码

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();

点击链接查看效果

你可能感兴趣的:(Android带编辑框的对话框)