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. 简单拓扑图
随机推荐
- [转]using components in Cakephp 2+ Shell
<?php App::uses('AppShell', 'Console/Command'); App::uses('ComponentCollection', 'Controller'); A ...
- tomcat缓存
问题描述: 一个用到struts2框架的web项目,由于在struts.xml中少配置了一个action,导致项目运行时报异常.将原本好的代码复旧,重启tomcat服务,第一次加载程序没问题,再刷新时 ...
- Azure Active Directory中的特权身份管理如何运作?
[TechTarget中国原创] 用户权限不是平等的.有些用户需要有大量权利和特权——通常这些都是管理员.企业在允许特权用户进行管理以及支持活动时,还需要意识到特权用户也有可能犯错.他们会犯错.他们可 ...
- socketCluster 使用
<html> <head> <title>test</title> <script src="https://cdn.bootcss.c ...
- leetcode 【 Sort List 】 python 实现
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码:oj 测试通过 Runtime: 372 m ...
- Windows核心编程小结1
这本书绝对经典,看看定会增加不少知识.当然这本书有很多东西比<Windows程序设计第五版>中的更加详细. 1.Unicode:宽字节字符集 这是一个国际的字符标准,16位,最大可支持65 ...
- 替换掉 in的like操作
select * from t_unit where '410300060025,410300004005,410300998851,' like '%'+ltrim(rtrim(unitcode)) ...
- [转]Docker容器内不能联网的6种解决方案
注: 下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.8.8.8) 1.使用--net:host选项 sudo docker ...
- jQuery动态显示和隐藏datagrid中的某一列的方法
在EasyUI中: 1)展示某列的方法: $('#jgrid').datagrid('showColumn', 'XXX'); -----其中 XXX 是隐藏列的 field 属性值 2) ...
- 习题:Wormhole(思路题)
tyvj1763 描述 一维的世界就是一个数轴.这个世界的狭小我们几乎无法想象.在这个数轴上,有N个点.从左到右依次标记为点1到N.第i个点的坐标为Xi.经过漫长时间的物理变化和化学变化,这个一维世界 ...