zoukankan      html  css  js  c++  java
  • 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);

    }

     

     

    }

     

     

  • 相关阅读:
    CF375D Tree and Queries
    进制转换
    贪心问题
    next_permutation函数
    C++ STL
    一些排序总结
    KMP算法
    围圈报数
    车辆调度—模拟栈的操作
    搜索题
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/7928820.html
Copyright © 2011-2022 走看看