RadioButton、CheckBox与checked事件
RadioButton是一个单选框控件
Checkbox是一个复选框控件
在JAVA代码中为它们添加监听器时,方法都叫做setOnCheckedChangeListener,因为它们只有选中(Checked)与否两种状态,而不是button空间的按压(Click)与否
MainActivity的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><Buttonandroid:id="@+id/RadioButton"android:layout_width="match_parent"android:layout_height="80dp"android:text="RadioButton"/><!--控件closed有两种方式,一种可以在内部再加控件--></LinearLayout>
再建立一个Activity文件,命名为Radioactivity,其XML文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".RadioActivity"android:orientation="vertical"><TextViewandroid:text="that man"android:textSize="25sp"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"/><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/radio1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="123"android:textSize="20sp"android:checked="true"/><!--checked表示默认选中--><RadioButtonandroid:id="@+id/radio2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="BlackGlasses"android:textSize="20sp"android:scrollbarSize="30sp"/><RadioButtonandroid:id="@+id/radio3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="葛底斯堡演说"android:textSize="20sp"android:scrollbarSize="30sp"/></RadioGroup><TextViewandroid:layout_marginTop="100dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="修改后"android:gravity="center"android:textSize="30dp"/><TextViewandroid:text="that man"android:textSize="25sp"android:layout_width="match_parent"andro手机上id:layout_height="wrap_content"android:gravity="center"/><RadioGroupandroid:id="@+id/group"android:layout_width="match_parent"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/radio4"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="123"android:gravity="center"andrnoid:background="@drawable/stroke"android:textSize="20sp"android:checked="true"android:button="@null"/><!--checked表示默认选中--><RadioButtonandroid:id="@+id/radio5"android:button="@null"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/stroke"android:text="BlackGlasses"a android:gravity="center"android:textSize="20sp"android:scrollbarSize="30sp"/><RadioButtonandroid:id="@+id/radio6"android:layout_width="match_parent"android:button="@null"android:layout_height="wrap_content"android:background="@drawable/stroke"android:text="葛底斯堡演说"android:gravity="center"android:textSize="20sp"h android:scrollbarSize="30sp"/></RadioGroup><CheckBoxandroid:layout_width="match_parent"android:layout_height="60dp"android:text="千反田"/><CheckBoxandroid:layout_width="match_parent"android:layout_height="60dp"android:text="小鸟游"/><CheckBoxandroid:layout_width="match_parent"android:layout_height="60dp"android:text="五更"/>
</LinearLayout>
MainActivity的JAVA代码,完成了Activity的跳转
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {private Button mBtnRadioButton; //在onCreate之前就声明了空的Button,在对Activity初始化后才绑定。也可以在onCreate中声明,但是不能加private@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstancieState);setContentView(R.layout.activity_main);mBtnRadioButton = findViewById(R.id.RadioButton);mBtnRadioButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, RadioActivity.class);startActivity(intent); //还有个startActivities()方法....半天没看出来错误}});}
RadioActivity的JAVA文件
package co'jm.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;public class RadioActivity extends AppCompatActivity {RadioGroup mBtnRadioButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio);mBtnRadioButton = findViewById(R.id.group);mBtnRadioButton.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { //区别于button的Clicked,它是Checked@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton radioButton = group.findViewById(checkedId); //Group是一个整体,要根据传入的checkedID找出到底是哪个RadioButton选中了Toast.makeText(RadioActivity.this, radioButton.getText(), Toast.LENGTH_LONG).show();}});}
}
在手机上运行,Radioactivity的效果如下,下方的黑框是一个Toast控件,可以在这个页面的JAVA代码中找到
RadioButton、CheckBox与checked事件相关推荐
- php 单选框选中事件,html中的checkbox和radio事件选择用法详解
radio注册了click事件以后,神奇的是用键盘上的上下左右选择时,居然会触发鼠标事件,滚轮也会触发,这种神奇的事情在mousedown下面是不会发生的.(webkit不能使用上下左右选择) che ...
- html单选框怎么提交数据库,HTML复选框和单选框 checkbox和radio事件介绍
checkbox 和 radio的事件选择一度让我很迷惑. 开始以我对js的理解,我觉得change事件应该是最合理的,可惜啊ie下change事件是在改变后焦点离开时才触发. 后来就用click m ...
- windows mobile C# net3.5 如何捕捉Arrow的CheckBox的KeyDown事件?
你需要override IsInputKey事件,可是这个时间却在.Net CF2中不支持.此路不通! 那么该如何做呢? 通过P/Invoke调用! 我是向来不喜欢C#的,搞来搞去,很多功能还是需要调 ...
- jq设置checked是否选中_javascript之jquery如何判断input type=checkbox中checked是否存在选中项呢?...
摘要: 下文讲述jquery技巧-获取checkbox对象是否选中的方法分享,如下所示: 实现思路: 方式1: jquery 1.6版本之前-我们检测checkbox是否被选中,可使用attr方法 $ ...
- TreeView的checked事件
今天下午花了两个小时写的,感觉很不错! /// <summary> /// TreeView checked事件 /// </summ ...
- jquery与checkbox的checked属性的问题
jquery与checkbox的checked属性的问题,讲的是控件<input type="checkbox"></input> 1.页面加载成功后,点击 ...
- checkbox中checked属性总结
一.checked属性定义和用法 1.checked属性是一个布尔属性 2.checked属性规定在页面加载时应该被预先选定的<input>元素 3.checked属性适用于<inp ...
- jquery设置checkbox为checked状态不改变的解决办法
使用jQuery设置checkbox为选择状态时,我们通常会使用 $(this).attr("checked", true); //或者 $(this).attr("ch ...
- c# Gridview 点击checkbox 触发的事件
今天介绍下gridview中点击checkbox触发的事件,包括全选. 声明下,gridview中的checkbox不是在gridview设计器里面添加的,而是在gridview的属性中设置出来的,具 ...
最新文章
- Go 学习笔记(28)— nil(nil 不能比较、不是关键字或保留字、nil 没有默认类型、不同类型的 nil 指针是一样的、不同类型的 nil 是不能比较的、相同类型的 nil 可能也无法比较)
- 寻找网页设计灵感的200佳网站推荐(系列二)
- 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a...
- python适合零基础学习吗-零基础能学好Python吗?哪些人更适合学习?
- 数据结构二之线段树Ⅱ——KiKi‘s K-Number,ball,The Child and Sequence,「雅礼集训 2017 Day1」市场,Atlantis
- 众咖齐聚!2018智能汽车跨界融合高峰论坛开幕在即
- QToolButton设置图片填充满_韩国高人气图片素材大合集!每张图,都是桌面壁纸,逼格满满...
- Delphi – 我的代码之简单五子棋
- [译] 正确实现 linkedPurchaseToken 以避免重复订阅
- flask使用sqlit3的两种方式
- Google 程序员消灭 Bug 的 5 大法宝!
- 【BUG解析】调用PagerAdapter.notifyDataSetChanged()不更新
- ZK 数据传输四:BackingBean获取spring层类再获取数据(jsp页面或zul页面)
- eplan p8详细安装步骤文库_eplan p8详细安装步骤
- d3.js 旋转图形_PROE、Creo绘制手轮图形案例,二维草图绘制练习项目
- Amaze UI后台管理模板 v2.7.2
- apache Ignite 安装和helloworld
- 剑指Offer_编程题(用两个栈实现队列/旋转数组的最小数字(O(n) + 二分O(lgn))/斐波那契数列/跳台阶)
- 一分钟教程:注册谷歌邮箱
- 测向交叉定位matlab,一种三维多站测向交叉定位算法