切换按钮-禁用滑动功能 [英] Switch button - disable swipe function

查看:124
本文介绍了切换按钮-禁用滑动功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个switch按钮(实际上是一个自定义按钮),出于某种原因,我想禁用滑动功能;我希望用户只能单击它.有没有办法做到这一点? 谢谢.

I have a switch button (actually is a custom one) and I want to disable the swipe functionality for some reason; I want the user to be able to click it only. Is there a way to achieve this? Thanks.

推荐答案

您可以将Switch设置为clickClick(false),然后在Switch的父级中侦听onClick()事件并以编程方式对其进行切换.开关似乎仍处于启用状态,但不会发生滑动动画.

You can setClickable(false) to your Switch, then listen for the onClick() event within the Switch's parent and toggle it programmatically. The switch will still appear to be enabled but the swipe animation won't happen.

...

[在onCreate()中]

[In onCreate()]

Switch switchInternet = (Switch) findViewById(R.id.switch_internet);
switchInternet.setClickable(false);

...

[点击监听器]

public void ParentLayoutClicked(View v){
    Switch switchInternet = (Switch) findViewById(R.id.switch_internet);

    if (switchInternet.isChecked()) {
        switchInternet.setChecked(false);           
    } else {
        switchInternet.setChecked(true);    
    }       
}

...

[layout.xml]

[layout.xml]

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:onClick="ParentLayoutClicked"
        android:focusable="false"
        android:orientation="horizontal" >

        <Switch
            android:layout_marginTop="5dip"
            android:layout_marginBottom="5dip"
            android:id="@+id/switch_internet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="NOT CONNECTED"
            android:textOn="CONNECTED"
            android:focusable="false"
            android:layout_alignParentRight="true"                                                
            android:text="@string/s_internet_status" />

    </RelativeLayout>

这篇关于切换按钮-禁用滑动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