脚本宝典收集整理的这篇文章主要介绍了如何利用ToggleButton切换图片,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
如何利用ToggleButton切换图片
java文件
package com.example.administrator.app;
import andROId.app.ActivITy;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class toggleBTn extends Activity implements CompoundButton.OnCheckedChangeListener {
PRivate ToggleButton tb;
private ImageView img;
protected void onCreate(Bundle saveinstancestate){
suPEr.onCreate(saveInstanceState);
setContentView(R.layout.togglebutton);
tb = (ToggleButton)findViewById(R.id.toggle_button);
img = (ImageView) findViewById(R.id.imageView);
/**
给当前的tb设置监听器
*/
tb.setOnCheckedChangeListener(this);
}
//Called when the checked state of a compound button has changed.
//@param buttonView The compound button view whose state has changed.
//@param isChecked The new checked state of buttonView.
-
void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@H_325_126@
//实现接口CompoundButton.OnCheckedChangeListener 来设置tb监听,当点击 按钮tb的时候开始执行
// onCheckedChanged方法
//buttonView 代表被点击的控件按钮
// isChecked 被点击控件的状态
//当点击tb开关的时候,更换img的背景
img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
}
}
布局文件
<?XMl version="1.0" encoding="utf-8"?>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical">
<!--textOn true
textOff false-->
<ToggleButton
android:checked="false"
android:textOff="关"
android:textOn="开"
android:id="@+id/toggle_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:background="@drawable/off"/>
</LinearLayout>
效果图:
以上是脚本宝典为你收集整理的如何利用ToggleButton切换图片全部内容,希望文章能够帮你解决如何利用ToggleButton切换图片所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。