android switch,看我的就行了,用原生的就够...几行代码搞定,效果很好

先上效果图:

  

三张图分别是未选中,拖动中,选中后的图...各位可以根据效果选择要不要继续往下看...

先声明,我这个直接用的原生swith,原生的会有一个bug,那就是拖动到中间可能会不动,针对这个问题,我自定义了一个switch..重写了部分内容,解决了这个问题...你们直接copy就好

package com;


import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Switch;

/**
 * switch 开关按钮
 */
public class Seniorswitch extends Switch {
    /**
     * 监听是否存在动作
     */
    private boolean IF_EVENT_MOVE = false;
    /**
     * 监听滑动时间
     */
    private long MOVE_TIME = -1;

    public Seniorswitch(Context context) {
        super(context);
    }

    public Seniorswitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Seniorswitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (IF_EVENT_MOVE && ev.getAction() == MotionEvent.ACTION_UP) {
            MOVE_TIME = -1;
            IF_EVENT_MOVE = false;
            if (ev.getX() <= getWidth() / 2) {
                setChecked(false);
            } else {
                setChecked(true);
            }
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            //松开
            IF_EVENT_MOVE = false;
            MOVE_TIME = -1;
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            //动作
            if (MOVE_TIME == -1) {
                MOVE_TIME = System.currentTimeMillis();
            } else {
                if (System.currentTimeMillis() - MOVE_TIME > 500) {
                    IF_EVENT_MOVE = true;
                }
            }
        }
        return super.onTouchEvent(ev);
    }
}



资源文件代码:

  <com.Seniorswitch
            android:id="@+id/switch_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="17dp"
            android:background="#00000000"
            android:checked="false"
            android:thumb="@drawable/switch_selector"
            android:track="@drawable/switch_track_selector" />

这时候你会发现:"@drawable/switch_selector"  和 android:track="@drawable/switch_track_selector 不存在,

在res下新建drawable文件夹:

在drawable新建文件switch_selector.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_on" android:state_checked="true" />
    <item android:drawable="@drawable/switch_off" android:state_checked="false" />
</selector>

这时候你又会发现:"@drawable/switch_on"  和 android:track="@drawable/switch_off 不存在,

在drawable新建文件switch_on.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <!--背景色-->
    <solid android:color="#ffffff" />
    <!--边框-->
    <stroke
        android:width="@dimen/dp_2"
        android:color="#00000000" />
    <!--大小-->
    <size
        android:width="@dimen/dp_17"
        android:height="@dimen/dp_17" />
</shape>

在drawable新建文件switch_off.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!--背景色-->
    <solid android:color="#ffffff" />
    <!--边框-->
    <stroke
        android:width="@dimen/dp_2"
        android:color="#00000000" />
    <!--大小-->
    <size
        android:width="@dimen/dp_17"
        android:height="@dimen/dp_17" />
</shape>

在drawable新建文件switch_track_selector.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_track_on" android:state_checked="true" />
    <item android:drawable="@drawable/switch_track_off" android:state_checked="false" />
</selector>

在drawable新建文件switch_track_on.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#1E7AFA" />
    <!--圆角-->
    <corners android:radius="@dimen/dp_9" />
    <!--大小-->
    <size
        android:width="@dimen/dp_19"
        android:height="@dimen/dp_19" />
</shape>

在drawable新建文件switch_track_off.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#7E8187" />
    <!--圆角-->
    <corners android:radius="@dimen/dp_9" />
    <!--大小-->
    <size
        android:width="@dimen/dp_19"
        android:height="@dimen/dp_19" />
</shape>

此外代码中,也说一下:

switch_bt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                        } else {
                        }
                    }
                });

可监听switch的开关操作..

结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值