直播商城原始碼,AlertDialog.Builder 設定點選不關閉彈框

zhibo系統開發發表於2023-10-10

直播商城原始碼,AlertDialog.Builder 設定點選不關閉彈框

可以透過手動設定mShowing的值可以透過手動設定mShowing的值。

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("標題");
                builder.setView(view);
                builder.setIcon(R.mipmap.ic_applogorec); //設定圖示
                builder.setCancelable(false);
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                            field.setAccessible(true);
                            if(true){
                                field.set(dialog, true);//true表示要關閉
                            } else{
                                field.set(dialog, false);//false不關閉
                            } 
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                builder.setNegativeButton("關閉", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //一旦上面設定了不關閉DIALOG,點取消也不會關閉,所以需在取消事件下設定為可關閉
                        try {
                            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                            field.setAccessible(true);
                            field.set(dialog, true);//true表示要關閉
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                        dialog.cancel();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();


以上就是 直播商城原始碼,AlertDialog.Builder 設定點選不關閉彈框,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2987882/,如需轉載,請註明出處,否則將追究法律責任。

相關文章