问题描述
编辑:此问题已转换为另一个问题
这是更新的问题的链接。 Android App Mouse Pointer Input Not working well on Activity UI Elements
这个问题:
我正在构建一个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应用程序用户界面上均未检测到任何类型的左键单击,但可在手机或标签页上使用。
我尝试过什么?
我使用以下代码来检测视图中的点击。因此,所有鼠标或触摸事件都将被检测到,并在给定XML代码的TextView id'xd'上将输出打印在Textview中以进行调试。
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;
}
});