1.效果图:选择正确的提示选对,选择错误提示选错
2.activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="com.example.app5.MainActivity"> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="请选择正确的题目" /> 13 <RadioGroup 14 android:id="@+id/rg" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content"> 17 <RadioButton 18 android:id="@+id/rb_1" 19 android:text="1+1=3" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" /> 22 <RadioButton 23 android:id="@+id/rb_2" 24 android:text="1+1=2" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" /> 27 <RadioButton 28 android:id="@+id/rb_3" 29 android:text="1+1=4" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" /> 32 <RadioButton 33 android:id="@+id/rb_4" 34 android:text="1+1=5" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" /> 37 38 </RadioGroup> 39 </LinearLayout>
2.MainActivity.java
1 package com.example.app5; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.RadioButton; 6 import android.widget.RadioGroup; 7 import android.widget.Toast; 8 9 public class MainActivity extends AppCompatActivity { 10 private RadioGroup rg; 11 private RadioButton rb_1,rb_2,rb_3,rb_4; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 rg=(RadioGroup)findViewById(R.id.rg); 18 rb_1 = (RadioButton) findViewById(R.id.rb_1); 19 rb_2 = (RadioButton) findViewById(R.id.rb_2); 20 rb_3= (RadioButton) findViewById(R.id.rb_3); 21 rb_4 = (RadioButton) findViewById(R.id.rb_4); 22 23 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 24 @Override 25 public void onCheckedChanged(RadioGroup group, int checkedId) { 26 27 //switch实现方式 28 /*switch (checkedId){ 29 case R.id.rb_1: 30 Toast.makeText(MainActivity.this,"您选错了"+rb_1.isChecked(),Toast.LENGTH_SHORT).show(); 31 break; 32 case R.id.rb_2: 33 Toast.makeText(MainActivity.this,"您选对了"+rb_2.isChecked(),Toast.LENGTH_SHORT).show(); 34 break; 35 case R.id.rb_3: 36 Toast.makeText(MainActivity.this,"您选错了"+rb_3.isChecked(),Toast.LENGTH_SHORT).show(); 37 break; 38 case R.id.rb_4: 39 Toast.makeText(MainActivity.this,"您选错了"+rb_4.isChecked(),Toast.LENGTH_SHORT).show(); 40 break;*/ 41 42 //if 判断实现方式 43 /* if(R.id.rb_2==checkedId){ 44 Toast.makeText(MainActivity.this,"正确"+rb_2.isChecked(),Toast.LENGTH_SHORT).show(); 45 } else { 46 Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show(); 47 }*/ 48 49 //对象的实现方式 50 RadioButton r = (RadioButton) findViewById(checkedId); 51 if(r.equals(rb_2)){ 52 Toast.makeText(MainActivity.this,"正确"+rb_2.isChecked(),Toast.LENGTH_SHORT).show(); 53 }else{ 54 Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show(); 55 } 56 } 57 }); 58 59 60 } 61 }