java 自定义视图“ImageButton”已调用 setOnTouchListener 但不会覆盖 performClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46135249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:05:15  来源:igfitidea点击:

Custom view 'ImageButton' has setOnTouchListener called on it but does not override performClick

javaandroidwarnings

提问by AeonDave

Well, I know there are some questions regarding this warning, but i still cannot figure out how to get rid of this. I don't want to implement the OnTouchListenerinterface on class level, because there are many buttons and i prefer to keep every piece of code on his own "space". I added performClick()after ACTION_UPcase but the warning still persist.

好吧,我知道有一些关于这个警告的问题,但我仍然不知道如何摆脱这个。我不想OnTouchListener在类级别实现接口,因为有很多按钮,我更喜欢将每一段代码都保留在他自己的“空间”中。我performClick()ACTION_UP案例后添加,但警告仍然存在。

enter image description here

在此处输入图片说明

Thank you

谢谢

send.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_UP:
                            view.performClick();
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });

回答by Dan

Your code is OK, if you are sure that your code works like you want, you can just disable this kind of warnings in: File-> Settings-> Editor-> Inspections-> Android-> Lint-> Accessibility-> Accessibility in Custom Views

你的代码没问题,如果你确定你的代码像你想要的那样工作,你可以禁用这种警告:文件->设置->编辑器->检查-> Android-> Lint->辅助功能->辅助功能自定义视图

回答by ThanosFisherman

Basically it suggests that you should subclass ImageButtonand override its performClick()method but it's not worth the hassle just suppress this warning using @SuppressLint("ClickableViewAccessibility")in your method or just disable this warning like I did.

基本上它建议你应该子类化ImageButton并覆盖它的performClick()方法,但是@SuppressLint("ClickableViewAccessibility")在你的方法中使用这个警告或者像我一样禁用这个警告是不值得的。

回答by Malik Ahsan

Change your this line of code

改变你的这行代码

View.performClick ();

TO

send.performClick ();

And add this override method at the end of you activity.

并在您的活动结束时添加此覆盖方法。

@Override
public boolean performClick() {
// do what you want
return true;
}

Hope that helps

希望有帮助