【问题标题】:Handling user Inputs from mouse pointer in Android TV Box on My Android Application在我的 Android 应用程序上处理来自 Android TV Box 中鼠标指针的用户输入
【发布时间】: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吗?还要检查按钮是否正确设置了clickablefocusable 属性
  • 是所有事件都丢失还是只有 ACTION_UP? (例如你得到 ACTION_DOWN)
  • @JensV 是的,当我在电视盒上尝试应用程序时,所有事件都不起作用,但代码和 UI 一切正常,检测标签或手机上的每个事件。我正在使用此示例测试点击事件,它仅适用于移动设备。 developer.android.com/training/gestures/…
  • @JensV 我发现按钮中没有使用可点击属性,但它仍然适用于移动设备。我将添加它并测试它是否对 TV Box 有任何影响。谢谢

标签: android android-tv tvbox


【解决方案1】:
 private int focusedItem=0;
btn.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event){
RecyclerView.LayoutManager lm=recyclerView.getLayoutManager();
if(event.getAction()==KeyEvent.ACTION_DOWN){
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){return tryMoveSelection(lm, 1);} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP){return tryMoveSelection(lm, -1);}}
return false;
}
});

【讨论】:

  • 感谢 anusha... 我已通过使用取消隐藏应用栏来解决此问题,因为隐藏应用栏会导致无法检测到点击的问题。 getSupportActionBar().hide(); 造成了这个奇怪的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多