1.效果图:选择正确的提示选对,选择错误提示选错

2.activity_main.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.app5.MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择正确的题目" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rb_1"
android:text="1+1=3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rb_2"
android:text="1+1=2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rb_3"
android:text="1+1=4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/rb_4"
android:text="1+1=5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </RadioGroup>
</LinearLayout>

2.MainActivity.java

 package com.example.app5;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
private RadioGroup rg;
private RadioButton rb_1,rb_2,rb_3,rb_4; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg=(RadioGroup)findViewById(R.id.rg);
rb_1 = (RadioButton) findViewById(R.id.rb_1);
rb_2 = (RadioButton) findViewById(R.id.rb_2);
rb_3= (RadioButton) findViewById(R.id.rb_3);
rb_4 = (RadioButton) findViewById(R.id.rb_4); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) { //switch实现方式
/*switch (checkedId){
case R.id.rb_1:
Toast.makeText(MainActivity.this,"您选错了"+rb_1.isChecked(),Toast.LENGTH_SHORT).show();
break;
case R.id.rb_2:
Toast.makeText(MainActivity.this,"您选对了"+rb_2.isChecked(),Toast.LENGTH_SHORT).show();
break;
case R.id.rb_3:
Toast.makeText(MainActivity.this,"您选错了"+rb_3.isChecked(),Toast.LENGTH_SHORT).show();
break;
case R.id.rb_4:
Toast.makeText(MainActivity.this,"您选错了"+rb_4.isChecked(),Toast.LENGTH_SHORT).show();
break;*/ //if 判断实现方式
/* if(R.id.rb_2==checkedId){
Toast.makeText(MainActivity.this,"正确"+rb_2.isChecked(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show();
}*/ //对象的实现方式
RadioButton r = (RadioButton) findViewById(checkedId);
if(r.equals(rb_2)){
Toast.makeText(MainActivity.this,"正确"+rb_2.isChecked(),Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show();
}
}
}); }
}

2.效果图:多选按钮,选择哪个之后提示选择了XX

(1)activity_main.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.app6.MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的爱好是" />
<CheckBox
android:id="@+id/cb_1"
android:text="sing"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb_2"
android:text="game"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb_3"
android:text="eat food"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

(2)MainActivity.java

 package com.example.app6;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { private CheckBox cb_1,cb_2,cb_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb_1=(CheckBox)findViewById(R.id.cb_1);
cb_2=(CheckBox)findViewById(R.id.cb_2);
cb_3=(CheckBox)findViewById(R.id.cb_3); cb_1.setOnCheckedChangeListener(new myListener());
cb_2.setOnCheckedChangeListener(new myListener());
cb_3.setOnCheckedChangeListener(new myListener()); }
class myListener implements CompoundButton.OnCheckedChangeListener { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //switch实现
/* switch (buttonView.getId()){
case R.id.cb_1:
Toast.makeText(MainActivity.this,cb_1.getText().toString()+" "+((CheckBox)buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
break;
case R.id.cb_2:
Toast.makeText(MainActivity.this,cb_2.getText().toString()+" "+((CheckBox)buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
break;
case R.id.cb_3:
Toast.makeText(MainActivity.this,cb_3.getText().toString()+" "+((CheckBox)buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
break;
}*/ //if实现
if(isChecked){
Toast.makeText(MainActivity.this,buttonView.getText().toString()+" "+(buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(MainActivity.this,buttonView.getText().toString()+" "+(buttonView).isChecked() ,Toast.LENGTH_SHORT).show(); }
}
}
}

3.效果图

(1)activity_main.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"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.app7.MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的爱好是" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/sing"
android:text="sing"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/game"
android:text="game"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/eat"
android:text="eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/bt"
android:text="提交"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

(2)MainAcivity.xml

 package com.example.app7;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
