横向开关(switch)

横向开关(switch)

一:属性

1.Activity

//横向开关
public class SwitchActivity extends Activity {
    
    private Switch switchComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.switch_layout);
        
        switchComponent = (Switch)findViewById(R.id.switchId);
        
        switchComponent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(SwitchActivity.this, "无线网络打开了!", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(SwitchActivity.this, "无线网络关闭了!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

2.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 横向开关 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="无线网:"
        android:textSize="20sp" />

    <Switch
        android:id="@+id/switchId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textOn="打开"
        android:textOff="关闭"
        />

</LinearLayout>

3.效果图如下:

posted @ 2016-04-09 18:03  疯狂的兔子!  阅读(868)  评论(0编辑  收藏  举报