0

有没有办法通过使用 RxBinding 在 EditText 的右侧 drawable 上实现点击监听器?

我唯一发现的是:

     RxTextView.editorActionEvents(mEditText).subscribeWith(new DisposableObserver<TextViewEditorActionEvent>() {
        @Override
        public void onNext(TextViewEditorActionEvent textViewEditorActionEvent) {
            int actionId = textViewEditorActionEvent.actionId();
            if(actionId == MotionEvent.ACTION_UP) {
            }

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    });

但在这种情况下,我找不到有关点击位置的信息。

这是我使用 RxJava 的方式:

public Observable<Integer> getCompoundDrawableOnClick(EditText editText, int... drawables) {
    return Observable.create(e -> {
        editText.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                for (int i : drawables) {
                    if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
                        if (event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
                            e.onNext(i);
                            return true;
                        }
                    }
                }
            }
            // add the other cases here
            return false;

        });
    });

但我觉得我在重新发明轮子

4

1 回答 1

1

你在错误的地方搜索,如果你需要检查触摸事件,使用基本View触摸事件RxView,然后应用你的逻辑并过滤掉所有不需要的触摸,以便在你想要的位置上“点击”(复合可绘制) .
我必须承认我不确定我是否理解 for 循环逻辑,您可以直接使用UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT,但无论如何在此示例中遵循您的逻辑:

public Observable<Object> getCompoundDrawableOnClick(EditText editText, int... drawables) {
        return RxView.touches(editText)
                .filter(motionEvent -> {
                    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                        for (int i : drawables) {
                            if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
                                if (motionEvent.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
                                    return true;
                                }
                            }
                        }
                    }
                    return false;
                })
                .map(motionEvent -> {
                    // you can omit it if you don't need any special object or map it to 
                    // whatever you need, probably you just want click handler so any kind of notification Object will do.
                });
    }
于 2017-06-10T19:39:17.860 回答