当前位置: 首页 > news >正文

togglebutton用法 java_Android控件ToggleButton的使用方法

ToggleButton(开关button)是Android系统中比較简单的一个组件,是一个具有选中和未选择状态双状态的button。而且须要为不同的状态设置不同的显示文本。

ToggleButton经常使用的XML属性

属性名称

描写叙述

android:disabledAlpha

设置button在禁用时透明度。

150215370.jpg

android:textOff

未选中时button的文本

android:textOn

选中时button的文本

main.xml:

xml version="1.0" encoding="utf-8"?>

android:orientation="vertical" android:layout_width="fill_parent" android:background="#FFF5F5F5"

android:layout_height="fill_parent">

android:layout_height="wrap_content" android:orientation="horizontal">

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="已开启" />

android:id="@+id/tglSound" android:background="@drawable/selector_butn_toggle"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:checked="true" android:textOn="" android:textOff=""

android:text="" />

MainActivity.java:

package com.apkbus.toggle;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.TextView;

import android.widget.ToggleButton;

public class MainActivity extends Activity implements OnCheckedChangeListener{

private ToggleButton mToggleButton;

private TextView tvSound;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏

setContentView(R.layout.main);

initView();//初始化控件方法

}

private void initView() {

mToggleButton = (ToggleButton) findViewById(R.id.tglSound); //获取到控件

mToggleButton.setOnCheckedChangeListener(this);//加入监听事件

tvSound = (TextView) findViewById(R.id.tvSound);

}

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if(isChecked){

tvSound.setText("已开启");

}else{

tvSound.setText("已关闭");

}

}

}

效果图:

101541k43o45m9su2rvyo3.jpg

101720ltjvjvvttz56jf4s.jpg

Demo下载地址:http://download.csdn.net/detail/u010963246/8941969

http://www.lryc.cn/news/567412.html

相关文章:

  • 公司各个部门所有英文缩写
  • 弈天棋缘-- 擂台规则
  • 五笔字根表识别码图_五笔输入法学习笔记
  • android drawline参数,Android绘图具体应用方式总结
  • 安装C++语言图形库graphics.h
  • d3.js介绍以及使用d3.js画一个圆圈和柱状图附带讲解d3.js的enter对象、update对象、exit对象
  • zencart经典模板lightinthebox安装教程及修改建议 (转)
  • JB的测试之旅-SEO了解下
  • 《BJBR虚拟仿真解决方案(描述精选)》(Yanlz+SteamVR+5G+AI+VR+AR+MR+HR+BR+CR+DR+ER+FR+GR+人工智能+人机交互+立钻哥哥+==)
  • G2O整理
  • KVM——1——KVM简介及基础安装配置
  • Android SE的多种集成形态:eSE、UICC、SD Card
  • java画环fillArc函数_使用Java的Graphics类进行绘图的方法详解
  • CoInitialize浅析一
  • Bibtex 参考文献样式
  • mysql排序order by asc | desc
  • 怎样把hlp转换成html,如何将HLP文件转换为CHM文件
  • 网卡bonding模式 - bond0、1、4配置
  • Python Web学习笔记,电影网站
  • 深入挖掘Windows脚本技术
  • 大数据流式计算
  • CocosCreator 源码cc.FadeOut详解
  • EMNLP 2022 | ELMER: 高效强大的非自回归预训练文本生成模型
  • Serverlet理解
  • CSS3属性border-radius参数详解
  • Idea Module not specified
  • Cable Modem工作原理
  • front-page.php模板,WordPress主题教程:WordPress的模板层次
  • VLAD原理简介
  • 【笔记】php-Cookie和Session