CheckBox replace setOnCheckedChangeListener with setOnLongClickListener

时间:2016-02-12 20:42:27

标签: android android-layout checkbox onlongclicklistener

I have a CheckBox that I need to respond to OnLongClickListener instead of OnCheckedChangeListener. How do I do that? Right now even though I implement the OnLongClickListener, the CheckBox still toggles when a user clicks it. I want the toggle to happen on longClick only as in

Stopping nginx:               [OK]

1 个答案:

答案 0 :(得分:0)

我会建议两件事......

不确定它们是否有效。我没有测试过。

也许您可以禁用布局文件上的点击。仅启用长按。

  <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:longClickable="true"
        ..... />

或者,您可以覆盖onCheckedChangeListener以不执行任何操作。

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        checkBox.setChecked(!isChecked);
    }
});