android_基础_switch

121 篇文章 1 订阅

目录:

1.应用场景与概述  
2.常用属性  
3.简单使用  
4.更改默认Switch的样式  
5.自定义Switch  

1.应用场景与概述
Switch是在4.0以后推出的,所以要注意开发时的minsdk设置,google在API 21后也推出support v7 包下的SwitchCompa的Material Design
开关控件,对低版本的有了更好的的支持。其实switch的应用场景和ToggleButton类似,多应用于两种状态的切换。

2.常用属性

android:typeface="normal" 		设置字体类型 
android:track=""				设置开关的轨迹,置开关的背景图片,类似于button的background。  	
android:textOff="开"			设置开关checked的文字  
android:textOn="关"				设置开关关闭时的文字 
android:thumb=""				设置开关的图片,文字所携带的背景,设置为背景色进行隐藏。不设置会出现一个背景框。
android:switchMinWidth=""		开关最小宽度
android:switchPadding=""		设置开关 与文字的空白距离   
android:switchTextAppearance=""	设置文本的风格 
android:checked=""				设置初始选中状态 
android:splitTrack="true"		是否设置一个间隙,让滑块与底部图片分隔(API 21及以上)
android:showText="true"			设置是否显示开关上的文字(API 21及以上)

简单设置:

<pre name="code" class="html">   
        <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff=""
        android:textOn=""
        android:switchMinWidth="120dp"
        android:thumb="@android:color/transparent"
        android:track="@drawable/switch_track"
        />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_close" android:state_checked="false" />
    <item android:drawable="@drawable/switch_open" android:state_checked="true" />
</selector>

效果展示:

在这里插入图片描述

这里layout_width:这能设置整个布局的宽度,不能设置具体的Switch的大小,需要使用switchMinWidth属性来设置。

thumb:文字所携带的背景,设置为背景色进行隐藏。不设置会出现一个背景框。

track:设置开关的背景图片,类似于button的background。

textoff、texton:设置开关时的文字显示。  

Switch的点击事件:

    private Switch mSwitch;
    private TextView mText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mSwitch = (Switch) findViewById(R.id.switch_);
        mText = (TextView) findViewById(R.id.text_);
        // 添加监听
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    mText.setText("开启");
                }else {
                    mText.setText("关闭");
                }
            }
        });
    }

3.简单使用
3.1)主布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aswitch.MainActivity">
    <!--
    android:typeface="normal":设置字体类型
    android:track="":设置开关的轨迹
    android:textOff="开":设置开关checked的文字
    android:textOn="关":设置开关关闭时的文字
    android:thumb="":设置开关的图片
    android:switchMinWidth="":开关最小宽度
    android:switchPadding="":设置开关 与文字的空白距离
    android:switchTextAppearance="":设置文本的风格
    android:checked="":设置初始选中状态
    android:splitTrack="true":是否设置一个间隙,让滑块与底部图片分隔
    -->
    <TextView
        android:id="@+id/switch_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="switch:" />
    <Switch
        android:layout_marginTop="10dp"
        android:layout_below="@+id/switch_tv"
        android:id="@+id/switch1"
        android:typeface="normal"
        android:textOff=""
        android:textOn=""
        android:switchMinWidth="40dp"
        android:switchPadding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <TextView
        android:layout_below="@+id/text"
        android:id="@+id/switch_compat_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="switchCompat:" />
    <android.support.v7.widget.SwitchCompat
        android:layout_marginTop="10dp"
        android:layout_below="@+id/switch_compat_tv"
        android:id="@+id/switch_compat"
        android:typeface="normal"
        android:switchMinWidth="40dp"
        android:switchPadding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text1"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/switch_compat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

3.2)主布局java类

package com.example.aswitch;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
 
    private Switch aSwitch;
    private SwitchCompat aSwitchCompat;
    private TextView text1,text2,switchText,switchCompatText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化
        aSwitch = (Switch) findViewById(R.id.switch1);
        aSwitchCompat = (SwitchCompat) findViewById(R.id.switch_compat);
        text1 = (TextView) findViewById(R.id.text);
        text2 = (TextView) findViewById(R.id.text1);
        //设置Switch事件监听
        aSwitch.setOnCheckedChangeListener(this);
        aSwitchCompat.setOnCheckedChangeListener(this);
    }
 
    /*
    继承监听器的接口并实现onCheckedChanged方法
    * */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switch1:
                if(isChecked){
                    text1.setText("开");
                }else {
                    text1.setText("关");
                }
                break;
            case R.id.switch_compat:
                if(isChecked){
                    text2.setText("开");
                }else {
                    text2.setText("关");
                }
                break;
            default:
                break;
        }
    }
}

3.3)截图效果

在这里插入图片描述

