zoukankan      html  css  js  c++  java
  • Android阻止AlertDialog关闭

     1 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     2 builder.setTitle("测试");
     3 
     4 LayoutInflater inflater = getActivity().getLayoutInflater();
     5 View view = inflater.inflate(R.layout.dialogfragment_num_input, null);
     6 builder.setView(view);
     7 
     8 builder.setPositiveButton("确定",
     9         new DialogInterface.OnClickListener() {
    10             @Override
    11             public void onClick(DialogInterface dialog, int id) {
    12 
    13                 Field field = null;
    14 
    15                 try {
    16                     //通过反射获取dialog中的私有属性mShowing
    17                     field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
    18                     field.setAccessible(true);//设置该属性可以访问
    19                 } catch (Exception ex) {
    20 
    21                 }
    22 
    23                 String inputValue = String.valueOf(mEdit.getText());
    24                 if (inputValue == null || "".equals(inputValue)) {
    25                     try {
    26                         //设置dialog不可关闭
    27                         field.set(dialog, false);
    28                         dialog.dismiss();
    29                     } catch (Exception ex) {
    30                     }
    31                 } else {
    32 
    33                    //
    34                    //做自己的事
    35                    //
    36                     try {
    37                         //关闭
    38                         field.set(dialog, true);
    39                         dialog.dismiss();
    40                     } catch (Exception ex) {
    41                     }
    42                 }
    43             }
    44         });
    45 builder.setNegativeButton("取消",
    46         new DialogInterface.OnClickListener() {
    47             @Override
    48             public void onClick(DialogInterface dialog, int id) {
    49 
    50                 Field field = null;
    51 
    52                 try {
    53                     //通过反射获取dialog中的私有属性mShowing
    54                     field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
    55                     field.setAccessible(true);//设置该属性可以访问
    56                 } catch (Exception ex) {
    57                 }
    58 
    59                 try {
    60                     field.set(dialog, true);
    61                     dialog.dismiss();
    62                 } catch (Exception ex) {
    63                 }
    64 
    65             }
    66         });
    67 
    68 
    69 builder.create();
  • 相关阅读:
    P1012 拼数(水题)
    oracle 存储过程中调用同义词报错“表和视图不存在”
    C#文件相对路径
    C# WebAPi接收和发送图片
    EFCore学习笔记一:(安装EFCore并根据Code First生成数据库)
    Winform切换登录用户
    Winform中子控件Dock排列顺序问题
    ORA-28001: the password has expired解决方法
    C#实体类生成XML(注意<![CDATA]>标签的不解析)
    一次完整的HTTP请求过程
  • 原文地址:https://www.cnblogs.com/wz122889488/p/5280644.html
Copyright © 2011-2022 走看看