首页 > 解决方案 > 在我的 Android 应用程序上处理来自 Android TV Box 中鼠标指针的用户输入

问题描述

编辑:这个问题已经变成了另一个问题

这是更新问题的链接。 Android 应用鼠标指针输入在 Activity UI 元素上效果不佳

这个问题 :

我正在构建一个需要在 Android 电视盒以及标签和手机上运行的 Android 应用程序。我已经能够根据需要制作应用程序,例如处理来自 USB 鼠标和手机触摸的用户输入,但在检测电视盒上的 USB 鼠标输入时遇到问题。远程控制输入工作正常,但在应用程序 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

我可以从这里尝试哪些选项来解决它?

标签: androidandroid-tvtvbox

解决方案


 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;
}
});

推荐阅读