AlertDialog仅在不工作的情况下保持打开状态

问题描述

我有以下代码,并且只希望在满足特定条件时关闭我的肯定按钮,否则我希望它在打开时保持打开状态:

  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",whichButton) -> Log.d("DEBUG","Cancelled creating new phrase card."));
        AlertDialog b = dialogBuilder.create();
        b.show();
    }

但是,无论如何,当我按下肯定按钮时,无论是否进入else语句,dialogBu​​ilder都会关闭。同样,即使我注释掉setPositiveButton的所有代码,它仍然会关闭该对话框!

我在做什么错了?

如果相关,这也是我的phrase_input_layout

<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_input_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();
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...