OnChencedChang
(一)
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();
}
OnChencedChang的更多相关文章
- 在Windows宿主机中连接虚拟机中的Docker容器
1. 简单拓扑图
随机推荐
- 微信小程序-----校园头条详细开发之注册登录
1.注册登录功能的实现 1.1结构 1.2 代码实现 1.2.1 为了通信的安全着想,在此我是通过小程序端获得code,然后传递给后端,在后端向微信后台发送api请求,解密,从而得到用户的唯一标示o ...
- 【转】tomcat与apache,tomcat与servlet的区别
tomcat与apache的区别:(转自:http://blog.csdn.net/longzs/article/details/10959945) 1.apache支持静态页,tomcat支持动态的 ...
- web自动化测试:watir+minitest(一)
基本介绍: 本课程的测试环境和工具为:win7+ruby+watir+minitest Watir 全称是"Web Application Testing in Ruby".它是一 ...
- Spring MVC与jQuery结合使用Ajax技术
gradle配置 group 'org.zln.webDemo' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'jetty' s ...
- [SDOI2009][bzoj1878] HH的项链 [莫队模板题]
题面: 传送门 思路: 就是一道莫队的模板题目...... 开一个1000000的数组记录每个数出现的次数,然后每次从1到0或者从0到1更新答案 莫队讲解看这里:莫队 Code: #include&l ...
- 永琳的竹林迷径(path)
永琳的竹林迷径(path) 题目描述 竹林可以看作是一个n 个点的树,每个边有一个边长wi,其中有k 个关键点,永琳需要破坏这些关键点才能走出竹林迷径. 然而永琳打算将这k 个点编号记录下来,然后随机 ...
- HashMap实现原理及源码分析(jdk1.8)
HashMap底层由数组+链表+红黑树组成,可接受null值,非线程安全 1.基本属性 transient Node<K,V>[] table; //hashmap数组 static fi ...
- 调试Java代码(Eclipse)汇总
Java 10个调试技巧(基础❤❤❤❤❤) Eclipse断点调试(和上一篇基本类似,补充❤❤) 使用Eclipse开发和调试java程序(从安装eclipse开始,特别细,有设置条件断点,回退的具体 ...
- Position 属性的学习理解
position 当时在学习的时候也没有进入深入的研究,主要是因为平时自己用的不是很多.今天看到了篇解释不错的文章就整理,学习下. http://www.cnblogs.com/bokin/archi ...
- linux文件属性详解及文件类型
一 drwxr-xr-x的意思解释: ls -al 得到如下列表: drwxr-xr-x oracle dba May : oralog1 drwxr-x--- root root May : ro ...