2012-12-10 58 views
9

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

+0

可以帮这样的:-http://计算器。com/questions/11753355 /在doubletap和swipe之间切换 –

+0

swipes(他们被称为FLING EVENTS) - 我不知道...我会在一分钟内尝试这个 – Paul

回答

23

您可以将setClickClick(false)设置为Switch,然后侦听Switch的父节点中的onClick()事件并以编程方式切换它。开关仍然显示为启用,但滑动动画不会发生。

...

[中的onCreate()]

Switch _Switch_Internet = (Switch) findViewById(R.id.m_switch_internet); 
_Switch_Internet.setClickable(false); 

...

[点击收听]

public void ParentLayoutClicked(View _v){ 

    Switch _Switch_Internet = (Switch) findViewById(R.id.m_switch_internet); 

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

...

[ 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/m_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> 
7

要禁用交换机使用以下方法

switchBtn.setEnabled(false); 

为了使开关不能点击使用

switchBtn.setClickable(false); 

其中switchBtn是开关对象

+0

它不起作用。首先因为点击事件后我们switchBtn.setEnabled(false)不会出现。其次,对于残疾人观点,我们有不同的资源。 – Sinigami

14

一种更好的方式是,以防止从接收MotionEvent.ACTION_MOVE事件切换课程。

switchButton.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
    return event.getActionMasked() == MotionEvent.ACTION_MOVE; 
    } 
}); 

然后你可以自由设定一个点击监听交换机作为适当的:

这是可以做到的。

查看implementation of Switch了解拖动的工作原理。它太酷了!

+0

它不起作用。点击事件不会出现。开关仍然在滑动。 – Sinigami

+0

另外尝试'setClickable(false)'并实现'setOnTouchListener' –

1

我的解决办法是:

switchView.setOnTouchListener(new View.OnTouchListener() { 
@Override   
public boolean onTouch(View view, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_UP) { 
          //Your code 
       } 
       return true;    
    }  
}); 

或者ButterKnife:

@OnTouch(R.id.switchView) 
public boolean switchStatus(Switch switchView, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_UP) { 
     // your code 
    } 
    return true; 
} 
2

您可以自定义扩展交换机,然后覆盖其的onTouchEvent()视图

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    boolean handle = ev.getActionMasked() == MotionEvent.ACTION_MOVE; 
    return handle ? handle : super.onTouchEvent(ev); 
} 

它应该工作。

0

//此代码工作正常

公共类MainActivity扩展AppCompatActivity实现CompoundButton.OnCheckedChangeListener { 开关my_switch;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    my_switch = (Switch) findViewById(R.id.my_switch); 
    my_switch.setOnCheckedChangeListener(this); 
} 
@Override 
public void onClick(View v) { 
    switch (v.getId()){ 
     case R.id.my_switch: 
      if(my_switch.isChecked()) 
       my_switch.setChecked(true);     
      else   
       my_switch.setChecked(true); 
      break; 
    } 
} 

}