我如何在RadioButton中返回分数? (进行测试)

时间:2014-08-14 07:52:45

标签: android oop radio-button

我正在进行MC测试。我想计算得分正确的ans。 这是我的代码。我想在onRadioButtonClicked()中返回分数,我该怎么做?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_general_test, container, false);

    handInBtn=(Button) rootView.findViewById(R.id.handInBtn);       
    handInBtn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            Toast toast = Toast.makeText(getActivity(), scores+"", Toast.LENGTH_LONG);
            toast.show();
        }});

    return rootView;
}

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.Radio1a:
            if (checked)
                scores++;
        case R.id.Radio2a:
            if (checked)
                scores++;
    }
}

1 个答案:

答案 0 :(得分:0)

在android中你可以创建一个无线电组,并且有一个redio组监听器,这样你就可以获得选择的单选按钮并获得该按钮的值,如果正确则比较正确的答案,然后增加其他离开。

for Example.
RadioGroup
    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
       Log.d("chk", "id" + checkedId);

            if (checkedId.gettext() == "Correct answer") {
                //some code
   correct++

            } else  {
                //some code
            }

        }

    });