Android AlertDialog仅在条件不工作时保持打开状态

Android AlertDialog仅在条件不工作时保持打开状态,android,android-alertdialog,Android,Android Alertdialog,我有以下代码,仅在满足某个条件时才希望我的“正”按钮关闭,否则我希望它保持打开状态并具有相同的数据: protected void showInputDialog() { final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); final View d

我有以下代码,仅在满足某个条件时才希望我的“正”按钮关闭,否则我希望它保持打开状态并具有相同的数据:

  protected void showInputDialog() {

        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.phrase_input_layout, null);
        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Create new phrase flashcard");
        dialogBuilder.setPositiveButton("Done", (dialog, whichButton) -> {
            SubmissionState state = addCardToDocument(dialogView);
            if (state == SubmissionState.SUBMITTED_WITH_RESULTS_FOUND){
                dialog.dismiss();
            } else {
                // still stay open here, but it still closes...
            }
        });
        dialogBuilder.setNegativeButton("Cancel", (dialog, whichButton) -> Log.d("DEBUG", "Cancelled creating new phrase card."));
        AlertDialog b = dialogBuilder.create();
        b.show();
    }
但是,无论发生什么情况,当我按下肯定按钮时,dialogBuilder都将关闭,无论它是否进入else语句;此外,即使我注释掉了
setPositiveButton
的所有代码,它仍然会关闭对话框

我做错了什么

如果相关,以下是我的
短语输入布局

<TextView
    android:id="@+id/phrase_translate_radio_group_header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="14dp"
    android:layout_marginBottom="4dp"
    android:text="@string/select_translation_mode"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" />

<RadioGroup
    android:id="@+id/phrase_translate_radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/phrase_manual_translation"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/manual_translation" />

    <RadioButton
        android:id="@+id/eng_auto_translation"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/english_auto_translation" />

    <RadioButton
        android:id="@+id/swe_auto_translation"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/swedish_auto_translation" />
</RadioGroup>

<EditText
    android:id="@+id/englishPhrase"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/english_phrase" />

<EditText
    android:id="@+id/swedishPhrase"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/swedish_phrase" />
</LinearLayout>


使用提供的
setPositiveButton
方法无法完成此操作。如果使用此方法设置侦听器,则无论何时单击该按钮,都会调用该侦听器,然后该对话框将被取消。我看不到重写此行为的方法,因为
AlertDialog
在内部使用实现此行为的
AlertController
对象

因此,您可以使用自定义按钮而不是默认按钮

例如,您可以在
phrase\u input\u layout
xml的底部添加两个按钮

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/negativeButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <View
        android:id="@+id/buttonSeparator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@color/separator" />

    <Button
        android:id="@+id/positiveButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
AlertDialog dialog;
protected void showInputDialog() {
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    
    final View dialogView = inflater.inflate(R.layout.phrase_input_layout, null);
    dialogBuilder.setView(dialogView);

    dialogBuilder.setTitle("Create new phrase flashcard");
    
    Button positiveButton = (Button) dialogView.findViewById(R.id.positiveButton);
    Button negativeButton = (Button) dialogView.findViewById(R.id.negativeButton);

    positiveButton.setText("Create new phrase flashcard");
    positiveButton.setOnClickListener(view -> {
        SubmissionState state = addCardToDocument(dialogView);
            if (state == SubmissionState.SUBMITTED_WITH_RESULTS_FOUND){
                dialog.dismiss();
            } else {
            }
    });

    negativeButton.setText("Cancel");
    negativeButton.setOnClickListener(view -> {
        dialog.dismiss()
    });

    dialog = dialogBuilder.create();
    dialog.show();
}