Skip to content Skip to sidebar Skip to footer

Android: Onclicklistener Does Not Work As Programmed

Quiz on android: I am setting an onClickListener to monitor which answer button is pressed and i am trying to access the correct answer from a table to check if score is given or n

Solution 1:

You have a lot of code, but I'm assuming that this is the problem:

controlsView.setOnClickListener(new View.OnClickListener() {

You give controlsView a private OnClickListener then expect it to handle click events from all the buttons. This will fail, since the other buttons aren't using this OnClickListener.

Make a shared listener.

View.OnClickListenerlistener=newView.OnClickListener() {//..

Then pass it off to the rest of the buttons.

answerButton1.setOnClickListener (listener);
answerButton2.setOnClickListener (listener);
//etc

You can also make the Activity implement the OnClickListener interface.

publicclassMyActivityextendsActivityimplementsOnClickListener{

@OverridepublicvoidonClick(View v)
{
 //implementation
}

Then to set the OnClickListener, you pass off the Activity instance instead.

answerButton1.setOnClickListener (MyActivity.this);

Post a Comment for "Android: Onclicklistener Does Not Work As Programmed"