2013-05-12 81 views
0

所以我創建了一個自定義對話框,我將它設置爲一個xml文件。在那個文件中我有2個按鈕。其中之一是響應它的onClick對應的另一個不是。我不知道爲什麼它不是。我已經正確設置了它的ID,並且完成了與其他按鈕完全相同的操作......它不會給我任何錯誤,它只是沒有做任何事情。 我甚至將按鈕屬性「clickable」設置爲true ...我不知道該怎麼做。Button沒有響應OnClickListener()

此外,似乎如果我嘗試添加另一個按鈕,它也沒有響應,當單擊...是否有一些對話框可以採取的小部件的限制?哈哈...

它不工作的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" /> 
+0

您是否使用了調試工具來查看您是否至少輸入了onClick()函數? – 2013-05-12 01:17:04

+0

我有,而我不是......那是令我感到困惑的。 – cj1098 2013-05-12 01:19:22

回答

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. 
} 
+0

問題在於你基本上只是進行一個函數調用,而且我需要從這個alertDialog中的某些變量,除非它們在對話框中,否則我不能得到它。 – cj1098 2013-05-12 01:15:55

+0

那你爲什麼不使用正面和負面的按鈕? – JustWork 2013-05-12 01:20:59

+0

我只是想出了它..這是因爲我有2種方式訪問​​此菜單,我從一個訪問它我忘了它沒有上述onClickListener()..它是一個1000行文件,所以它花了我一會兒才找到它。< 謝謝你的幫助! 也謝謝你,我瞭解到你可以調用xml很酷的功能! – cj1098 2013-05-12 01:23:09