Android RX绑定并单击右侧可绘制
有没有一种方法可以通过使用RxBinding在EditText的右侧可绘制部分实现单击侦听器 我唯一发现的是:Android RX绑定并单击右侧可绘制,android,textview,rx-java,rx-java2,rx-binding,Android,Textview,Rx Java,Rx Java2,Rx Binding,有没有一种方法可以通过使用RxBinding在EditText的右侧可绘制部分实现单击侦听器 我唯一发现的是: RxTextView.editorActionEvents(mEditText).subscribeWith(new DisposableObserver<TextViewEditorActionEvent>() { @Override public void onNext(TextViewEditorActionEvent tex
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;
});
});
但是我觉得我是在重新设计轮子,你在错误的地方搜索,如果你需要检查触摸事件,使用RxView使用基本视图触摸事件,然后应用你的逻辑并过滤掉所有不需要的触摸,以便在你想要的位置上进行“点击”。 我必须承认,我不确定我是否理解for循环逻辑,您可以直接使用UiUtil.component\u DRAWABLE.DRAWABLE\u,但无论如何,在本例中遵循了您的逻辑:
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.
});
}