CheckBox OnLongClick行为

时间:2016-05-08 15:07:39

标签: android checkbox

我有垂直放置的物品清单。每个项目都有一个CheckBox和一个TextView。 我正在尝试为CheckBox实现LongClick行为。更确切地说,我希望它在LongClicked时使用垃圾图标进行更改。在发布时,它必须再次成为一个复选框,并在单击时保持检查/取消复选行为。

我怎样才能实现这一目标?有多少种方法? 我帮助任何帮助之王!

我尝试使用setBackgroud()函数,但它只会在现有的复选框视图下绘制,框仍然位于顶部。

cb.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Resources res = tmp_context.getResources();
                Drawable drawable = res.getDrawable(R.mipmap.ic_trash);
                cb.setBackground(drawable);
                return false;
            }
        });

1 个答案:

答案 0 :(得分:0)

<强>更新

首先将你的chekcbox放在一个framelayout中:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/one"
    android:id="@+id/fl">
    <CheckBox

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Two"
        android:longClickable="true"
        android:id="@+id/two"/>
</FrameLayout>

您可以使用LongClickListener和OnTouchListener:

private View.OnLongClickListener pressHoldListener = new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View pView) {
        // Do something when your hold starts here.
        isSpeakButtonLongPressed = true;
        mFrameLayout.setBackgroundResource(R.drawable.trash_can);
        yourCheckBox.setVisibility(VIEW.INVISIBLE);
        return true;
    }
};

private View.OnTouchListener pressTouchListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View pView, MotionEvent pEvent) {
        pView.onTouchEvent(pEvent);
        // We're only interested in when the button is released.
        if (pEvent.getAction() == MotionEvent.ACTION_UP) {
            // We're only interested in anything if our speak button is currently pressed.
            if (isSpeakButtonLongPressed) {
                // Do something when the button is released.
                yourCheckBox.setVisibility(View.VISIBLE);
                mFrameLayout.setBackground(null);
                isSpeakButtonLongPressed = false;
            }
        }
        return false;
    }
};

在你里面onCreate():

yourCheckBox.setOnLongClickListener(pressHoldListener);  
yourCheckBox.setOnTouchListener(pressTouchListener);

同时添加

android:longClickable="true"

到您的复选框xml。