猿问

Android-我将如何动态更改clickListener

所以我有一个按钮数组,其中一个是正确的答案,而另外三个是不正确的。但是,对于每个问题,正确的按钮都会更改。我将如何更新我的点击监听器?似乎很简单的问题,也许我在这里看不到明确的答案...


到目前为止,这是我的代码,在此先感谢:


int correctIndex=newQuestion(questionTextView,answerButtons);//CREATES A NEW QUESTION and returns the correct index (0-3);


answerButtons[correctIndex].setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        correctDialog(questionTextView,answerButtons);

    }

});


for (int i = 0; i < 4; i++) {

    final int j = i;

    if (j != correctIndex) {

        answerButtons[j].setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                wrongDialog(questionTextView, answerButtons);

            }

        });

    }

}


浮云间
浏览 163回答 2
2回答

慕后森

创建一个通用侦听器,您可以将其添加到所有按钮,并在该侦听器内处理根据需要确定正确的逻辑。例如:class YourListener implements View.OnClickListener {&nbsp; &nbsp; private int correctButtonId;&nbsp; &nbsp; public YourListener(int correctButtonId) {&nbsp; &nbsp; &nbsp; &nbsp; this.correctButtonId = correctButtonId;&nbsp; &nbsp; }&nbsp; &nbsp;@Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; if (v.getId() == correctButtonId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do stuff&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do other stuff&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后,您可以将所有n按钮设置为具有该侦听器,并可以从侦听器外部根据需要设置正确按钮的ID。如// this is the id of the button that is correct, where x represents its index, which you know ahead of timeint id = answerButtons[x].getId();for (int i = 0; i < 4; i++) {&nbsp; &nbsp; &nbsp;answerButtons[i].setOnClickListener(new YourListener(id));}编辑以回答:如何correctDialog从侦听器内部调用方法(例如,在您的情况下)。一种方法是使侦听器成为您活动中的内部类。因此,您有以下内容(未经测试,请尝试一下):public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; private class YourListener implements View.OnClickListener {&nbsp; &nbsp; &nbsp; &nbsp; private TextView textView;&nbsp; &nbsp; &nbsp; &nbsp; private Button[] buttons;&nbsp; &nbsp; &nbsp; &nbsp; private int correctButtonId;&nbsp; &nbsp; &nbsp; &nbsp; public YourListener(TextView textView, Button[] buttons, int correctButtonId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.textView = textView;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.buttons = buttons;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.correctButtonId = correctButtonId;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (v.getId() == correctButtonId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainActivity.this.correctDialog(textView, buttons);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainActivity.this.wrongDialog(textView, buttons);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

呼如林

我将为所有按钮设置相同的clickListener,然后将逻辑移到那里。只需检查数组中按钮的索引是否与正确答案的索引相同,而无需更新clickListener或设置不同。
随时随地看视频慕课网APP

相关分类

Python
我要回答