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);
        }
      });
    }

    }

    效果:

      

  • 相关阅读:
    Perl的运算符号字符
    windows xp 使用远程桌面时的关机/重新启动方法
    抵御TCP的洪水
    远程桌面连接中的常见问题 连接上就断开
    批量kill mysql进程
    Linux如何查看硬盘型号和缓存
    Apache Rewrite 规则详解
    nginx 内置变量大全
    大数据量分页存储过程效率测试附代码
    ASP.Net 更新页面输出缓存的几种方法(包括用户控件,iframe,页面缓存等)
  • 原文地址:https://www.cnblogs.com/czhyuwj/p/4752218.html
Copyright © 2011-2022 走看看