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. Python代码规范

    一:背景 用于规范化ocp python开发,对于使用python开发的程序使用统一的风格,便于代码的维护 二:python风格规范 分号:不要在行尾加分号,也不要用分号将两条命令放在同一行 括号:宁 ...

  2. 常见通用的 JOIN 查询

    SQL执行循序: 手写: SELECT DISTINCT <query_list> FROM <left_table> <join type> JOIN <r ...

  3. javascript实现瀑布流效果(固定宽度)

    HTML代码: <div id="content"> <div class="box"> <div class="img ...

  4. Hibernate 懒加载 错误----no session

    错误: unable to evaluate the expression Method threw 'org.hibernate.LazyInitializa org.hibernate.LazyI ...

  5. tomcat:tomcat的OutOfMemoryError解决

    最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植到oracle,首先把jdbc的连接指向 mysql,打包放到tomcat里面,可以跑起来,没有问题,可是当把jdbc连接指向oracle ...

  6. NOIP2005过河(青蛙过河)

    题目传送门 这道题主要是因为L长度最大可以为1e9 而石子却最多只有100个 这样就浪费了很多时间空间 所以我们压缩一波路径就可以了 剩余的就是枚举每个点以及i-y到i-x的dp了 这里要说一句为什么 ...

  7. [转载]超赞!32款扁平化Photoshop PSD UI工具包(下)

    32款扁平化风格的UI工具包第二弹!上篇为大家分享了16款风格各异的UI Kits,下篇继续为大家呈上16款精美的UI工具包,全部都有Photoshop PSD文件可以下载哦,喜欢就赶紧收藏吧! 17 ...

  8. linux平台从源码安装git【转】

    转自:http://blog.csdn.net/lianshaohua/article/details/50571560 版权声明:本文为博主原创文章,未经博主允许不得转载. 如果是ubuntu等能自 ...

  9. go语言的定时器

    package main import ( "fmt" "time" ) func main(){ //创建一个定时器,时间为2s,2s过后会自动往通道里面写入 ...

  10. 在另一个文本框显示input file选择的文件名字

    javascript 获取文件域 (type=file) 的完整路径一直是很麻烦的问题,问题主要出在一些浏览器基于安全性考虑而不能正常获取到文件域中选中图片的决对路径,尤其一些基于webkit的浏览器 ...