3

我正在开发一个应用程序,用户必须正确匹配图像和相应的名称。

我的问题是当用户首先选择图像并选择错误的名称时,它将显示错误的答案,如果他选择了答案,它将显示正确的答案。

用户不必再次重新选择图像

我已将 onClickListerner 设为空,但它不起作用我的一些代码如下,

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

我在该方法中调用了一个方法,在该方法中我将所有 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);

谁能告诉我哪里出了问题或我可以对其进行的任何修改。

提前致谢

4

5 回答 5

3

尝试使用

       txt_tag[0].setClickable(false);
       txt_tag[1].setClickable(false);
       ..
       img[0].setClickable(false);
       img[1].setClickable(false);
       ..
于 2012-04-27T10:28:43.140 回答
2

你的问题不是很清楚.. 但是如果你想让你的图像和文本标签不可点击..android:clickable="false"用 xml 或setClickable(false);

于 2012-04-27T10:17:19.063 回答
2

如果我是你,我会在侦听器中检查该逻辑。因此,如果问题(如果是测验)处于“已回答”状态,请不要对事件做出反应。

于 2012-04-27T10:30:24.047 回答
1

这是我的建议,如果你想编写特定的行为,你可以使用 onClickListener 回调来实现你想要的。

在监听器中,检查图像的状态;如果它已经被选中并且您想忽略该事件,那么您只需退出回调。

我认为将 onClickListener 设置为 null 是错误的做法。

于 2012-04-27T10:40:55.687 回答
1

您的问题不清楚,但我的理解如下:

  • 你有一堆ImageViews一堆TextViews和它们之间的映射。
  • 您希望能够先选择一个ImageView,然后选择一个TextView。如果他们匹配,“正确答案”将显示在某处,如果不匹配,“错误答案”将显示
  • 如果在选择 ImageView 之前单击 TextView,则不会发生任何事情
  • 如果您单击一个 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);

在每个元素上单击一个。如果选择了新图像,则必须再次将它们设置为可点击。

编辑:我同意 Rob 的观点,你不应该删除你的Listeners来实现这种行为。

于 2012-04-27T10:49:15.043 回答