ToggleButton的demo

.xml:

<?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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textView" /> <!-- ToggleButton - 双状态按钮控件 textOn - 当按钮状态为 true 时所显示的文本 textOff - 当按钮状态为 false 时所显示的文本 --> <ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="关闭" android:textOff="打开" /> </LinearLayout>

 

.java:

package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.ToggleButton; public class _ToggleButton extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.togglebutton); setTitle("ToggleButton"); final ToggleButton btn = (ToggleButton) this.findViewById(R.id.toggleButton); // setOnClickListener() - 响应按钮的鼠标单击事件 btn.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { TextView txt = (TextView) _ToggleButton.this.findViewById(R.id.textView); // ToggleButton.isChecked() - 双状态按钮的按钮状态 txt.setText("按钮状态:" + String.valueOf(btn.isChecked())); } }); } }

你可能感兴趣的:(android,xml,layout,Class,encoding)