private Button bt;
private CheckBox sing;
private CheckBox game;
private CheckBox eat;
private List<CheckBox> list;
private int count=0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sing = (CheckBox) findViewById(R.id.sing);
game = (CheckBox) findViewById(R.id.game);
eat = (CheckBox) findViewById(R.id.eat);
bt = (Button) findViewById(R.id.bt);
list = new ArrayList<>();
list.add(sing);
list.add(game);
list.add(eat); bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();;
for (CheckBox checkbox:list){
if (checkbox.isChecked()){
count++;
sb.append(checkbox.getText().toString()+" ");
}
}
if(sb==null||"".equals(sb.toString())){
Toast.makeText(MainActivity.this,"至少选择一项",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,sb.toString()+",共"+count+"项",Toast.LENGTH_SHORT).show();
count=0;
}
}
});
}
}

选择改变事件OnCheckedChange的更多相关文章

  1. DropDownList 下拉框选择改变,促发事件和防全局刷新(记录)

    代码: <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:Script ...

  2. 下拉框改变事件:获取下拉框中当前选择的文本 SelectionChanged事件

    /// <summary> /// 下拉框改变事件:获取下拉框中当前选择的文本 /// </summary> /// <param name="sender&q ...

  3. select change下拉框改变事件 设置选定项,禁用select

    select change下拉框改变事件 设置选定项,禁用select 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

  4. js获取select改变事件

    js获取select改变事件onchage前的值 和 onclick事件 <select id="wupin_id" name="wupin_id" on ...

  5. Android 监听EditView中的文本改变事件

    android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面的搜索列表就显示有包含输入关键字的选项,这个输入监听怎么实现的呢? 我们可以建一个例子,效果图如下: 我们可以监 ...

  6. ComboBox赋值ItemsSource数据源的时候会触发SelectionChanged改变事件的解决办法

    我用的方法是设置开关 bool flag = false;//默认开关关闭(全局变量) flag = false;在赋值数据源之前设置关闭box.ItemsSource = lstProperty;/ ...

  7. div、span绑定内容改变事件

    内容改变事件onchange只适用于form表单标签(input.select.textarea) 当需要对div.span标签进行内容改变监听则无法适用,查阅了一些资料发现jquery有针对的方法, ...

  8. 单选框radio改变事件详解(用的jquery的radio的change事件)

    单选框radio改变事件详解(用的jquery的radio的change事件) 一.总结 1.用的jquery的radio的change事件:当元素的值发生改变时,会发生 change 事件,radi ...

  9. 监听EditView中的文本改变事件详解--转

    转自: http://blog.csdn.net/zoeice/article/details/7700529 android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面 ...

随机推荐

  1. Javascript 的addEventListener()及attachEvent()区别分析

    大家都知道事件的用法就是当某个事件(状况)被触发了之后就会去执行某个Function, 尤其是Javascript, 在当红AJAX的催化下, 了解Javascript的Event用法更加重要, 在这 ...

  2. npm安装node-sass失败,EACCES: permission denied

    增加--unsafe-perm,即 sudo npm install node-sass --unsafe-perm --save-dev 成功安装node-sass

  3. Fragment使用--文章集锦

    android使用Fragment实现底部菜单使用show()和hide()来切换以保持Fragment状态 Android Fragment 真正的完全解析(上) Android Fragment实 ...

  4. 粉刷匠(bzoj 1296)

    Description windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色. 每个 ...

  5. centos 下构建lamp环境

    构建准备: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp - ...

  6. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  7. TCP的三次握手和四次挥手+TCP和UDP的区别

    TCP的三次握手: LISTEN:表示服务器端的某个socket处于监听状态,可以接收连接了. SYN_SENT:当客户端SOCKET执行connect连接时,它首先发送syn报文,随即会进入到此状态 ...

  8. 定制一个支持中英文的简单LaTex模板

    平常写汇报文档什么的,word排版有时还是比较费劲,遂定制一个简单的LaTex模板,中文默认为宋体,英文为LaTex默认字体,支持彩色高亮展示,有目录书签,有页眉展示,大致如下: LaTex代码如下: ...

  9. django自带的orm增删改

    # 转载请留言联系 模型管理器 模型管理器:objects属性 每个模型类默认都有一个叫 objects 的类属性,它由django自动生成 我们把 objects 称为 模型管理器,其类型为: dj ...

  10. Java中的标记接口(zz)

    1.什么是标记接口? Java中把没有定义任何方法和常量的接口称之为标记接口,我们经常使用的比较多的是“”Serializable“”,这个接口也是没有定义人任何方法和常量的. 2.标记接口的作用? ...