How to disable OnClickListerners

I am developing an application in which the user must correctly match the image and the corresponding name.

My problem is that the user first selects the image and selects the wrong name on which the wrong answer will be displayed, and if he chooses the answer, the correct answer will be displayed on it.

User must not reselect image again

I made onClickListerner null, but it does not work, some of my code looks like this:

    txt_tag[0] = (TextView) findViewById(R.id.txt_tag1);
    txt_tag[0].setOnClickListener(this);
    txt_tag[0].setTypeface(tf);

    txt_tag[1] = (TextView) findViewById(R.id.txt_tag2);
    txt_tag[1].setOnClickListener(this);
    txt_tag[1].setTypeface(tf);

    txt_tag[2] = (TextView) findViewById(R.id.txt_tag3);
    txt_tag[2].setOnClickListener(this);
    txt_tag[2].setTypeface(tf);

    txt_tag[3] = (TextView) findViewById(R.id.txt_tag4);
    txt_tag[3].setOnClickListener(this);
    txt_tag[3].setTypeface(tf);

    img[0] = (ImageButton) findViewById(R.id.img1);
    img[0].setOnClickListener(this);

    img[1] = (ImageButton) findViewById(R.id.img2);
    img[1].setOnClickListener(this);

    img[2] = (ImageButton) findViewById(R.id.img3);
    img[2].setOnClickListener(this);

    img[3] = (ImageButton) findViewById(R.id.img4);
    img[3].setOnClickListener(this);

    btn_nxt = (Button) findViewById(R.id.btn_next);
    btn_nxt.setOnClickListener(this);

and I called the method inside this method, where I did everything onClickListerner null

txt_tag[0].setOnClickListener(null);
txt_tag[1].setOnClickListener(null);
txt_tag[2].setOnClickListener(null);
txt_tag[3].setOnClickListener(null);
img[0].setOnClickListener(null);
img[1].setOnClickListener(null);
img[2].setOnClickListener(null);
img[3].setOnClickListener(null);

Can someone tell me where I am going wrong, or any changes that I can make with this.

Thanks in advance

+3
source share
5

       txt_tag[0].setClickable(false);
       txt_tag[1].setClickable(false);
       ..
       img[0].setClickable(false);
       img[1].setClickable(false);
       ..
+3

, , , android:clickable="false" xml setClickable(false);

+2

, . quiestion ( ) "ANSWERED", .

+2

, , , onClickListener , .

; , .

, onClickListener null - .

+1

, :

  • ImageViews TextViews .
  • ImageView, TextView. , " " -, , " ".
  • TextView , ImageView,
  • Textview TextView ,

, :

int selectedImage = -1;
int selectedText  = -1;

OnClickListener :

if (source instanceof ImageViews) {
  selectedImage = getArrayIndex(source); // I guess you already have a method to retrieve the index
  selectedText = -1; // reset textSelection
} else {
  if (selectedText < 0) {
    selectedText = getArrayIndex(source);
  }
}
updateAnswerTextView(); // here you check if the two selections (selectedText and selectedImage) match and display the corresponding string.

TextView

setClickable(false);

for each item as soon as you click. If a new image is selected, you will need to reinstall them again.

EDIT: And I agree with Rob, you should not delete yours Listenersto achieve this behavior.

+1
source

All Articles