Android控件之ToggleButton的使用方法
togglebutton的状态只能是选中和未选中,并且需要为不同的状态设置不同的显示文本。
以下案例为togglebutton的用法
目录结构
main.xml布局文件
import android.app.activity; public class togglebuttonactivity extends activity { public void oncheckedchanged(compoundbutton buttonview,
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<imageview android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bulb_off"
android:layout_gravity="center_horizontal" />
<togglebutton android:id="@+id/togglebutton"
android:layout_width="140dip"
android:layout_height="wrap_content"
android:texton="开灯"
android:textoff="关灯"
android:layout_gravity="center_horizontal" />
</linearlayout>
togglebuttonactivity类
package com.ljq.tb;
import android.os.bundle;
import android.widget.compoundbutton;
import android.widget.imageview;
import android.widget.togglebutton;
import android.widget.compoundbutton.oncheckedchangelistener;
private imageview imageview=null;
private togglebutton togglebutton=null;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
imageview=(imageview) findviewbyid(r.id.imageview);
togglebutton=(togglebutton)findviewbyid(r.id.togglebutton);
togglebutton.setoncheckedchangelistener(new oncheckedchangelistener(){
boolean ischecked) {
togglebutton.setchecked(ischecked);
imageview.setimageresource(ischecked?r.drawable.bulb_on:r.drawable.bulb_off);
}
});
}
}
上一篇: iMindMap思维导图怎么绘制流程图?