(一)

1,布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app5.MainActivity"> <TextView android:layout_width="wrap_content"
android:id="@+id/tv"
android:layout_height="wrap_content"
android:text="选择正确答案:" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:text="2+1=2"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:text="2+1=3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb3"
android:text="2+1=4"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb4"
android:text="2+1=1"/> </RadioGroup> </LinearLayout>

注意事项:RadioButton需要包含在RadioGroup内部才能实现单选的效果

2,逻辑代码的处理

package helloworld.com.inspur.app5;

import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
private TextView tv;
private RadioGroup rg;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb4;
private RadioButton rb3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
rg=(RadioGroup)findViewById(R.id.rg);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb4=(RadioButton)findViewById(R.id.rb4);
rb3=(RadioButton)findViewById(R.id.rb3);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
case R.id.rb1:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show(); break;
case R.id.rb2:
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
break;
case R.id.rb3:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
break;
case R.id.rb4:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
break; }
}
}); }
}

注意:

 Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
在后面加上.show()才会显示。

(二)if判断id是否相同

if(R.id.rb2==checkedId)
{
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
}

(三)if判断对象是否相同

RadioButton r=(RadioButton)findViewById(checkedId);
if(rb2.equals(r))
{
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
}

最新文章

  1. Sublime Text 3插件安装
  2. 使用Guava提供的filter过滤集合
  3. SSIS Component的ValidateExternalMetadata属性
  4. linux中非root用户使用wireshark进行抓包
  5. DTO概念
  6. 【转】来自《轻松scrum之旅》的敏捷开发总结
  7. MySQL5.6 windows7下安装及基本操作
  8. nginx的配置说明
  9. C#List&lt;long&gt;与String(Linq)
  10. 201521123042《Java程序设计》 第9周学习总结
  11. jquery插件存档
  12. Win10系列:C#应用控件进阶8
  13. 两段锁协议(Two-Phase Locking――2PL)
  14. mysql 在原有的时间上加10个月或者一年
  15. 使用Redis数据库(1)(三十三)
  16. intellij怎么导入MySQL的驱动包
  17. orcale 函数wm_concat不存咋lob值使用zh_concat 替换
  18. C++几个重要关键字(包含借鉴其他博主的东西)
  19. JQuery UI - draggable(转)
  20. android之滑屏的实现

热门文章

  1. sqoop安装和使用
  2. Django admin模块使用search时报错:django.core.exceptions.FieldError: Related Field got invalid lookup: contains
  3. python中subprocess.Popen的args和shell参数的使用
  4. JWT实现token的生成和认证demo
  5. python 学习分享-函数篇
  6. Webapp和后端交互检查测试
  7. 用nc+简单bat/vbs脚本+winrar制作迷你远控后门
  8. Mysql实战之数据备份
  9. [nowcoder_Wannafly挑战赛4_F]线路规划
  10. vue2搭建简易spa