首页 > 解决方案 > 如何计算不同的无线电组?

问题描述

我在 android studio 中进行了 12 个问题的测试,并且很想知道如何创建一个结果按钮来计算 12 个单选组,每个单选按钮有 3 个单选按钮?我在每个问题中添加了 sharedPrefences 以保存选定的按钮。

RadioButton radioButton31, radioButton32, radioButton33, radioButton34, radioButton35, radioButton36;

RadioGroup radioGroup11, radioGroup12;

Button calcularButton;

@Override
public void onBackPressed() {
    startActivity (new Intent(this, MainActivity12.class));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main13);

    txt31=findViewById(R.id.txt31);
    txt32=findViewById(R.id.txt32);

    radioButton31=findViewById(R.id.radioButton31);
    radioButton32=findViewById(R.id.radioButton32);
    radioButton33=findViewById(R.id.radioButton33);
    radioButton34=findViewById(R.id.radioButton34);
    radioButton35=findViewById(R.id.radioButton35);
    radioButton36=findViewById(R.id.radioButton36);

    calcularButton=findViewById(R.id.calcularButton);

    radioGroup11 = findViewById(R.id.radioGroup11);
    RadioButton checkedButton11 = findViewById(radioGroup11.getCheckedRadioButtonId());

    radioGroup11.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            SharedPreferences settings = getSharedPreferences("RespuestasTest", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("radioButton33", radioButton33.isChecked()); // first argument is a name of a data that you will later use to retrieve it and the second argument is a value that will be stored
            editor.putBoolean("radioButton32", radioButton32.isChecked());
            editor.putBoolean("radioButton31", radioButton31.isChecked());
            editor.commit();
        }
    });

    radioGroup12 = findViewById(R.id.radioGroup12);
    RadioButton checkedButton12 = findViewById(radioGroup12.getCheckedRadioButtonId());

    radioGroup12.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            SharedPreferences settings = getSharedPreferences("RespuestasTest", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("radioButton36", radioButton36.isChecked()); 
            editor.putBoolean("radioButton35", radioButton35.isChecked());
            editor.putBoolean("radioButton34", radioButton34.isChecked());
            editor.commit();

        }
    });




    calcularButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(MainActivity13.this, MainActivity14.class);
            startActivity(intent);
        }
    });
}

}

我对此测试有 5 个意图,因此结果将基于所选的高选项。例如.. 问题 ABC 问题 2 ABC

所以,我们有 12 个问题和 3 个选项,应该给出一个像 1、2 或 3 这样的 int num,然后计算每个答案,如果我们的 1 比 2 或 3 多,那么该页面将是另一个显示信息的意图结果1。

谢谢!

标签: javaandroidkotlintesting

解决方案


您可以使用 RadioGroup 创建自定义视图,并可以在主布局中使用该视图。这是一个带有代码的示例:

自定义视图

CustomViewXml

在您的活动中:

与监听器的活动


推荐阅读