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. 很轻很强大:轻量级桌面环境比较(转自linuxeden)

    这天你终于下定决心购买了一台流行的 Netbook ,与往常装机一样,直接安装心爱的 Linux 发行版.好不容易安装完成了,却发现平日启动飞快的应用程序在 Netbook 上怎么都跑不快.怎么办呢? ...

  2. C中字符串分割函数strtok的一个坑

    strtok的典型用法是: p = strtok(s4, split); while(p != NULL){ printf("%s\n", p); p = strtok(NULL, ...

  3. 最简易的PHP Storm调试模式开启方式

    使用的是xdebug调试工具. 其实真的很想吐槽php语言开启调试模式真的好麻烦. 据说xdebug调试工具不支持php7.0以下版本,我同事的php5.6就不支持噗嗤.. 正文: 1.先用phpSt ...

  4. C#基础 - C# 的 常见概念简述

    在上篇文章中,你跟着我写了一个HelloWorld,本篇中,我们来谈谈一些C#程序中的小概念 1.C# 程序结构 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declarat ...

  5. OGG_GoldenGate安装和环境搭建(案例)

    2014-03-02 Created By BaoXinjian

  6. 安卓解析 json 4种格式 全解析

    总共4种json  ,    还提供了 httclient  和 httpurlconnection 2种方式获取json的方法(请把baseurl 改为可以地址比如http://www.sohu.c ...

  7. setContentView(R.layout.activity_main)无法正常引用

    今天在写Android代码的过程中,编译器一直报错,错误出在这一行代码: setContentView(R.layout.activity_main) 提示信息是: activity_main can ...

  8. Externalizable的使用方法

    package com.itbuluoge.object; import java.io.Externalizable; import java.io.FileInputStream; import ...

  9. [转]玩转UltraEdit,UE常见快捷键操作

    编辑器Ultraedit快捷键说到编辑器的快捷键,VIM是无与伦比的.要反对,也得是带脚踏板的EmaCS.UE还是有差距的,很大差距.注意:VIM是开源.免费的,而UE则需要注册.UE是Windows ...

  10. Apache 隐藏 index.php,如将 tp5.com/index.php/hello/123 变成 tp5.com/hello/123

          以Apache为例,需要在index.php入口文件的同级添加.htaccess文件,内容如下: <IfModule mod_rewrite.c> Options +Follo ...