zoukankan      html  css  js  c++  java
  • Android控件之Switch

    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>

    点击下载:源代码

    运行效果:如图

  • 相关阅读:
    python基础篇 08 文件操作
    python基础篇 07set集合 深浅拷贝
    python 基础篇 06 编码 以及小知识点补充
    python基础篇 05字典
    钉钉中设置代码提交提醒--Github机器人(转)
    Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)
    Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)
    为什么添加了@Aspect 还要加@Component(转)
    Servlet 服务器 HTTP 响应
    Servlet 客户端 HTTP 请求
  • 原文地址:https://www.cnblogs.com/skywang12345/p/3123046.html
Copyright © 2011-2022 走看看