2013-05-12 8 views
0

Итак, я создаю настраиваемое диалоговое окно, где я устанавливаю его в XML-файл. В этом файле у меня есть 2 кнопки. Один из них отвечает на это onClick, если другой нет. У меня нет идеи, почему это не так. Я правильно установил его идентификатор и сделал все точно так же, как сделал другую кнопку ... это не дает мне никаких ошибок, это просто ничего не делает. Я даже установил атрибут кнопки «clickable» в true ... У меня нет идей.Кнопка не отвечает на 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()? –

+0

У меня есть, а я нет ... вот что меня озадачивает. – cj1098

ответ

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

+0

Тогда почему бы вам не использовать положительные и отрицательные кнопки? – JustWork

+0

Я просто понял, что это потому, что у меня есть два способа доступа к этому меню, и я обращался к нему с одного, я забыл о том, где он не имеет вышеуказанного onClickListener() .. Это файл с 1000 строк, поэтому он взял меня на то, чтобы найти его>< Спасибо, что обратились за помощью! Также благодаря вам, я узнал, что вы можете вызывать функции из xml довольно круто! – cj1098

Смежные вопросы