android中ToggleButton的使用

来源:互联网 发布:局域网电脑监控软件 编辑:程序博客网 时间:2024/06/05 06:37

java:

package EX05_06.txt;import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.LinearLayout;import android.widget.ToggleButton;public class EX05_06 extends Activity {private ToggleButton mButton;private LinearLayout mLayout;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                initApp();    }private void initApp() {// TODO Auto-generated method stubmButton = (ToggleButton)findViewById(R.id.ToggleButton01);mLayout = (LinearLayout)findViewById(R.id.LinearLayout01);mButton.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {mLayout.setOrientation(1);} else {mLayout.setOrientation(0);}}});}}


 

main.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"    ><ToggleButtonandroid:layout_marginLeft = "20dip" android:textOn = "竖直排列"android:textOff = "横向排列"android:id="@+id/ToggleButton01" android:layout_width="100dip" android:layout_height="wrap_content"></ToggleButton><LinearLayout android:id="@+id/LinearLayout01"android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"><Button android:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><Button android:text="Button03" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout></LinearLayout>


 

原创粉丝点击