代码之家  ›  专栏  ›  技术社区  ›  Binary Baba

双击不会在对讲模式下单击父视图

  •  2
  • Binary Baba  · 技术社区  · 7 年前

    我有以下xml文件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/linear_layout1"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:focusable="true"
            android:layout_margin="10dp"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:text="first text"
                android:layout_margin="10dp"/>
    
            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="second text"
                android:layout_margin="10dp"/>
    
        </LinearLayout>
    
    </LinearLayout>
    

    我为LinearLayout和TextView设置了一个单击侦听器。

    TextView textView1 = (TextView) findViewById(R.id.text1);
    LinearLayout linearLayout = findViewById(R.id.linear_layout1);
    linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(v.getContext(), "Linear layout clicked", Toast.LENGTH_SHORT).show();
                    }
                });
    
    textView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "text view clicked", Toast.LENGTH_SHORT).show();
                }
            });
    

    我已经将LinearLayout设置为可聚焦,以便在对讲模式下,它可以获得可访问性焦点。现在,当我双击它时,将调用文本视图的click事件,而不是线性布局。

    如何让它调用线性布局的单击侦听器?

    3 回复  |  直到 7 年前
        1
  •  5
  •   dane123    6 年前

    我也遇到了同样的问题,我通过创建新视图解决了这个问题,该视图扩展了LinearLayout,在这个视图中,我覆盖了 onInterceptTouchEvent() 方法在此方法中,检查是否启用了辅助功能以及是否选择了视图组(LinearLayout)。如果是,则返回true并拦截此事件。

    public class AccessibleLinearLayout extends LinearLayout {
    
        public AccessibleLinearLayout(Context context) {
            super(context);
        }
    
        public AccessibleLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public AccessibleLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled() && isAccessibilityFocused()) {
                return true;
            }
            return super.onInterceptTouchEvent(ev);
        }
    }
    

    我不确定你是否需要检查 ((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled() 当你检查是否 isAccessibilityFocused()

        2
  •  4
  •   MobA11y    7 年前

    这里有一些有趣的事情。这是两种不同的对讲方法*点击事件处理。

    1. 旧对讲传递不同于正常触摸事件的事件。它不发送物理触摸事件,而是发送调用“单击操作”的请求。因此,不,点击不会像正常的“点击”那样传播。

    2. TalkBack现在依靠手势API发送实际的物理事件。这些人工物理点击事件被发送到视图的中心,该视图通过对讲发送了激活操作(双击操作)。

    你所面临的问题是,这两种行为将打破两种显而易见的解决方案之一。您使用了一个显而易见的解决方案(仅使用多个单击侦听器,并假设事件被传递到聚焦节点),但它没有起作用,因为您碰巧使用的技术组合是V2(发送人工硬件单击)。然而,如果你要在三星设备上安装你的应用程序,你很可能会得到你想要的行为。。。但这并不意味着它有效!:)

    由于这两种不同的行为,使其普遍工作的唯一方法(所有版本的TalkBack和Android)是重写其中一个视图AccessibilityDelegates(实际上可能是两个视图),并拦截这些事件。不要让它们进入默认处理程序,默认处理程序是上述两种方法之一。相反,调用你想调用的逻辑,并在自己周围传递通风口。

    • 注意:这实际上可能更依赖于Android操作系统的版本/制造商,但我现在不在我的开发机器上,信不信由你,但在web浏览器中浏览AOSP代码不会发生在周日晚上10点。稍后,我可能会更新此答案,并提供更多详细信息。
        3
  •  -2
  •   Balu Sangem    7 年前

    高度和宽度 TextView 是问题

    更改自

    <TextView
      android:id="@+id/text1"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:text="first text"
      android:layout_margin="10dp"/>
    

    <TextView
      android:id="@+id/text1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="first text"
      android:layout_margin="10dp"/>