- 列表内容
ToggleButton,状态按钮控件。该控件扩展自CompoundButton类,具有选中和非选中两种状态。可以通过单击在两种状态间切换。主要的xml属性包括:android:textOn和android:textOff分别用于设置当按钮处于选中和非选中状态时显示的文本内容。可以通过isChecked()方法获取状态按钮的当前状态。
·状态按钮
- 继承自CompoundButton
·主要属性 - Android:textOn
- Android:textOf
checked:默认是否选中
·主要方法 - isChecked();
·主要事件 - setOnClickListener(onClickListener l)
实例:
效果图:
- 继承自CompoundButton
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:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textSize="20sp"
android:text="状态按钮被打开"
android:textColor="#fff000aa" />
<ToggleButton
android:id="@+id/tbText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textOff="关闭"
android:textOn="打开"
android:checked="true"
android:onClick="doClick"/>
</LinearLayout>
activity:
package com.ecainiao.togglebutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
private TextView tvInfo;
private ToggleButton tbText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.tvInfo);
tbText = (ToggleButton) findViewById(R.id.tbText);
}
public void doClick(View v){
if(tbText.isChecked()){
tvInfo.setText("状态按钮打开");
}else{
tvInfo.setText("状态按钮关闭");
}
}
}