选择改变事件OnCheckedChange
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的更多相关文章
- DropDownList 下拉框选择改变,促发事件和防全局刷新(记录)
代码: <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:Script ...
- 下拉框改变事件:获取下拉框中当前选择的文本 SelectionChanged事件
/// <summary> /// 下拉框改变事件:获取下拉框中当前选择的文本 /// </summary> /// <param name="sender&q ...
- select change下拉框改变事件 设置选定项,禁用select
select change下拉框改变事件 设置选定项,禁用select 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
- js获取select改变事件
js获取select改变事件onchage前的值 和 onclick事件 <select id="wupin_id" name="wupin_id" on ...
- Android 监听EditView中的文本改变事件
android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面的搜索列表就显示有包含输入关键字的选项,这个输入监听怎么实现的呢? 我们可以建一个例子,效果图如下: 我们可以监 ...
- ComboBox赋值ItemsSource数据源的时候会触发SelectionChanged改变事件的解决办法
我用的方法是设置开关 bool flag = false;//默认开关关闭(全局变量) flag = false;在赋值数据源之前设置关闭box.ItemsSource = lstProperty;/ ...
- div、span绑定内容改变事件
内容改变事件onchange只适用于form表单标签(input.select.textarea) 当需要对div.span标签进行内容改变监听则无法适用,查阅了一些资料发现jquery有针对的方法, ...
- 单选框radio改变事件详解(用的jquery的radio的change事件)
单选框radio改变事件详解(用的jquery的radio的change事件) 一.总结 1.用的jquery的radio的change事件:当元素的值发生改变时,会发生 change 事件,radi ...
- 监听EditView中的文本改变事件详解--转
转自: http://blog.csdn.net/zoeice/article/details/7700529 android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面 ...
随机推荐
- PHP设计模式-工厂模式、单例模式、注册模式
本文参考慕课网<大话PHP设计模式>-第五章内容编写,视频路径为:http://www.imooc.com/video/4876 推荐阅读我之前的文章:php的设计模式 三种基本设计模式, ...
- java封装的使用
一:前言 其实以前我们来学习java特性的时候,对于封装好想觉得没什么用处,至少我那个时候的感觉(不知道是不是我学的太浅薄了~),现在由于项目从零开始做得,在做得过程中我感觉到原来封装是这样用的. 二 ...
- jquery中lhgdialog插件(一)
一:前言 最近在使用jquery的控件,其实以前也写但是突然之间遇到了需要从弹出窗口传值到父窗口,突然觉得这种传值的方式其实也是需要javascript的基础的,但是我自己还没有去真正的做过,所以还是 ...
- 计算机网络中七层,五层,四层协议;IP 地址子网划分
七层协议: 7 应用层(http) 6 表示层(上层用户可以相互识别的数据:jpg) 5 会话层(不同主机不同线程间的通信) 4 运输层(tcp/ip:传输层提供端到端的透明数据服务)/差错控制和流量 ...
- Codeforces 940F Machine Learning 带修改莫队
题目链接 题意 给定一个长度为\(n\)的数组\(a\),\(q\)个操作,操作分两种: 对于区间\([l,r]\),询问\(Mex\{c_0,c_1,c_2,⋯,c_{10^9}\}\),其中\(c ...
- HDU2441 ACM(Array Complicated Manipulation)
ACM(Array Complicated Manipulation) Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- textbox自动提示
AutoCompleteStringCollection myCutomSource = new AutoCompleteStringCollection(); myCutomS ...
- 安装VMware Tools的步骤和那些坑
背景环境:VMware workstation 12.5+Ubuntu16.04 首先VMware Tools在ubuntu中是及其不稳定的,也就是说,当你点击菜单栏中的install vmware ...
- Oracle rman 脚本
1.shell脚本1)vi rman_backup.cmd#rman_backup.cmdconnect target /run{ allocate channel d1 device type d ...
- HDU 3507 Print Article(斜率优化推导)
$dp$,斜率优化. 第一次做斜率优化的题目,看了一些题解,自己总结一下. 这题是说有$n$个数字,可以切成任意段,每一段的费用是这一段数字的和平方加上$M$.问最小费用是多少. 设$dp[i]$为$ ...