ToggleButton控件

来源:互联网 发布:java的发展和前景如何 编辑:程序博客网 时间:2024/06/02 01:52
<RelativeLayout 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.togglebutton.MainActivity" >    <ToggleButton        android:checked="false"        android:textOn="开"        android:textOff="关"        android:id="@+id/toggleButton1"        android:layout_width="match_parent"        android:layout_height="wrap_content"         />    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/toggleButton1"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:layout_below="@+id/toggleButton1"        android:background="@drawable/xiaochuang" />  </RelativeLayout>


package com.example.togglebutton;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View.OnClickListener;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.ImageView;import android.widget.ToggleButton;public class MainActivity extends Activity  implements OnCheckedChangeListener{private ToggleButton tb;private ImageView img;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tb=(ToggleButton) findViewById(R.id.toggleButton1);        img=(ImageView) findViewById(R.id.imageView1);        tb.setOnCheckedChangeListener(this);    }@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubimg.setBackgroundResource(isChecked?R.drawable.ic_launcher:R.drawable.xiaochuang);}}

0 0