0

我正在制作一个应用程序,其中有 18 个按钮(bAnswer1 到 bAnswer18)和 6 个文本视图(tvAnswer1 到 6)。我的意思是将 6 个单词分成 2 个单词的 3 个单词(例如 button,bu - tt - on),我用 6 个单词做到了这一点,每个单词都将它们放在一个片段中(slice1 到 18),所以这段代码会在 case R 中重复。 id.bAnswer18:你们会注意到这将是休!我知道我可以通过添加一个 for 循环让它变得非常简单,但它不会工作而且我不能出去?!

有人可以帮我解决这个问题吗?

我的代码:

public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()) {
    case R.id.bAnswer1:

        if (testing1 != 6) {
            String old = tvAnswer1.getText().toString();
            tvAnswer1.setText(old + slice1);
            bAnswer1.setVisibility(View.GONE);
            break;

        }
        if (testing2 != 6) {
            String old = tvAnswer2.getText().toString();
            tvAnswer2.setText(old + slice1);
            bAnswer1.setVisibility(View.GONE);
            break;
        }
        if (testing3 != 6) {
            String old = tvAnswer3.getText().toString();
            tvAnswer3.setText(old + slice1);
            bAnswer1.setVisibility(View.GONE);
            break;
        }
        if (testing4 != 6) {
            String old = tvAnswer4.getText().toString();
            tvAnswer4.setText(old + slice1);
            bAnswer1.setVisibility(View.GONE);
            break;
        }
        if (testing5 != 6) {
            String old = tvAnswer5.getText().toString();
            tvAnswer5.setText(old + slice1);
            bAnswer1.setVisibility(View.GONE);
            break;
        }
        if (testing6 != 6) {
            String old = tvAnswer6.getText().toString();
            tvAnswer6.setText(old + slice1);
            bAnswer1.setVisibility(View.GONE);
            break;
        }

        break;
    case R.id.bAnswer2:
        if (testing1 != 6) {
            String old = tvAnswer1.getText().toString();
            tvAnswer1.setText(old + slice2);
            bAnswer2.setVisibility(View.GONE);
            break;

        }
        if (testing2 != 6) {
            String old = tvAnswer2.getText().toString();
            tvAnswer2.setText(old + slice2);
            bAnswer2.setVisibility(View.GONE);
            break;
        }
        if (testing3 != 6) {
            String old = tvAnswer3.getText().toString();
            tvAnswer3.setText(old + slice2);
            bAnswer2.setVisibility(View.GONE);
            break;
        }
        if (testing4 != 6) {
            String old = tvAnswer4.getText().toString();
            tvAnswer4.setText(old + slice2);
            bAnswer2.setVisibility(View.GONE);
            break;
        }
        if (testing5 != 6) {
            String old = tvAnswer5.getText().toString();
            tvAnswer5.setText(old + slice2);
            bAnswer2.setVisibility(View.GONE);
            break;
        }
        if (testing6 != 6) {
            String old = tvAnswer6.getText().toString();
            tvAnswer6.setText(old + slice2);
            bAnswer2.setVisibility(View.GONE);
            break;
        }
        break;
4

1 回答 1

0

您正在寻找的真正答案并不是真正的 for 循环。

如果你这样做,for循环会有所帮助

 OnClickListener myOnClick = new OnClickListener() {
 //privately paste what you had
 public void onClick(View v) {
 if (testing1 != 6) {
        String old = tvAnswer1.getText().toString();
        tvAnswer1.setText(old + slice1);
        v.setVisibility(View.GONE);
        break;

    }
    if (testing2 != 6) {
        String old = tvAnswer2.getText().toString();
        tvAnswer2.setText(old + slice1);
        v.setVisibility(View.GONE);
        break;
    }
    if (testing3 != 6) {
        String old = tvAnswer3.getText().toString();
        tvAnswer3.setText(old + slice1);
        v.setVisibility(View.GONE);
        break;
    }
    if (testing4 != 6) {
        String old = tvAnswer4.getText().toString();
        tvAnswer4.setText(old + slice1);
        v.setVisibility(View.GONE);
        break;
    }
    if (testing5 != 6) {
        String old = tvAnswer5.getText().toString();
        tvAnswer5.setText(old + slice1);
        v.setVisibility(View.GONE);
        break;
    }
    if (testing6 != 6) {
        String old = tvAnswer6.getText().toString();
        tvAnswer6.setText(old + slice1);
        v.setVisibility(View.GONE);
        break;
    }});


 Button[] answerButtons = new Button[18];

 for(Button answer : answerButtons)
     answer.setOnClickListener(myOnClick);

请记住,不要在您的 Activity 中实现 OnClickListener,让视图自己处理它,除非您真的需要自己处理/调度 onClicks。

于 2013-02-07T11:39:36.560 回答