首页 > 技术文章 > 开关按钮(ToggleButton&Switch)

gdpdroid 2014-03-29 16:33 原文

开关按钮,很实用的小东西。

下面上实例:

-------------------------------我是邪恶的分割线---------------------------------------

1.java代码

 1 package gdp.switchtestv1;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.widget.CompoundButton;
 7 import android.widget.CompoundButton.OnCheckedChangeListener;
 8 import android.widget.LinearLayout;
 9 import android.widget.Switch;
10 import android.widget.ToggleButton;
11 
12 public class MainActivity extends Activity {
13     private ToggleButton toggle;
14     private Switch switcher;
15     private LinearLayout test;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.main);
20         
21         //获取空间对象
22         toggle=(ToggleButton)findViewById(R.id.toggle);
23         switcher=(Switch)findViewById(R.id.switcher);
24         test=(LinearLayout)findViewById(R.id.test);
25         
26         //绑定监听器
27         toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
28             
29             @Override
30             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
31                 // TODO Auto-generated method stub
32                 if(isChecked){
33                     test.setOrientation(1);
34                 }else{
35                     test.setOrientation(0);
36                 }
37             }
38         });
39         
40         switcher.setOnCheckedChangeListener(new OnCheckedChangeListener() {
41             
42             @Override
43             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
44                 // TODO Auto-generated method stub
45                 if(isChecked){
46                     test.setOrientation(1);
47                 }else{
48                     test.setOrientation(0);
49                 }
50             }
51         });
52     }
53 
54     @Override
55     public boolean onCreateOptionsMenu(Menu menu) {
56         // Inflate the menu; this adds items to the action bar if it is present.
57         getMenuInflater().inflate(R.menu.main, menu);
58         return true;
59     }
60 
61 }

2.xml文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent"
 4     android:orientation="vertical" >
 5     <!-- texton表示开关打开状态显示的文本,textoff表示开关关闭状态显示的文本 -->
 6     <!-- checked属性为默认开关所处的状态 -->
 7     <ToggleButton
 8         android:id="@+id/toggle"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:textOn="on"
12         android:textOff="off"
13         android:checked="true"
14         />
15     <Switch
16         android:id="@+id/switcher"
17         android:layout_width="wrap_content"
18         android:layout_height="wrap_content"
19         android:textOn="on"
20         android:textOff="off"
21         android:checked="true"
22         />
23     <LinearLayout 
24         android:id="@+id/test"
25         android:layout_width="fill_parent"
26         android:layout_height="fill_parent"
27         android:orientation="vertical">
28         <Button
29             android:layout_width="80dp"
30             android:layout_height="wrap_content"
31             android:text="bn1"
32             />
33         <Button
34             android:layout_width="80dp"
35             android:layout_height="wrap_content"
36             android:text="bn2"
37             />
38         <Button
39             android:layout_width="80dp"
40             android:layout_height="wrap_content"
41             android:text="bn3"
42             />
43     </LinearLayout>
44 
45 </LinearLayout>

 

推荐阅读