Android: How to get a modal dialog or similar modal behavior?

后端 未结 11 1934
暗喜
暗喜 2020-12-01 05:17

These days I\'m working on simulating modal dialog in Android. I\'ve googled a lot, there\'s much discussions but sadly there\'s not much options to get it modal. Here\'s so

11条回答
  •  孤街浪徒
    2020-12-01 05:33

    Developers of Android and iOS decided that they are powerful and smart enough to reject Modal Dialog conception (that was on market for many-many years already and didn't bother anyone before), unfortunately for us.

    Here is my solution, it works great:

        int pressedButtonID;
        private final Semaphore dialogSemaphore = new Semaphore(0, true);
        final Runnable mMyDialog = new Runnable()
        {
            public void run()
            {
                AlertDialog errorDialog = new AlertDialog.Builder( [your activity object here] ).create();
                errorDialog.setMessage("My dialog!");
                errorDialog.setButton("My Button1", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        pressedButtonID = MY_BUTTON_ID1;
                        dialogSemaphore.release();
                        }
                    });
                errorDialog.setButton2("My Button2", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        pressedButtonID = MY_BUTTON_ID2;
                        dialogSemaphore.release();
                        }
                    });
                errorDialog.setCancelable(false);
                errorDialog.show();
            }
        };
    
        public int ShowMyModalDialog()  //should be called from non-UI thread
        {
            pressedButtonID = MY_BUTTON_INVALID_ID;
            runOnUiThread(mMyDialog);
            try
            {
                dialogSemaphore.acquire();
            }
            catch (InterruptedException e)
            {
            }
            return pressedButtonID;
        }
    

提交回复
热议问题