28

ボタンがありswitch(実際にはカスタム ボタンです)、何らかの理由でスワイプ機能を無効にしたいと考えています。ユーザーがクリックできるようにしたい。これを達成する方法はありますか?ありがとう。

4

7 に答える 7

54

Switch に Clickable(false) を設定し、Switch の親内で onClick() イベントをリッスンして、プログラムで切り替えることができます。スイッチは引き続き有効に見えますが、スワイプ アニメーションは発生しません。

...

[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);    
    }       
}

...

[レイアウト.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>
于 2013-06-19T19:04:26.427 に答える
23

より良い方法は、Switch クラスがMotionEvent.ACTION_MOVEイベントを受信しないようにすることです。

これは、次の方法で実行できます。

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

その後、必要に応じてスイッチにクリック リスナーを自由に設定できます。

Switch の実装をチェックして、ドラッグがどのように機能するかを確認してください。超カッコイイ!

于 2014-01-14T23:12:41.590 に答える
5

私の解決策は次のとおりです。

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

またはバターナイフ:

@OnTouch(R.id.switchView)
public boolean switchStatus(Switch switchView, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        // your code
    }
    return true;
}
于 2015-10-12T11:23:44.157 に答える
0

バインディング アダプターを使用すると、これは次の単純な Kotlin 拡張機能で実行できます。

@SuppressLint("ClickableViewAccessibility")
@BindingAdapter("isSwipeEnabled")
fun SwitchCompat.setSwipeEnabled(isSwipeEnabled: Boolean) {
    setOnTouchListener { _, event ->
        if (isSwipeEnabled)
            false
        else
            event.actionMasked == MotionEvent.ACTION_MOVE
    }
}

次に、次のようなレイアウト ファイルで使用できます。

<com.google.android.material.switchmaterial.SwitchMaterial
        android:id="@+id/switch"
        isSwipeEnabled="@{false}"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
于 2022-01-17T15:54:08.267 に答える