2012-12-10 56 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; 
    } 
} 

}