AlertDialog 仅在不工作的情况下保持打开状态
AlertDialog to only stay open on condition not working
我有以下代码,只希望在满足特定条件时关闭肯定按钮,否则我希望它在相同数据下保持打开状态:
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
的所有代码,它仍然会关闭对话框!
我做错了什么?
如果相关,这也是我的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();
}
我有以下代码,只希望在满足特定条件时关闭肯定按钮,否则我希望它在相同数据下保持打开状态:
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
的所有代码,它仍然会关闭对话框!
我做错了什么?
如果相关,这也是我的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();
}