0

因此,我创建了一个自定义对话框,并将其设置为 xml 文件。在那个文件中,我有 2 个按钮。其中一个正在响应它的 onClick 对应项,而另一个则没有。我不知道为什么不是。我已经正确设置了它的 ID,并且所做的一切都与我做另一个按钮的方式完全相同......它没有给我任何错误,它只是没有做任何事情。我什至将按钮属性“可点击”设置为真......我没有想法。

此外,似乎如果我尝试添加另一个按钮,单击时它也没有响应......对话框可以使用的小部件数量是否有限制?哈哈...

是 SaveAndFinish 按钮不起作用

Dialog createFlashCards = new Dialog(FlashCard.this);
                                createFlashCards.setContentView(R.layout.flash_card_create);
                                final EditText Question = (EditText)createFlashCards.findViewById(R.id.FlashCard_CreateQuestionEditText);
                                final EditText Answer = (EditText)createFlashCards.findViewById(R.id.FlashCard_CreateAnswerEditText);
                                Button SaveFlashCard = (Button)createFlashCards.findViewById(R.id.create_new_saved_FlashCard);
                                Button FinishAndSave = (Button)createFlashCards.findViewById(R.id.finish_creating_flashCards);

                                //saves the flashCard to the file on their phone
                                SaveFlashCard.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View arg0) {
                                        //have to check for empty questions or answers
                                        if (!Question.getText().toString().equals("") && !Answer.getText().toString().equals("")) {
                                                String newLineQuestionFixer = Question.getText().toString();
                                                String newLineAnswerFixer = Answer.getText().toString();
                                                newLineQuestionFixer = newLineQuestionFixer.replaceAll("\n", "<GAR>");
                                                newLineAnswerFixer = newLineAnswerFixer.replaceAll("\n", "<GAR>");
                                                Statics.addDataIntoFlashCardFile(FlashCard.this, input.getText().toString(), newLineQuestionFixer,
                                                        newLineAnswerFixer, Statics.mainPageListPosition);
                                            Toast.makeText(FlashCard.this, "FlashCard successfully created", Toast.LENGTH_SHORT).show();

                                        }
                                        else {
                                            Toast.makeText(FlashCard.this, "Must have a question and an answer to save a flashCard", Toast.LENGTH_SHORT).show();
                                        }
                                        Question.setText("");
                                        Answer.setText("");
                                    }
                                });

                                FinishAndSave.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View arg0) {
                                        //have to check for empty questions or answers
                                        if (!Question.getText().toString().equals("") && !Answer.getText().toString().equals("")) {
                                                String newLineQuestionFixer = Question.getText().toString();
                                                String newLineAnswerFixer = Answer.getText().toString();
                                                newLineQuestionFixer = newLineQuestionFixer.replaceAll("\n", "<GAR>");
                                                newLineAnswerFixer = newLineAnswerFixer.replaceAll("\n", "<GAR>");
                                                Statics.addDataIntoFlashCardFile(FlashCard.this, input.getText().toString(), newLineQuestionFixer,
                                                        newLineAnswerFixer, Statics.mainPageListPosition);
                                            Toast.makeText(FlashCard.this, "FlashCard successfully created", Toast.LENGTH_SHORT).show();
                                        //just an intent to refresh the class
                                        Intent refresh = new Intent(FlashCard.this, FlashCard.class);
                                        refresh.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        startActivity(refresh);
                                        overridePendingTransition(0,0);
                                        }
                                        else {
                                            Toast.makeText(FlashCard.this, "Must have a question and an answer to save a flashCard", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                                createFlashCards.show();

这是xml中按钮的声明

<Button
        android:id="@+id/finish_creating_flashCards"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/btn_round_xml"
        android:textColor="#FFFFFF"
        android:clickable="true"
        android:text="Finish and Save" />

    <Button
        android:id="@+id/create_new_saved_FlashCard"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/btn_round_xml"
        android:clickable="true"
        android:text="Save_FlashCard"
        android:textColor="#FFFFFF" />
4

1 回答 1

1

你能尝试捕捉这样的点击事件吗:

<Button
    android:id="@+id/finish_creating_flashCards"
    android:layout_width="wrap_content"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/btn_round_xml"
    android:textColor="#FFFFFF"
    android:clickable="true"
    android:text="Finish and Save"
    android:onClick="finishCreatingFlashCards" />

在您的 .java 代码文件中:

public void finishCreatingFlashCards(View v) {
        // your code on button click.
}
于 2013-05-12T01:11:47.283 回答