zoukankan      html  css  js  c++  java
  • ToggleButton --------- 按钮实现开关效果

       ToggleButton(开关按钮)是Android系统中比较简单的一个组件,是一个具有选中和未选择状态双状态的按钮,并且需要为不同的状态设置不同的显示文本 ,默认状态下 关。

        ToggleButton常用的XML属性

    属性名称

    描述

    android:disabledAlpha

    设置按钮在禁用时透明度。

    android:textOff

    未选中时按钮的文本

    android:textOn

    选中时按钮的文本

    下面是具体的例子: 开关状态 图片切换

    package com.example.togglebuttondemo;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.*;

    public class MainActivity extends ActionBarActivity {

      private ToggleButton toggle = null;
      private ImageView image = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      image = (ImageView)findViewById(R.id.imageView1);
      toggle = (ToggleButton) findViewById(R.id.toggleButton1);

      //设置监听事件   toggleButton 是通过 checked 属性来控制开关 
      toggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){

      @Override

      public void onCheckedChanged(CompoundButton buttonView,

        boolean isChecked) {

          /**

            params 参数列表

            buttonView 代表当前点击的对象

            isChecked 代表当前点击对象的checked对象

          **/
          image.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
        }
      });
    }

    }

    效果:

      

  • 相关阅读:
    线程池参数详解
    线程池各个参数详解以及如何自定义线程池
    fastdfs 安装
    SQL 执行顺序
    《SQL 进阶教程》 查找局部不一致的数据
    redis 高性能的原因
    一致性hash
    环境部署数据库报错
    redis 常用命令
    redis 高级学习和应用场景
  • 原文地址:https://www.cnblogs.com/czhyuwj/p/4752218.html
Copyright © 2011-2022 走看看