zoukankan      html  css  js  c++  java
  • android控件 ToggleButton的应用

    ToggleButton是android给我们提供的开关按钮,
    有两种状态:选中和未选择状态。


    以下是代码实例: main.xml

    [html] view plain copy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.         android:orientation="vertical" android:layout_width="fill_parent" android:background="#FFF5F5F5"  
    4.         android:layout_height="fill_parent">  
    5.         <LinearLayout android:layout_width="fill_parent"  
    6.                 android:layout_height="wrap_content" android:orientation="horizontal">  
    7.                 <TextView android:textSize="14.0sp" android:id="@+id/tvSound" android:textColor="@android:color/black"  
    8.                         android:layout_width="wrap_content" android:layout_height="wrap_content"  
    9.                         android:text="已开启" />  
    10.                 <ToggleButton   
    11.                         android:id="@+id/tglSound" android:background="@drawable/selector_butn_toggle"  
    12.                         android:layout_width="wrap_content" android:layout_height="wrap_content"  
    13.                         android:checked="true" android:textOn="" android:textOff=""   
    14.                         android:text="" />  
    15.         </LinearLayout>  
    16. </LinearLayout>  
    [java] view plain copy
    1. 这是主MainActivity  
    2.   
    3. package com.apkbus.toggle;  
    4.   
    5. import android.app.Activity;  
    6. import android.os.Bundle;  
    7. import android.view.Window;  
    8. import android.widget.CompoundButton;  
    9. import android.widget.CompoundButton.OnCheckedChangeListener;  
    10. import android.widget.TextView;  
    11. import android.widget.ToggleButton;  
    12.   
    13. public class MainActivity extends Activity implements OnCheckedChangeListener{  
    14.         private ToggleButton mToggleButton;  
    15.         private TextView tvSound;  
    16.         @Override  
    17.     public void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏  
    20.         setContentView(R.layout.main);  
    21.         initView();//初始化控件方法  
    22.     }  
    23.   
    24.         private void initView() {  
    25.                 mToggleButton = (ToggleButton) findViewById(R.id.tglSound); //获取到控件  
    26.                 mToggleButton.setOnCheckedChangeListener(this);//添加监听事件  
    27.                 tvSound = (TextView) findViewById(R.id.tvSound);  
    28.         }  
    29.   
    30.         @Override  
    31.         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
    32.                 if(isChecked){  
    33.                         tvSound.setText("已开启");  
    34.                 }else{  
    35.                         tvSound.setText("已关闭");  
    36.                 }  
    37.         }  
    38. }  


     这是效果图


    实现起来相当简单, 供新手们参考。
    源码下载:http://pan.baidu.com/share/link?shareid=413709&uk=1796216265

    或者这个:https://github.com/zcweng/ToggleButton

  • 相关阅读:
    idea maven install java: 程序包不存在
    Window10取消文件默认打开方式
    @ModelAttribute与@RequestBody的区别
    python小知识
    CentOS下yum方式安装FFmpeg
    推荐一款可以直接下载浏览器sources资源的Chrome插件
    如何在python中使用Elasticsearch
    python logging模块“另一个程序正在使用此文件,进程无法访问。”问题解决办法
    Python的伪造数据生成器:Faker
    docker修改系统时间总结
  • 原文地址:https://www.cnblogs.com/huangjialin/p/5997375.html
Copyright © 2011-2022 走看看