Android Studio RadioButton实现单选试题评分机制

效果实现图在这里插入图片描述Java代码:

public class MainActivity extends AppCompatActivity {


    private Button btnMainReset;
    private RadioGroup radGupDemo1;
    private RadioButton Radbtn1A;
    private RadioButton Radbtn1B;
    private RadioButton Radbtn1C;
    private RadioButton Radbtn1D;
    private RadioGroup radGupDemo2;
    private RadioButton Radbtn2A;
    private RadioButton Radbtn2B;
    private RadioButton Radbtn2C;
    private RadioButton Radbtn2D;
    private RadioGroup radGupDemo3;
    private RadioButton Radbtn3A;
    private RadioButton Radbtn3B;
    private RadioButton Radbtn3C;
    private RadioButton Radbtn3D;
    private RadioGroup radGupDemo4;
    private RadioButton Radbtn4A;
    private RadioButton Radbtn4B;
    private RadioButton Radbtn4C;
    private RadioButton Radbtn4D;
    private Button btnMainRefer;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //每个单选组添加监听器
        radGupDemo1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radBtn = findViewById(checkedId);
                radBtn.getText().toString();
                Log.i("RadioButton", radBtn.getText().toString());
            }
        });
        radGupDemo2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radBtn = findViewById(checkedId);
                radBtn.getText().toString();
                Log.i("RadioButton", radBtn.getText().toString());
            }
        });
        radGupDemo3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radBtn = findViewById(checkedId);
                radBtn.getText().toString();
                Log.i("RadioButton", radBtn.getText().toString());
            }
        });
        radGupDemo4.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radBtn = findViewById(checkedId);
                radBtn.getText().toString();
                Log.i("RadioButton", radBtn.getText().toString());
            }
        });
        btnMainRefer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int optionID1 = radGupDemo1.getCheckedRadioButtonId();
                int optionID2 = radGupDemo2.getCheckedRadioButtonId();
                int optionID3 = radGupDemo3.getCheckedRadioButtonId();
                int optionID4 = radGupDemo4.getCheckedRadioButtonId();
                if (optionID1 == R.id.Radbtn1A & optionID2==R.id.Radbtn2C & optionID3==R.id.Radbtn3B & optionID4==R.id.Radbtn4A) {
                    tvMainResult.setText("分数:100分");
                } else if (optionID1 == R.id.Radbtn1A & optionID2==R.id.Radbtn2C & optionID3==R.id.Radbtn3B || optionID2==R.id.Radbtn2C & optionID3==R.id.Radbtn3B & optionID4==R.id.Radbtn4A || optionID1 == R.id.Radbtn1A& optionID3==R.id.Radbtn3B & optionID4==R.id.Radbtn4A) {
                    tvMainResult.setText("分数:75分");
                }else if (optionID1 == R.id.Radbtn1A & optionID2==R.id.Radbtn2C || optionID1 == R.id.Radbtn3B& optionID3==R.id.Radbtn3B||optionID1 == R.id.Radbtn1A& optionID4==R.id.Radbtn4A|| optionID2==R.id.Radbtn2C & optionID3==R.id.Radbtn3B|| optionID2==R.id.Radbtn2C & optionID4==R.id.Radbtn4A|| optionID3==R.id.Radbtn3B & optionID4==R.id.Radbtn4A){
                    tvMainResult.setText("分数:50分");
                }else if (optionID1 == R.id.Radbtn1A  || optionID2==R.id.Radbtn2C  || optionID3==R.id.Radbtn3B || optionID4==R.id.Radbtn4A){
                    tvMainResult.setText("分数:25分");
                }else if (optionID1==-1||optionID2==-1||optionID3==-1||optionID4==-1 ){
                    Toast.makeText(MainActivity.this, "请作出选择", Toast.LENGTH_SHORT).show();
                }else{
                    tvMainResult.setText("分数:0分");
                }
            }

        });
        btnMainReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Radbtn1A.setChecked(false);
                Radbtn1B.setChecked(false);
                Radbtn1C.setChecked(false);
                Radbtn1D.setChecked(false);
                Radbtn2A.setChecked(false);
                Radbtn2B.setChecked(false);
                Radbtn2C.setChecked(false);
                Radbtn2D.setChecked(false);
                Radbtn3A.setChecked(false);
                Radbtn3B.setChecked(false);
                Radbtn3C.setChecked(false);
                Radbtn3D.setChecked(false);
                Radbtn4A.setChecked(false);
                Radbtn4B.setChecked(false);
                Radbtn4C.setChecked(false);
                Radbtn4D.setChecked(false);
                tvMainResult.setText("");
               Toast.makeText(MainActivity.this,"重置完毕",Toast.LENGTH_SHORT).show();

            }
        });


    }


    private void initView() {
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
        radGupDemo1 = (RadioGroup) findViewById(R.id.radGupDemo1);
        Radbtn1A = (RadioButton) findViewById(R.id.Radbtn1A);
        Radbtn1B = (RadioButton) findViewById(R.id.Radbtn1B);
        Radbtn1C = (RadioButton) findViewById(R.id.Radbtn1C);
        Radbtn1D = (RadioButton) findViewById(R.id.Radbtn1D);
        radGupDemo2 = (RadioGroup) findViewById(R.id.radGupDemo2);
        Radbtn2A = (RadioButton) findViewById(R.id.Radbtn2A);
        Radbtn2B = (RadioButton) findViewById(R.id.Radbtn2B);
        Radbtn2C = (RadioButton) findViewById(R.id.Radbtn2C);
        Radbtn2D = (RadioButton) findViewById(R.id.Radbtn2D);
        radGupDemo3 = (RadioGroup) findViewById(R.id.radGupDemo3);
        Radbtn3A = (RadioButton) findViewById(R.id.Radbtn3A);
        Radbtn3B = (RadioButton) findViewById(R.id.Radbtn3B);
        Radbtn3C = (RadioButton) findViewById(R.id.Radbtn3C);
        Radbtn3D = (RadioButton) findViewById(R.id.Radbtn3D);
        radGupDemo4 = (RadioGroup) findViewById(R.id.radGupDemo4);
        Radbtn4A = (RadioButton) findViewById(R.id.Radbtn4A);
        Radbtn4B = (RadioButton) findViewById(R.id.Radbtn4B);
        Radbtn4C = (RadioButton) findViewById(R.id.Radbtn4C);
        Radbtn4D = (RadioButton) findViewById(R.id.Radbtn4D);
        btnMainRefer = (Button) findViewById(R.id.btnMainRefer);
        tvMainResult = (TextView) findViewById(R.id.tvMainResult);
    }
}
原创文章 6 获赞 2 访问量 145

猜你喜欢

转载自blog.csdn.net/weixin_46267489/article/details/105899665
今日推荐