【发布时间】:2020-12-06 07:19:26
【问题描述】:
编辑:这个问题已经变成另一个问题
这是更新问题的链接。 Android App Mouse Pointer Input Not working well on Activity UI Elements
这个问题:
我正在构建一个 Android 应用程序,它需要在 Android 电视盒以及标签和手机上运行。 我已经能够根据需要制作应用程序,例如处理来自 USB 鼠标和手机触摸的用户输入,但 在检测电视盒上的 USB 鼠标输入时遇到问题。 遥控器输入正常非常好,但在应用程序 UI 上再次未检测到鼠标指针点击。 所有其他应用的 UI 都可以完美地使用鼠标和远程输入。
我正在使用以下监听器来检测鼠标点击或当前代码中的任何点击。
btn.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
if ((arg1.getAction() == MotionEvent.ACTION_UP)) {
// Button Click Using Mouse Detected
// doing Desired Action Here
}
return true;
}
});
我还在同一个按钮上添加了 onClickListener(),但在手机上使用这两个选项都没有问题。
活动 XML 文件:
<!-- Text view to show click event data -->
<TextView
android:id="@+id/xd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20sp"
android:fontFamily="@font/poppinsregular"
android:textColor="#000000"
android:textSize="20sp"
android:text="Test"
android:visibility="visible" />
<!-- button 1 -->
<Button
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/sync_btn_design"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="@font/poppinsregular"
android:text="Tester 1"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />
<!-- button 2 -->
<Button
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:background="@drawable/buttondesign"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="@font/poppinsregular"
android:text="Tester 2"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold" />
基本上任何类型的左键点击都不会在 Android TV Box 应用 UI 上检测到,但适用于手机或标签。
我尝试了什么?
我使用以下代码来检测对我的视图的点击。因此,所有鼠标或触摸事件都会被检测到,并在 Textview 中打印输出,以便在给定 XML 代码中的 TextView id 'xd' 上进行调试。
https://developer.android.com/training/gestures/detector#detect-all-supported-gestures
我可以从这里尝试哪些选项来解决它?
【问题讨论】:
-
你能给我们布局xml吗?还要检查按钮是否正确设置了
clickable和focusable属性 -
是所有事件都丢失还是只有 ACTION_UP? (例如你得到 ACTION_DOWN)
-
@JensV 是的,当我在电视盒上尝试应用程序时,所有事件都不起作用,但代码和 UI 一切正常,检测标签或手机上的每个事件。我正在使用此示例测试点击事件,它仅适用于移动设备。 developer.android.com/training/gestures/…
-
@JensV 我发现按钮中没有使用可点击属性,但它仍然适用于移动设备。我将添加它并测试它是否对 TV Box 有任何影响。谢谢
标签: android android-tv tvbox