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>

    点击下载:源代码

    运行效果:如图

  • 相关阅读:
    Linkerd 2.10(Step by Step)—将 GitOps 与 Linkerd 和 Argo CD 结合使用
    Linkerd 2.10(Step by Step)—多集群通信
    Linkerd 2.10(Step by Step)—使用 Kustomize 自定义 Linkerd 的配置
    Linkerd 2.10(Step by Step)—控制平面调试端点
    Linkerd 2.10(Step by Step)—配置超时
    Linkerd 2.10(Step by Step)—配置重试
    Linkerd 2.10(Step by Step)—配置代理并发
    本地正常运行,线上环境诡异异常原因集合
    Need to invoke method 'xxx' declared on target class 'yyy', but not found in any interface(s) of the exposed proxy type
    alpine 安装常用命令
  • 原文地址:https://www.cnblogs.com/skywang12345/p/3123046.html
Copyright © 2011-2022 走看看