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

    }

     

     

    }

     

     

  • 相关阅读:
    解决document.execCommand执行fontSize不能超过48px的问题
    JS实现值复制
    DGIS之遥感影像数据获取
    Chrome获取微信授权,调试公众号页面
    【Three.js】如何选中外部模型
    【Three.js】模型抗锯齿处理
    【Three.js】实现随心所欲的展示外部三维模型
    【GIS新探索】算法实现在不规则区域内均匀分布点
    WebGIS简单实现一个区域炫酷的3D立体地图效果
    【GIS新探索】GeoHash原理和编解码实现
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/7928820.html
Copyright © 2020-2023  润新知