2013-05-12 3 views
0

Je crée donc une boîte de dialogue personnalisée dans laquelle je la place dans un fichier xml. Dans ce fichier, j'ai 2 boutons. L'un d'entre eux répond à son homologue onClick alors que l'autre ne l'est pas. Je n'ai aucune idée pourquoi ce n'est pas le cas. J'ai réglé son identifiant correctement, et j'ai tout fait exactement de la même manière que l'autre bouton ... ça ne me donne pas d'erreur, ça ne fait rien. J'ai même mis l'attribut bouton "cliquable" à vrai ... Je suis à court d'idées.Le bouton ne répond pas à OnClickListener()

En outre, il semble que si j'essaie d'ajouter un autre bouton, il ne répond pas lorsque je clique dessus ... y a-t-il une limite à la quantité de widgets qu'un dialogue peut prendre? lol ...

c'est le bouton SaveAndFinish qui ne fonctionne pas

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

est ici la déclaration des boutons dans 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

Avez-vous utilisé l'outil de débogage pour voir si vous entrez au moins la fonction onClick()? –

+0

J'ai, et je ne suis pas ... c'est ce qui me dérange. – cj1098

Répondre

1

Pouvez-vous essayer d'attraper cliquez événement comme celui-ci:

<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" /> 

Dans votre fichier de code .java:

public void finishCreatingFlashCards(View v) { 
     // your code on button click. 
} 
+0

Le problème avec ceci est que vous faites simplement un appel de fonction et j'ai besoin de certaines variables de l'alertDialog au-dessus de celui-ci que je ne peux pas obtenir à moins qu'elles ne soient dans le dialogue. – cj1098

+0

Alors pourquoi n'utilisez-vous pas les boutons positifs et négatifs? – JustWork

+0

Je l'ai juste compris ... c'était parce que j'ai 2 façons d'accéder à ce menu, et j'y accédais à partir d'un j'ai oublié où il n'a pas le onClickListener() ci-dessus .. C'est un fichier de 1000 lignes donc m'a pris un certain temps pour le trouver>. < Nous vous remercions de votre aide! Aussi grâce à vous, j'ai appris que vous pouvez appeler des fonctions de xml assez cool! – cj1098

Questions connexes