Android阻止AlertDialog關閉,androidalertdialog

來源:互聯網
上載者:User

Android阻止AlertDialog關閉,androidalertdialog

 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             @Override11             public void onClick(DialogInterface dialog, int id) {12 13                 Field field = null;14 15                 try {16                     //通過反射擷取dialog中的私人屬性mShowing17                     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             @Override48             public void onClick(DialogInterface dialog, int id) {49 50                 Field field = null;51 52                 try {53                     //通過反射擷取dialog中的私人屬性mShowing54                     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();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.