xml

<?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:orientation="vertical"
tools:context="com.example.chenshuai.test322.UIActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择Android的开发语言是什么?"
android:padding="10dp"/> <RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rg"> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C++"
android:id="@+id/rb1"
android:layout_marginRight="30dp"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/rb2"
android:layout_marginRight="30dp"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="JAVA"
android:id="@+id/rb3"
android:layout_marginRight="30dp"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C#"
android:id="@+id/rb4"
/>
</RadioGroup> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择字体效果:"
android:id="@+id/ziti"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="宋体"
android:checked="true"
android:id="@+id/cb_song"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加粗"
android:id="@+id/cb_cu"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="斜体"
android:id="@+id/cb_xie"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下划线"
android:id="@+id/cb_xia"/> </LinearLayout>

java

package com.example.chenshuai.test322;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class UIActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ui); RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg); //radiogroup的监听事件 匿名内部类
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb = (RadioButton) findViewById(checkedId); if (checkedId == R.id.rb3) {
Toast.makeText(UIActivity.this, "选对了", Toast.LENGTH_LONG).show();
}
Toast.makeText(UIActivity.this, rb.getText(), Toast.LENGTH_LONG).show(); }
});
CheckBox cb_song = (CheckBox)findViewById(R.id.cb_song);
cb_song.setOnCheckedChangeListener(new cboncheckedchangelistener()); CheckBox cb_cu = (CheckBox)findViewById(R.id.cb_cu);
cb_cu.setOnCheckedChangeListener(new cboncheckedchangelistener()); CheckBox cb_xia = (CheckBox)findViewById(R.id.cb_xia);
cb_xia.setOnCheckedChangeListener(new cboncheckedchangelistener()); CheckBox cb_xie = (CheckBox)findViewById(R.id.cb_xie);
cb_xie.setOnCheckedChangeListener(new cboncheckedchangelistener()); } //checkbox的监听事件 内部类
private class cboncheckedchangelistener implements CompoundButton.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CheckBox cb = (CheckBox)buttonView; if (isChecked)
{
Toast.makeText(UIActivity.this, "选中了"+cb.getText(), Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(UIActivity.this, "取消选中了"+cb.getText(), Toast.LENGTH_SHORT).show();
} }
}
}

Android——RadioGroup和CheckBox的更多相关文章

  1. Android UI系列-----CheckBox和RadioButton(1)

    主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...

  2. Android ExpandableListView 带有Checkbox的简单应用

    expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...

  3. android listview 使用checkbox问题

    在android中使用listview时需要了解listview加载数据的原理,为了避免listview由于列表项过多每次需要进行new造成性能低下的问题,android中的listview使用了控件 ...

  4. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  5. Android ListView带CheckBox实现单选

    第1种方法: 首先是我们的bean: public class Bean { private boolean isChecked; private String msg = "这是一条测试数 ...

  6. Android Studio 之 CheckBox

    •任务 •基本用法 CheckBox,复选框,即可以同时选中多个选项. 从网上找了三个图标,分别命名为 apple.jpg , banana.jpg , oranges.jpg 放置在了 drawab ...

  7. Android RadioGroup和RadioButton详解

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...

  8. Android学习笔记——CheckBox

    该工程的功能实现在一个activity中显示一个单选框和一个多选框 以下代码是MainActivity.java文件中的代码 package com.example.checkbox; import ...

  9. Android RadioGroup 及资源文件 & selector

    RadioGroup :单选组         RadioButton :单选按钮 RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单 ...

随机推荐

  1. HDUOJ--畅通工程

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. 入门Nginx

    一.正向代理和反向代理 正向代理举例:翻越万里长城去游览墙外的景色 反向代理举例:负载均衡 正向代理和反向代理涉及三个主体: 请求方 代理 被请求方 正向代理中,代理跟请求方是一家子,请求方说要啥,代 ...

  3. JavaScript 设计模式之简介

    一.设计模式概念解读 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.无数实战代码设计经验的总结.使用设计模式是为了让系统代码可重用.可扩展.可解耦.更容易被人 ...

  4. xp看系统位数

      运行cmd,看打开的cmd窗口标题一般c:\windows\system32代表32位操作系统,64位就是system64,详细系统信息运行命令“systeminfo”  

  5. Quartz.net官方开发指南[转]

    http://www.cnblogs.com/shanyou/category/102991.html

  6. 非IE内核浏览器如何支持activex插件

    原文地址:https://blog.csdn.net/johnson2008t/article/details/46126605 之前在一个B/S项目中遇到一个需求,就是客户需要在页面上对报表的布局以 ...

  7. iOS开发打包

    ios11个论坛地址   邓白氏编码    邓白氏申请 http://blog.fir.im/faq/ AdHoc,InHouse,App Store Account Type: InHouse  ( ...

  8. 固定尺寸内存块的缓冲队列类及C++实现源代码

    -------------------------------------------------------------------------------- 标题: 固定尺寸内存块的缓冲队列类及实 ...

  9. Kali 2.0最新国内源:阿里云,中科大

    版权声明:本文为博主原创文章,转载请注明来源. https://blog.csdn.net/liushulin183/article/details/51519628  刚刚要给kali装个中文输入法 ...

  10. redis投票计数

    <?php /** * * 缓存利用测试,这里我们获取传过来的投票数据,每次加1,如果增加到了设定值,才将投票 * 次数写回mysql,这大大减轻了与mysql链接的开销,redis的使用由此可 ...