android - 在我的 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
我可以从这里尝试哪些选项来解决它?
解决方案
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;
}
});
推荐阅读
- python - SSH 上的 Visual Studio Code Python 代码导航
- python - 将“y”轴设置为科学计数法
- javascript - 我们可以在 Web 项目中检测移动设备中的字体大小或字体系列吗?
- python - 清理和替换具有复杂层次结构的数据
- scala - Specs2 单位规格参考
- stored-procedures - 将两个变量连接成新变量
- php - 使用 DeviceCheck API 和服务器端 php 代码无法正常工作的唯一标识 iOS 设备
- javascript - 包含加法和减法运算符的字符串,用于评估答案
- android - 如何从谷歌地图中删除特定的多个标记而不是单个或所有标记
- python - 想在 Ubuntu 中安装 Django