我创建了一个onTouchListener
。不幸的是onTouch()方法throws
给我一个警告:
com/calculator/activitys/Calculator$1#onTouch should call View#performClick when a click is detected
这意味着什么?我没有找到有关此警告的任何信息。这是完整的代码:
LinearLayout llCalculatorContent = (LinearLayout) fragmentView.findViewById(R.id.calculator_content);
llCalculatorContent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Tools.hideKeyboard(getActivity(), getView());
getView().clearFocus();
return false;
}
});
答案 0 :(得分:98)
你走了:
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//some code....
break;
case MotionEvent.ACTION_UP:
v.performClick();
break;
default:
break;
}
return true;
}
答案 1 :(得分:1)
如果您未使用显式覆盖Custom View
的{{1}},则仅遵循Secko的回答就不会删除警告。
除了他的答案外,要在类似onPerformClick
或android.widget.Button
的类上执行相同的操作,您还需要创建一个简单的自定义视图来扩展目标视图。
示例:
自定义视图类:
Button
XML :
public class UselessButton extends AppCompatButton {
public UselessButton(Context context) {
super(context);
}
public UselessButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UselessButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean performClick() {
return super.performClick();
}
}
Java :
<stackoverflow.onEarth.UselessButton
android:id="@+id/left"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/left"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/right"
app:layout_constraintVertical_bias="0.5" />
当前问题:IDE可以解决警告,但无法在实际的Android设备上看到此按钮实际上执行的点击操作。
编辑:修复了点击事件:使用 left.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
enLeft = 1;
enRight = 0;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
enLeft = 0;
v.performClick();
return false;
} else {
return false;
}
});
View.setPressed(boolean)
答案 2 :(得分:0)
您可以抑制皮棉
@SuppressLint("ClickableViewAccessibility")
您应该在performClick()
内致电onTouchEvent()
。
@Override
public boolean onTouchEvent(MotionEvent event) {
//A logic
performClick();
return super.onTouchEvent(event);
}
了解更多here
答案 3 :(得分:0)
只需调用performClick方法,如下所示:
@Override
public boolean onTouch(View v, MotionEvent event) {
v.performClick();
Tools.hideKeyboard(getActivity(), getView());
getView().clearFocus();
return false;
}
答案 4 :(得分:0)
我在使用MultiTouchListener
时遇到了类似的问题,并解决了该问题,它实现了GestureDetector
并监听了SingleTap
(这不会删除警告,但会触发onClick
我认为发生的事件
class TouchListener(context: Context) : MultiTouchListener() {
private var tochedView: View? = null
private var mGestureDetector = CustomGestureDetector()
private var gestureDetector: GestureDetector = GestureDetector(context, mGestureDetector)
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(view: View?, event: MotionEvent?): Boolean {
val aux = super.onTouch(view, event)
tochedView = view
gestureDetector.onTouchEvent(event)
return aux
}
private inner class CustomGestureDetector: GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent?): Boolean {
// this will be called even when a double tap is
tochedView?.performClick()
return super.onSingleTapUp(e)
}
override fun onSingleTapConfirmed(e: MotionEvent?): Boolean {
// this will only be called after the detector is confident that the
// user's first tap is not followed by a second tap leading to a double-tap gesture.
tochedView?.performClick()
return super.onSingleTapConfirmed(e)
}
}
}
答案 5 :(得分:0)
这是一个超级迟到的答案,但我希望它会对某人有所帮助。不久前我遇到了这个警告,我的方法是,创建一个扩展 View.class
的类,然后在该类中创建一个名为 closeKeyboard(View view){}
的公共方法,如下所示:
public class MyTouchEvent extends View {
public MyTouchEvent(Context context) {
super(context);
}
public void closeKeyboard(View view) {
view.setOnTouchListener((view1, motionEvent) -> {
view1.performClick();
// this is a library handels the closing of keyboard
UIUtil.hideKeyboard((Activity) getContext());
return false;
});
}
@Override
public boolean performClick() {
super.performClick();
return true;
}
}
在 MainActivity
中,我调用了 closeKeyboard(view)
并使用父布局作为参数,如下所示:
new MyTouchEvent(this).collapseKeyboard(parentLayout);
用于关闭键盘的库
implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC2'
答案 6 :(得分:0)
我不得不忽略它。我有一个 TouchListener 来触发动画按钮推送(在我的情况下改变颜色)。但我也有一个点击监听器来完成按钮的工作。所以这导致我的点击监听器再次触发。我在触摸事件(Action_Down、Up 等)的切换中尝试将其作为默认情况