很早很早的控件了,不过好像一直都没用过,今天看as控件发现还有这么一个东东,所以百度了一番,觉得很简单,就记录一下吧,老规矩,我很懒,记录代码以后复制粘贴用。
先上布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ToggleButton
android:id="@+id/tglSound"
android:layout_width="70dp"
android:layout_height="44dp"
android:background="@drawable/selector_butn_toggle"
android:checked="false"
android:text=""
android:textOff=""
android:textOn=""
/>
</RelativeLayout>
使用更简单:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((ToggleButton)findViewById(R.id.tglSound)).setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.e("TAG", "onCheckedChanged: 111111111111" );
}else{
Log.e("TAG", "onCheckedChanged: 2222222222222");
}
}
觉得不足的地方就是还是需要切图。。。。