Android控件之ToggleButton(多状态按钮)
一、概述
ToggleButton有两种状态:选中状态和没选中状态(类似一个开关),并且需要为不同的状态设置不同的显示文本
二、ToggleButton属性
android:checked = "true" ——按钮的状态(true为选中(textOn),false为没有选中(textOff))
android:textOff = "关"
android:textOn = "开"
三、代码演示
1、先将下面两个图片放入到资源文件中(分别命名为off和on)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textOn="开"
android:textOff="关" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/off" /> </LinearLayout>
package com.muke.textview_edittext; import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;
import android.app.Activity; public class MainActivity extends Activity implements OnCheckedChangeListener{
private ToggleButton tb;
private ImageView img; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //第一步:初始化控件(找到需要操作的控件)
tb = (ToggleButton) findViewById(R.id.toggleButton1);
img = (ImageView) findViewById(R.id.imageView1); //给当前的tb控件设置监听器
tb.setOnCheckedChangeListener(this); } @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//当tb被点击的时候,当前的方法会执行
//buttonView——代表被点击控件的本身
//isChecked——代表被点击的控件的状态
//当点击tb这个控件的时候更好img的src img.setImageResource(isChecked?R.drawable.on:R.drawable.off);//在java代码中设置ImageView控件的src路径(这里使用三元运算来设置图片的路径) }
}
Android控件之ToggleButton(多状态按钮)的更多相关文章
- ToggleButton开关状态按钮控件
ToggleButton开关状态按钮控件 一.简介 1. 2.ToggleButton类结构 父类是CompoundButton,引包的时候注意下 二.ToggleButton开关状态按钮控件使用方法 ...
- Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像
此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...
- Android控件介绍
1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...
- Android - 控件android:ems属性
Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...
- Android控件系列之CheckBox
学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...
- android控件的属性
android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...
- 一步一步学android控件(之十六)—— CheckBox
根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...
- Android控件TextView的实现原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...
- Qt控件精讲一:按钮
原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...
随机推荐
- JAVA fundamentals of exception handling mechanism
Agenda Three Categories Of Exceptions Exceptions Hierarchy try-catch-finally block The try-with-reso ...
- java提高篇---LinkedList
一.概述 LinkedList与ArrayList一样实现List接口,只是ArrayList是List接口的大小可变数组的实现,LinkedList是List接口链表的实现.基于链表实现的方式使得L ...
- Linux基本配置
Linux发行版:centos 6.5 配置yum源 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -P /etc/yum.repos ...
- Action访问Servlet API
访问Servlet API 1.通过ActionContent类访问Servlet API ActionContext中访问Servlet API的几个常用的方法: (1)Map getApplica ...
- 三、java中的流程控制
流程控制:1.分类:顺序结构.选择结构.循环结构.2.顺序结构:依次执行.3.选择结构:if.if...else.if...else if...else:三目运算符(表达式?为true的执行语句:为f ...
- 我的android学习经历29
四大组件之广播接收者BroadcastReceiver 新建广播接收器 需要新建一个类继承类BroadcastReceiver,并且重写其中的方法onReceive(),不要在这个方法中添加过多的逻辑 ...
- BZOJ 2666: [cqoi2012]组装
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2666 题意:n种零件,m个位置,每个位置有一种零件.求一个位置x,使得cost(1 ...
- BZOJ 2229 最小割
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2229 题意:给定一个带权无向图.若干询问,每个询问回答有多少点对(s,t)满足s和t的最 ...
- java传递和返回对象
java传递的只是一个引用,一定要注意准确认识在对象传递和赋值时所发生的一切. 事实上,java中的每个对象(除了基本数据类型以外)的标识符都属于指针的一种,但是其使用受到了严格的限制和防范,不仅在编 ...
- Ubuntu中文输入法的添加
做了一个英文环境的Ubuntu14.04LTS,为了写博客方便,添加了中文输入法,在网上搜寻了一堆方法,最后找到个靠谱的. 1 添加fcitx输入法框架.(在此框架下有各种输入法) sudo add- ...