选择改变事件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也比较常用,那比如在搜索框中,没输入一个字,下面 ...
随机推荐
- 团队代码中Bug太多怎么办?怎样稳步提高团队的代码质量
最近负责的Android APP项目,由于团队成员变动.界面改版导致代码大幅修改等原因,产品发布后屡屡出现BUG导致的程序崩溃. 经过对异常统计和代码走读,BUG主要集中在空指针引起的NullPoin ...
- SpringMVC学习 -- REST
REST:表现层状态转化. REST 是目前最流行的一种互联网软件架构.他结构清晰.符合标准.易于理解.扩展方便 , 所以正得到越来越多网站的采用. 状态转化:浏览器 form 表单只支持 GET 和 ...
- codechef T1 What's int the name
NITIKA: 姓名本无意题目描述 Nitika 读了一本历史书,想要理清其中的人物关系.因此她要她的哥哥把书中出现的历史人 物全部列出来.哥哥把列好的人名给了 Nitika,但 Nitika 非常不 ...
- Mysql TEXT类型长度
BLOBTEXT一个BLOB或TEXT列,最大长度为65535(2^16-1)个字符. MEDIUMBLOBMEDIUMTEXT一个BLOB或TEXT列,最大长度为16777215(2^24-1)个字 ...
- jetty bleed漏洞利用工具
两个exp: https://github.com/AppSecConsulting/Pentest-Tools/blob/master/jetty-bleed.py https://github.c ...
- 表单文件上传,ajax文件上传
原创链接:http://www.cnblogs.com/yanqin/p/5345562.html html代码 index.jsp(表单文件上传) <form action="sh ...
- js面向对象编程(三)非构造函数的继承(转载)
Javascript面向对象编程(三):非构造函数的继承 今天是最后一个部分,介绍不使用构造函数实现"继承". 一.什么是"非构造函数"的继承? 比如,现在有一 ...
- Spring --- 异常处理机制
1.定义全局异常处理器,为全局的异常,如出现将调用 error.JSP <!-- 定义异常处理器 --> <bean class="org.springframework. ...
- vim操作大全
# 转自 https://blog.csdn.net/weixin_37657720/article/details/80645991 曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim ...
- hdu 5747(数学,贪心)
Aaronson Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...