【问题标题】:Custom view 'CustomAutoCompleteTextView' has setOnTouchListener called on but does not override performClick自定义视图“CustomAutoCompleteTextView”已调用 setOnTouchListener 但未覆盖 performClick
【发布时间】:2018-04-16 05:22:46
【问题描述】:

我为我的 AutocompleteTextView 创建了一个 onTouchListener。但是 onTouch() 方法显示警告:

如果覆盖 onTouchEvent 或使用 OnTouchListener 的 View 没有实现 performClick 并在检测到点击时调用它,则 View 可能无法正确处理可访问性操作。理想情况下,处理点击动作的逻辑应该放在 View#performClick 中,因为一些无障碍服务会在发生点击动作时调用 performClick。

我不明白这是什么意思。这是代码。

actvEntryCategory.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                try{
                    actvEntryCategory.clearFocus();
                    actvEntryCategory.requestFocus();
                }catch (Exception e){
                    Log.d("eEMP/EntryCreate", "Error raised at EntryCategory touch event due to " + e.toString());
                }
                return false;
            }
        });

我是 Andoird 的新手。任何帮助将不胜感激。

【问题讨论】:

  • But onTouch() method shows warning: 什么警告请与问题分享
  • 我已经发布了警告。请检查@NileshRathod

标签: android


【解决方案1】:

自动完成文本视图。但是 onTouch() 方法会显示警告:如果覆盖 onTouchEvent 或使用 OnTouchListener 的 View 也没有实现 performClick

来自文档

Handling custom touch events

自定义视图控件可能需要非标准的触摸事件行为。例如,自定义控件可能会使用onTouchEvent(MotionEvent) 监听器方法来检测ACTION_DOWNACTION_UP 事件并触发特殊的点击事件。为了保持与无障碍服务的兼容性,处理此自定义点击事件的代码必须执行以下操作:

  1. 为解释的点击操作生成适当的 AccessibilityEvent。

  2. 启用无障碍服务,以便为无法使用触摸屏的用户执行自定义点击操作。

要以有效的方式处理这些要求,您的代码应覆盖performClick() 方法,该方法必须调用此方法的超级实现,然后执行单击事件所需的任何操作。当检测到自定义点击操作时,该代码应调用您的 performClick() 方法。下面的代码示例演示了这种模式。

解决方案

  1. 使用 @SuppressLint("ClickableViewAccessibility") 忽略此警告
  2. 第二种解决方案您的自定义视图应覆盖performClick() 方法,

用于覆盖文档中的 performClick() 方法的示例代码

class CustomTouchView extends View {

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

    boolean mDownTouch = false;

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

        // Listening for the down and up touch events
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownTouch = true;
                return true;

            case MotionEvent.ACTION_UP:
                if (mDownTouch) {
                    mDownTouch = false;
                    performClick(); // Call this method to handle the response, and
                                    // thereby enable accessibility services to
                                    // perform this action for a user who cannot
                                    // click the touchscreen.
                    return true;
                }
        }
        return false; // Return false for other touch events
    }

    @Override
    public boolean performClick() {
        // Calls the super implementation, which generates an AccessibilityEvent
        // and calls the onClick() listener on the view, if any
        super.performClick();

        // Handle the action for the custom click here

        return true;
    }
}

【讨论】:

  • 但是如何为 AutocompleteTextview 设置 onTouchListener 像这样 actvEntryCategory.setOnTouchListener @Nilesh Rathod
  • @vishnu 没有得到你
  • 在我的代码中,我为 actvEntryCategory 调用了 setOnTouchListener。当我单击 actvEntryCategory 时,将调用此方法。但是在您的解决方案中,没有 setOnTouchListener。当 onTouchEvent 被调用时。你能为我的代码制定解决方案吗
  • @vishnu 为什么在 AutocompleteTextview 中需要 TouchListener
  • 当我点击该 AutoCompleteTextview 时,将调用 TouchListener 并且我的 customTextview 获得焦点@Nilesh Rathod
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多