如果 - 否则如果不工作

时间:2018-02-04 19:29:25

标签: android if-statement

我不知道为什么这最后的if-else如果不起作用。

我正在尝试在项目(测验应用程序)同时检查2个复选框时创建事件。

我在监听器中放了一个log.v来查看listenCheck是否正常工作 - 而且确实如此。当我检查前两个复选框(q7R1,q7R2)为1时,它会递增,如果我检查其他两个复选框,则递减2(q7R3,q7R4)

谢谢

public class Main2Activity extends AppCompatActivity {
    int listenCheck = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final Button button = findViewById(R.id.sub);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });

        q7R1 = findViewById(R.id.q7checkBox1);
        q7R2 = findViewById(R.id.q7checkBox2);
        q7R3 = findViewById(R.id.q7checkBox3);
        q7R4 = findViewById(R.id.q7checkBox4);


        q7Img = findViewById(R.id.imgQ7);
        q7R1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton1, boolean b) {
                if (q7R1.isChecked()) {
                    listenCheck += 1;
                    Log.v("Main2Activity", "listenchk" + listenCheck);
                }

            }
        });

        q7R2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton2, boolean b) {

                if (q7R2.isChecked()) {
                    listenCheck += 1;
                    Log.v("Main2Activity", "listenchk" + listenCheck);
                }
            }
        });

        q7R3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton3, boolean b) {
                if (q7R3.isChecked()) {
                    listenCheck -= 2;
                    Log.v("Main2Activity", "listenchk" + listenCheck);
                }
            }
        });

        q7R4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton4, boolean b) {
                if (q7R4.isChecked()) {
                    listenCheck -= 2;
                    Log.v("Main2Activity", "listenchk" + listenCheck);
                }
            }
        });

        correctState = (ContextCompat.getColorStateList(getApplicationContext(), R.color.radionbuttonstate));
        wrongState = (ContextCompat.getColorStateList(getApplicationContext(), R.color.radiobuttonwrong));
        checkAnswers();
    }

    public void q7Check2() {

        if (listenCheck == 2) {
            score = score + (100 / 8);
            q7R1.setTextColor(correctState);
            q7R2.setTextColor(correctState);
            q7Img.setImageResource(R.drawable.ddvspan);
        } else if (listenCheck < 0) {
            q7R3.setTextColor(wrongState);
            q7R4.setTextColor(wrongState);
            Toast.makeText(getApplicationContext(), getResources().getString(R.string.correct7), Toast.LENGTH_SHORT).show();
        }

        Log.v("Main2Activity", "listenchk" + listenCheck);
    }

    public void checkAnswers() {
        q1Check();
        q2Check();
        q3Check();
        q4Check();
        q5Check();
        q6Check();
        q7Check2();
        q8Check();
    }
}

1 个答案:

答案 0 :(得分:0)

在每个onCheckedChanged()方法中只需调用q7Check2()方法

fetch (axios like module)