4.更改默认Switch的样式
4.1)在styles.xml中自定义style

    <!--自定义switch的按钮和轨迹颜色theme-->  
    <style name="mySwitch" parent="Theme.AppCompat.Light">
        <!-- switch 打开时的按钮的颜色 轨迹颜色默认为30%(看效果就明白30%是怎么回事了)这个颜色 -->
        <item name="colorControlActivated">@android:color/holo_green_dark</item>
        <!--  switch关闭时的按钮的颜色 -->
        <item name="colorSwitchThumbNormal">@color/colorAccent</item>
        <!-- switch关闭时的轨迹的颜色 30%这个颜色 -->
        <item name="android:colorForeground">@color/colorPrimaryDark</item>
 
    </style>    
4.1)在布局文件中通过android:theme="@style/mySwitch"设置
   <android.support.v7.widget.SwitchCompat
        android:layout_marginTop="10dp"
        android:layout_below="@+id/switch_compat_tv"
        android:id="@+id/switch_compat"
        android:typeface="normal"
        android:theme="@style/mySwitch"
        android:switchMinWidth="40dp"
        android:switchPadding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />    

5.自定义Switch

5.1)导入资源图片thumb.png ,thumb\_on.png ,track\_nomal.png ,track\_on.png ,track\_press.png  
  
5.2)实现thumb_selector.xml   
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--选中时的滑块图片-->
<item android:drawable="@drawable/thumb_on" android:state_checked="true"/>
    <!--正常情况滑块图片-->
<item android:drawable="@drawable/thumb"/>
</selector>

5.3)实现track_selector.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--打开时switch轨迹图片-->
    <item android:state_pressed="true"  android:drawable="@drawable/track_on" />
    <!--按压时switch轨迹图片-->
    <item android:state_checked="true"  android:drawable="@drawable/track_press" />
    <!--正常状态switch轨迹图片-->
    <item                               android:drawable="@drawable/track_nomal" />
 
    </selector>

5.4)主布局actiity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aswitch.SecondActivity">
 
    <TextView
        android:id="@+id/CustomSwitchCompat_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CustomSwitchCompat:" />
    <android.support.v7.widget.SwitchCompat
        android:layout_marginTop="10dp"
        android:layout_below="@+id/CustomSwitchCompat_tv"
        android:id="@+id/CustomSwitchCompat"
        android:layout_width="wrap_content"
        android:minWidth="40dp"
        android:minHeight="20dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/custom_result"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/CustomSwitchCompat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

5.5)主布局java类SecondActivity.java

package com.example.aswitch;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
 
public class SecondActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
 
    private SwitchCompat customSwitchCompat;
    private TextView custom_result,CustomSwitchCompat_tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //实例化
        customSwitchCompat = (SwitchCompat) findViewById(R.id.CustomSwitchCompat);
        custom_result = (TextView) findViewById(R.id.custom_result);
        //设置自定义的thumb和track
       customSwitchCompat.setThumbResource(R.drawable.thumb_selector);
        customSwitchCompat.setTrackResource(R.drawable.track_selector);
        //设置Switch事件监听
        customSwitchCompat.setOnCheckedChangeListener(this);
    }
 
    /*
    继承监听器的接口并实现onCheckedChanged方法
    * */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    custom_result.setText("开");
                }else {
                    custom_result.setText("关");
                }
    }
}

ps:其实自定义的途径还可以通过shape的绘制和java代码绘制,在这里就不详细说了

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要学习Android Studio的基础知识,可以参考以下几点: 1. 安装Android Studio:在运行Android Studio之前,需要先在计算机上安装Android Studio。确保您已经下载并安装了最新版本的Android Studio。 2. 创建和运行项目:使用Android Studio可以创建新的Android项目。您可以选择使用现有模板或创建自定义的应用程序。 3. 布局设计:在Android Studio的布局编辑器中,您可以使用拖放功能来创建应用程序的用户界面。可以通过添加各种视图和布局来设计应用程序的UI。 4. XML布局:使用XML布局文件可以更精确地定义应用程序的UI。可以使用不同的布局容器和视图来组织和排列UI元素。 5. 按钮点击事件:在Android Studio中,可以通过实现OnClickListener接口并重写onClick方法来处理按钮的点击事件。可以在onClick方法中使用switch语句来处理不同按钮的点击事件[2]。 6. 监听事件:使用监听事件可以对用户的操作做出应。可以使用setOnCheckedChangeListener方法来监听RadioGroup中RadioButton的选择变化,并在onCheckedChanged方法中处理相应的逻辑。 7. 图片显示:可以使用ImageView来显示图片。可以通过设置background属性来设置背景图片,通过设置src属性来设置前景图片。 8. 进度条:可以使用ProgressBar来显示进度。可以根据需要设置进度条的样式和颜色。 总之,Android Studio是一个强大的开发工具,可以用于创建Android应用程序。通过学习上述基础知识,您可以开始使用Android Studio来开发自己的Android应用程序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android java专业音乐播放器应用程序及其源代码](https://download.csdn.net/download/qq_37270421/88273960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android Studio开发基础知识(持续更新中~)](https://blog.csdn.net/Monster_CYC/article/details/127018047)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值