skywang12345

导航

 

Android控件之Switch

1 Switch简介

Switch用于开关按钮。Switch和ToggleButton稍有区别:ToggleButton是按下弹起的开关,而Switch是左右滑动的开关。

 

2 Switch示例

创建一个activity,包含1个Switch。

应用层代码

package com.skywang.control;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;

public class MySwitch extends Activity {

    private Switch mSwitch;
    private TextView mViewShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_switch);
        
        mViewShow = (TextView)findViewById(R.id.tv_show);
        
        // 设置Switch开关
        mSwitch = (Switch)findViewById(R.id.switch_def);
        mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if (isChecked) {
                    // 开启switch,设置提示信息
                    mViewShow.setText(getString(R.string.text_on));
                } else {
                    // 关闭swtich,设置提示信息
                    mViewShow.setText(getString(R.string.text_off));
                }
            }
        });
    }

}

layout文件

<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="vertical"
    >
    
    <TextView 
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/text_def"
        />
    
    <Switch 
        android:id="@+id/switch_def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/text_on"
        android:textOff="@string/text_off"
        />

    
</LinearLayout>

注意:
若提示错误“View requires API level 14 (current min is 10): <Switch>”。
那是Switch的最小版本必须是API level14(即Android4.0版本),因为Switch是Android4.0支持的。
解决办法:
在manifest中修改最小的sdk版本为14(即Android4.0版本),添加如下内容即可
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />

manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.skywang.control"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.skywang.control.MySwitch"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

点击下载:源代码

运行效果:如图

 

posted on 2013-06-15 15:45  如果天空不死  阅读(16935)  评论(0编辑  收藏  举报