RadioButton强制关闭应用程序

时间:2016-03-31 18:32:44

标签: android radio-button

我的无线电组有问题,如果用户不选择答案,应用会强制关闭,如果用户选择答案,那么第二个问题的答案也是相同的。另外,当用户在按下下一个按钮时没有选择任何答案时,如何添加提示信息?你的回答将不胜感激。谢谢

这是java文件

  public class QuizActivity extends Activity{
        List<Question> quesList;
        int score=0;
        int qid=0;
        Question currentQ;
        TextView txtQuestion;
        RadioButton rda, rdb, rdc;
        Button butNext;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_quiz);
            DbHelper db=new DbHelper(this);
            quesList=db.getAllQuestions();
            currentQ=quesList.get(qid);
            txtQuestion=(TextView)findViewById(R.id.textView1);
            rda=(RadioButton)findViewById(R.id.radio0);
            rdb=(RadioButton)findViewById(R.id.radio1);
            rdc=(RadioButton)findViewById(R.id.radio2);
            butNext=(Button)findViewById(R.id.button1);
            setQuestionView();
            butNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {



                    RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
                    RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
                    Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
                    if(currentQ.getANSWER().equals(answer.getText()))
                    {
                        score++;
                        Log.d("score", "Your score"+score);
                    }
                    if(qid<5){
                        currentQ=quesList.get(qid);
                        setQuestionView();
                    }else{
                        Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                        Bundle b = new Bundle();
                        b.putInt("score", score); //Your score
                        intent.putExtras(b); //Put your score to your next Intent
                        startActivity(intent);
                        finish();
                    }
                }
            });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_quiz, menu);
            return true;
        }
        private void setQuestionView()
        {
            txtQuestion.setText(currentQ.getQUESTION());
            rda.setText(currentQ.getOPTA());
            rdb.setText(currentQ.getOPTB());
            rdc.setText(currentQ.getOPTC());
            qid++;
        }
    }

这是xml文件

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".QuizActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:layout_gravity="center">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.04">
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checked="false"
                android:text="RadioButton" />
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checked="false"
                android:text="RadioButton" />
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checked="false"
                android:text="RadioButton" />
        </RadioGroup>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/str_next"
                android:visibility="visible"/>
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果用户没有选择任何内容,您可以像这样控制它:

if(grp.getCheckedRadioButtonId() != null){
   //do something
}else{
    Toast.makeText(getActivity(), "You have to choose one option", Toast.LENGTH_SHORT).show();
}

关于下一个问题中的相同答案,您必须在所有单选按钮之前重置。像这样:

rda.setChecked(false);
rdb.setChecked(false);
rdc.setChecked(false);