选择改变事件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也比较常用,那比如在搜索框中,没输入一个字,下面 ...
随机推荐
- cglib
参考:http://blog.csdn.net/zhoudaxia/article/details/30591941 <!-- https://mvnrepository.com/artifac ...
- Flink源码阅读(一)--Checkpoint触发机制
Checkpoint触发机制 Flink的checkpoint是通过定时器周期性触发的.checkpoint触发最关键的类是CheckpointCoordinator,称它为检查点协调器. org.a ...
- Linux 远程登录——(九)
Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable ...
- ubuntu 安装wxpython以及boa-constructor
直接参考 官方的安装文档. 学习python 的时候就 用 wxPython . 那个时候用的是windows 的版本. 现在 用 ubuntu 下开发了.没有搭建好环境. 其实就一句话: sudo ...
- jquery——通过name属性查找元素
Js代码 : $("div[id]") 选择所有含有id属性的div元素 $("input[name='newsletter']") 选择所有的name属性 ...
- Linux内核设计与实现读书笔记(8)-内核同步方法【转】
转自:http://blog.chinaunix.net/uid-10469829-id-2953001.html 1.原子操作可以保证指令以原子的方式执行——执行过程不被打断.内核提供了两组原子操作 ...
- js监听不到组合键
我在js文件中写代码,监听 ctrl + enter 组合键,但是一直监听不到.只能监听到单个键. 后来我将监听的代码放到html页面中去,就能监听到了. 这个问题困扰我很久,记录下!
- webpack-dev-server坑
转载人家滴 https://segmentfault.com/q/1010000007561947/a-1020000007596130 需要webpack开发服务(webpack-dev-serve ...
- 计算器(丑陋版 and 加法专用版)
from tkinter import * win = Tk() win.geometry('500x300+400+300') win['bg'] = '#0099ff' win.title('魔方 ...
- 在表达式和脚本中将bean实例暴露出来
默认情况下,在activiti.cfg.xml中的所有bean和Spring配置文件中的所有bean都可以用于表达式和脚本.如果要限制配置文件中的bean的可见性,可以在流程引擎配置文件中配置一个名为 ...