如何重新创建提醒对话框

时间:2017-10-02 16:04:22

标签: android alertdialog

如果当前alert dialog没有位置详细信息,我正在尝试构建一个弹出屏幕的user

此外,我想检查用户是否已填写EditTexts,如果没有,我想重新创建alert dialog,阻止用户进一步前进。

是否有一种重新启动提醒的简单方法?

警报对话框

    AlertDialog.Builder alert = new AlertDialog.Builder(UserActivity.this, R.style.MyDialogTheme);
    LayoutInflater inflater = UserActivity.this.getLayoutInflater();
    View layout = inflater.inflate(R.layout.no_location_alert_layout, null);
    alert.setView(layout);

    final EditText userCountryInput = (EditText) layout.findViewById(R.id.alert_country_edit_text);
    final EditText userCityInput = (EditText) layout.findViewById(R.id.alert_city_edit_text);

    alert.setTitle("Enter your details");
    alert.setMessage("Oops! Looks like you haven't added any current location details.");
    alert.setCancelable(false);

    alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String userCountry = userCountryInput.getText().toString();
            String userCity = userCityInput.getText().toString();

            if (userCountry.isEmpty() && userCity.isEmpty()) {
                Toast.makeText(context, "Please enter both your current city and country", Toast.LENGTH_LONG).show();
                // HERE I NEED TO RESTART THE ALERT
            }
            else
            {
            // DO SOME STUFF
            }
        }
    });
    alert.show();

添加alert.show()会导致此错误:

 E/UncaughtException: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4498)
    at android.view.ViewGroup.addView(ViewGroup.java:4339)
    at android.view.ViewGroup.addView(ViewGroup.java:4311)
    at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:647)
    at android.support.v7.app.AlertController.setupView(AlertController.java:463)
    at android.support.v7.app.AlertController.installContent(AlertController.java:226)
    at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:436)
    at android.app.Dialog.show(Dialog.java:314)
    at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
    at com.example.android.booktrade_v10.ui.searchFeature.UserActivity$5.onClick(UserActivity.java:279)
    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

2 个答案:

答案 0 :(得分:0)

只需致电alert.show();再次显示提醒。

答案 1 :(得分:0)

我找到了一个限制用户访问的解决方案,在按done按钮之前,根据个人需要实际禁用button(例如,请求用户输入一些信息到{{1}因此,无需重新启动对话框。

以下示例代码禁用editText

positivenegative按钮
alert dialog