将特定值分配给复选框

时间:2018-08-06 01:39:04

标签: android

我目前正在编写调查表,据此,当回答每个问题时,将分配一个数值。我可以使用以下代码通过单选按钮成功完成此操作:

        radioGroup1f.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int a) {
            int finalScore = Values.getInstance().updateScore(a);
            tvf.setText(" " + finalScore);

        }
    });
    radioGroup2f.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int b) {
            int finalScore = Values.getInstance().updateScore(b);
            tvf.setText(" " + finalScore);
        }
    });

我如何对复选框应用类似的编码,以便如果选中它,则分配一个值,如果不是,则该值应为0?我已经尝试过类似的方法,但是它不起作用

 radioGroup9f = (RadioGroup) view.findViewById(R.id.radioGroupQ9F);
    tvf = (TextView) view.findViewById(R.id.textView4f);
    checkBox8a = (CheckBox) view.findViewById(R.id.checkBoxQ8Fa);
    checkBox8b = (CheckBox) view.findViewById(R.id.checkBoxQ8Fb);
    checkBox8c = (CheckBox) view.findViewById(R.id.checkBoxQ8Fc);
    checkBox8d = (CheckBox) view.findViewById(R.id.checkBoxQ8Fd);
    checkBox8e = (CheckBox) view.findViewById(R.id.checkBoxQ8Fe);
    checkBox8f = (CheckBox) view.findViewById(R.id.checkBoxQ8Ff);



            radioGroup9f.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, @IdRes int a) {
                    int finalScore = Values.getInstance().updateScore(a);
                    tvf.setText(" " + finalScore);

                }
            });


            checkBox8a.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    int finalScore = Values.getInstance().updateScore(7);
                    tvf.setText(" " + finalScore);

                }
            });

            checkBox8b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean c) {
                    int finalScore = Values.getInstance().updateScore(7);
                    tvf.setText(" " + finalScore);

                }
            });

    checkBox8c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean d) {
            int finalScore = Values.getInstance().updateScore(7);
            tvf.setText(" " + finalScore);

        }
    });

    checkBox8d.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean e) {
            int finalScore = Values.getInstance().updateScore(7);
            tvf.setText(" " + finalScore);

        }
    });

    checkBox8e.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean f) {
            int finalScore = Values.getInstance().updateScore(7);
            tvf.setText(" " + finalScore);

        }
    });
    checkBox8f.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean g) {
            int finalScore = Values.getInstance().updateScore(7);
            tvf.setText(" " + finalScore);

        }
    });


    btnFragFemale3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (radioGroup9f.getCheckedRadioButtonId() == -1) {
                        Toast.makeText(getActivity(), "All Questions Need to be Answered", Toast.LENGTH_SHORT).show();
                    } else {

                        ((MainActivityFemale) getActivity()).setViewPager(3);
                    }
                }
            });


            return view;


    }

}

值页面如下:

public class Values {

private static Values instance;
private int QUESTIONS_COUNT = 45;
private int[] scores = new int[QUESTIONS_COUNT];

private Values(){

}

//Todo take a look with regards to thread safe access
public static Values getInstance(){
    if(instance == null){
        instance = new Values();
    }
    return instance;
}

public int updateScore(int checkedId) {
    switch(checkedId){

             case R.id.checkBoxQ8Fa:
            scores[7] = (8);
            break;
        case R.id.checkBoxQ8Fb:
            scores[7] = (0);
            break;
        case R.id.checkBoxQ8Fc:
            scores[7] = 6;
            break;
        case R.id.checkBoxQ8Fd:
            scores[7] = 6;
            break;
        case R.id.checkBoxQ8Fe:
            scores[7] = 10;
            break;
        case R.id.checkBoxQ8Ff:
            scores[7] = 10;
            break;

        //Q9 for female
        case R.id.radioButtonQ9Fa:
            scores[8] = (-5);
            break;
        case R.id.radioButtonQ9Fb:
            scores[8] = (-5);
            break;
        case R.id.radioButtonQ9Fc:
            scores[8] = 10;
            break;
        case R.id.radioButtonQ9Fd:
            scores[8] = 10;
            break;














    }

    int finalScore = 0;
    for(int score :  scores){
        finalScore += score;
    }
    return finalScore;
}

}

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

即使>>> list(get_multiples(79, 400, 6, 10, 20)) [120, 180, 240, 300, 360] 拥有CheckBox。您可以按照以下步骤进行操作:

setOnCheckedChangeListener