Android按钮已调用setOnTouchListener,但未覆盖performClick [英] Android button has setOnTouchListener called on it but does not override performClick

查看:1428
本文介绍了Android按钮已调用setOnTouchListener,但未覆盖performClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将onTouchListner()添加到按钮时,它会带给我

When I try to add onTouchListner() to a button, it gets me the

按钮已调用setOnTouchListener,但没有覆盖 performClick

Button has setOnTouchListener called on it but does not override performClick

警告.有人知道如何解决吗?

warning. Does anyone know how to fix it?

btnleftclick.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }
});

错误:

自定义视图已调用setOnTouchListener,但没有覆盖 performClick如果View覆盖onTouchEvent或使用 OnTouchListener也未实现performClick并在以下情况下调用它 检测到点击,该视图可能无法处理辅助功能 适当地.理想情况下,应将处理点击动作的逻辑放入 View#performClick,因为某些辅助功能调用了performClick 当应该执行点击操作时.

Custom view has setOnTouchListener called on it but does not override performClick If a View that overrides onTouchEvent or uses an OnTouchListener does not also implement performClick and call it when clicks are detected, the View may not handle accessibility actions properly. Logic handling the click actions should ideally be placed in View#performClick as some accessibility services invoke performClick when a click action should occur.

推荐答案

出现此警告是因为Android希望提醒您考虑可能正在使用您的应用的盲人或视力障碍者.建议您观看此视频,以快速了解该内容就像.

This warning comes up because Android wants to remind you to think about the blind or visually impaired people who may be using your app. I suggest you watch this video for a quick overview about what that is like.

所有标准UI视图(如ButtonTextView等)均已设置为通过可访问性服务为盲人用户提供适当的反馈.当您尝试自己处理触摸事件时,您可能会忘记提供反馈.这就是警告的目的.

The standard UI views (like Button, TextView, etc.) are all set up to provide blind users with appropriate feedback through Accessibility services. When you try to handle touch events yourself, you are in danger of forgetting to provide that feedback. This is what the warning is for.

处理触摸事件通常是在自定义视图中完成的事情.不要太快地取消此选项.并不是那么困难.以下是TextView的完整示例,该示例已被覆盖以处理触摸事件:

Handling touch events is normally something that is done in a custom view. Don't dismiss this option too quickly. It's not really that difficult. Here is a full example of a TextView that is overridden to handle touch events:

public class CustomTextView extends AppCompatTextView {

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return true;

            case MotionEvent.ACTION_UP:
                performClick();
                return true;
        }
        return false;
    }

    // Because we call this from onTouchEvent, this code will be executed for both
    // normal touch events and for when the system calls this using Accessibility
    @Override
    public boolean performClick() {
        super.performClick();
        doSomething();
        return true;
    }

    private void doSomething() {
        Toast.makeText(getContext(), "did something", Toast.LENGTH_SHORT).show();
    }
}

然后,您将像这样使用它:

Then you would just use it like this:

<com.example.myapp.CustomTextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="Click me to do something"/>

有关创建自定义视图的更多详细信息,请参见我的其他答案.

See my other answer for more details about making a custom view.

其他时候,最好只是静默警告.例如,我不确定需要使用触摸事件的Button是什么.如果像我上面对自定义TextView所做的那样,要制作一个自定义按钮并在onTouchEvent中调用performClick(),则每次都会调用两次,因为Button已经调用了performClick().

Other times it might be better to just silence the warning. For example, I'm not sure what it is you want to do with a Button that you need touch events for. If you were to make a custom button and called performClick() in onTouchEvent like I did above for the custom TextView, then it would get called twice every time because Button already calls performClick().

有两个原因可能会让您忽略警告:

Here are a couple reasons you might want to just silence the warning:

  • 您通过触摸事件执行的工作只是视觉上的.它不会影响您的应用程序的实际运行.
  • 您很冷漠,不关心让世界成为盲人的美好地方.
  • 您太懒了,无法复制和粘贴我在上面的选项1中给您的代码.

在方法的开头添加以下行以禁止显示警告:

Add the following line to the beginning of the method to suppress the warning:

@SuppressLint("ClickableViewAccessibility")

例如:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button myButton = findViewById(R.id.my_button);
    myButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return false;
        }
    });
}

这篇关于Android按钮已调用setOnTouchListener,但未覆盖performClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