• ToggleButton


     

     

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:paddingBottom="@dimen/activity_vertical_margin"

        android:paddingLeft="@dimen/activity_horizontal_margin"

        android:paddingRight="@dimen/activity_horizontal_margin"

        android:paddingTop="@dimen/activity_vertical_margin"

        tools:context="com.example.togglebuttondemo.MainActivity"

        android:orientation="vertical" >

     

        <ToggleButton

            android:id="@+id/toggleButton1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textOn="开"

            android:textOff="关"

            android:checked="false" />

        <ImageView 

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:id="@+id/imageView1"

            android:background="@drawable/dark" />

     

    </LinearLayout>

     

    MainActivity.java

     

    package com.example.togglebuttondemo;

     

    import android.support.v7.app.ActionBarActivity;

    import android.os.Bundle;

    import android.view.Menu;

    import android.view.MenuItem;

    import android.widget.CompoundButton;

    import android.widget.CompoundButton.OnCheckedChangeListener;

    import android.widget.ImageView;

    import android.widget.ToggleButton;

     

    public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener {

    ToggleButton tb;

    ImageView iv;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    tb=(ToggleButton) findViewById(R.id.toggleButton1);

    iv=(ImageView) findViewById(R.id.imageView1);

    tb.setOnCheckedChangeListener(this);

    }

    @Override

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    //buttonView代表哪个控件被点击了,isChecked代表被点击控件的状态

    iv.setBackgroundResource(isChecked?R.drawable.light:R.drawable.dark);

    }

     

     

    }

     

     

  • 相关阅读:
    PLC衔接新方式UcAsp.Opc
    dev barmanager 中的 add按钮出不来,无法创建菜单的问题解决
    一个或多个页边距被设置到也可打印的页面范围之外,处理方式
    bar设置全背景色
    PHP计算两个字符的相似程度similar_text
    PHP中双冒号::的用法
    百度站内搜索应该注意哪些方面?
    快给你的网站添加微信公众号吧!
    实例讲解网站前台界面开发流程
    百度富文本编辑器UEditor安装配置全过程
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/7928820.html
Copyright © 2020-2023  润新知