Android应用程序鼠标指针输入在Activity UI元素上无法正常工作。

huangapple go评论76阅读模式
英文:

Android App Mouse Pointer Input Not working well on Activity UI Elements

问题

已解决!https://stackoverflow.com/a/63790855/12021422

我正在开发一个Android应用,该应用适用于手机、平板和电视机顶盒。我的应用将需要处理来自触摸、鼠标指针或遥控器的输入。

目前,我的代码能够很好地实现所需的功能,但在处理鼠标输入方面,只在电视机顶盒上出现异常行为。

问题:当应用启动时,遥控器可以轻松导航到“可聚焦”元素,但是指针处的鼠标不能被应用元素检测到。
现在,当鼠标指针移到音量选项并在点击后返回时,应用开始检测鼠标,但在执行某些操作后,它再次从应用界面中失去焦点,鼠标指针的移动和点击再次变得不被检测到。

请查看以下链接中的视频,以查看应用当前的行为:https://jumpshare.com/v/bvyfbqZFWNJfoUsXJbSU

问题要点:由于音量控制器显示在应用之上,鼠标可以在调整音量跟踪器并在进行左键单击后退出。应用程序对悬停和鼠标指针操作变得响应,但在应用上执行某些操作后,它再次变得不响应。

应用应遵循以下条件:

  • 最低SDK版本19
  • ScrollView

代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dad6d6"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!-- 其他控件被省略... -->

        &lt;/LinearLayout&gt;

    &lt;/ScrollView&gt;
    &lt;/LinearLayout&gt;

MainActivity.java

package com.example.remoteandmouseapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    // MainActivity的代码被省略...
}

AndroidManifest.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    package=&quot;com.example.remoteandmouseapp&quot;&gt;

    <!-- 其他配置被省略... -->

&lt;/manifest&gt;

(注意:这只是代码的一部分,根据您的要求,我只提供了翻译后的部分内容。如果需要完整的翻译,请继续提供原始内容。)

英文:

Solved! https://stackoverflow.com/a/63790855/12021422

I am working on an Android App which works on Mobiles, Tabs and TV Boxes. My app will need to handle inputs from Touch, Mouse Pointer or Remote.

Currently, My Code works well to achieve the functionality but there is abnormal behaviour in handling of Mouse inputs only happens on TV Box.

Problem: When the app starts the remote can easily navigate on the focusable elements
but the mouse at pointer does not get detected by app elements.
Now When a mouse pointer is taken to Volume Option and then comes back with a Click. The app starts detecting mouse but after some Action, it again looses focus from App UI and motion of the mouse pointer and clicks become undetected again.

Please Watch the Video from Link below to see How the app is currently behaving.
https://jumpshare.com/v/bvyfbqZFWNJfoUsXJbSU

Problem Gist: As the Volume controller is displayed above the app the mouse can make changes on volume tracker and come out after making left-click. The app becomes responsive to hovers and mouse pointer action and when some action is made on the app it again becomes unresponsive.

Some Conditions that App should Follow:

  • Minimum SDK 19
  • ScrollView

Code

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/root_layout&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;#dad6d6&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;ScrollView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;vertical&quot;&gt;


            &lt;TextView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;30sp&quot;
                android:fontFamily=&quot;@font/poppinsregular&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;Mouse and Remote Input App&quot;
                android:textColor=&quot;#000000&quot;
                android:textSize=&quot;30sp&quot; /&gt;


            &lt;Button
                android:id=&quot;@+id/btn1&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:background=&quot;@drawable/buttondesign&quot;
                android:focusable=&quot;true&quot;
                android:focusableInTouchMode=&quot;true&quot;
                android:fontFamily=&quot;@font/poppinsregular&quot;
                android:text=&quot;Button 1&quot;
                android:textColor=&quot;#ffffff&quot;
                android:textSize=&quot;20sp&quot;
                android:textStyle=&quot;bold&quot; /&gt;

            &lt;Button
                android:id=&quot;@+id/btn2&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;10sp&quot;
                android:background=&quot;@drawable/buttondesign&quot;
                android:focusable=&quot;true&quot;
                android:focusableInTouchMode=&quot;true&quot;
                android:fontFamily=&quot;@font/poppinsregular&quot;
                android:text=&quot;Button 2&quot;
                android:textColor=&quot;#ffffff&quot;
                android:textSize=&quot;20sp&quot;
                android:textStyle=&quot;bold&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;

                android:layout_marginTop=&quot;20sp&quot;
                android:fontFamily=&quot;@font/poppinsregular&quot;
                android:text=&quot;Try Switch 1&quot;
                android:textColor=&quot;#000000&quot;
                android:textSize=&quot;20sp&quot; /&gt;

            &lt;Switch
                android:id=&quot;@+id/switch1&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:layout_marginTop=&quot;2sp&quot;
                android:focusable=&quot;true&quot;
                android:focusableInTouchMode=&quot;true&quot;

                /&gt;


            &lt;TextView
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:fontFamily=&quot;@font/poppinsregular&quot;
                android:text=&quot;Edit Text 1&quot;
                android:textColor=&quot;#000000&quot;
                android:textSize=&quot;20sp&quot; /&gt;

            &lt;EditText
                android:id=&quot;@+id/edittext1&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;center&quot;
                android:autofillHints=&quot;&quot;
                android:background=&quot;@drawable/edittext_design&quot;
                android:ems=&quot;5&quot;
                android:focusable=&quot;true&quot;
                android:focusableInTouchMode=&quot;true&quot;
                android:hint=&quot;Number&quot;
                android:inputType=&quot;number&quot;
                android:textAlignment=&quot;center&quot;
                android:textColor=&quot;@android:color/black&quot;
                android:textSize=&quot;30sp&quot; /&gt;


        &lt;/LinearLayout&gt;

    &lt;/ScrollView&gt;
    &lt;/LinearLayout&gt;

MainActivity.java

package com.example.remoteandmouseapp;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
LinearLayout root_layout;
Switch switch1;
EditText edittext1;
Button btn1, btn2;
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@SuppressLint(&quot;ClickableViewAccessibility&quot;)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
switch1 = (Switch) findViewById(R.id.switch1);
edittext1 = (EditText) findViewById(R.id.edittext1);
root_layout = findViewById(R.id.root_layout);
edittext1.setText(&quot;4&quot;);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//textInfo.setText(&quot;&quot;);
Toast toast = Toast.makeText(getApplicationContext(), &quot;Button 1 Clicked&quot;, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
btn1.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
if ((arg1.getAction() == MotionEvent.ACTION_UP)) {
Toast toast = Toast.makeText(getApplicationContext(), &quot;Button 1 Touched&quot;, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
return true;
}
});
btn1.setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
btn1.setBackgroundResource(R.drawable.onfoucsbuttondesign);
}
if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
btn1.setBackgroundResource(R.drawable.buttondesign);
}
return true;
}
});
btn1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
btn1.setBackgroundResource(R.drawable.onfoucsbuttondesign);
} else {
btn1.setBackgroundResource(R.drawable.buttondesign);
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//textInfo.setText(&quot;&quot;);
Toast toast = Toast.makeText(getApplicationContext(), &quot;Button 2 Clicked&quot;, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
btn2.setOnHoverListener(new View.OnHoverListener() {
@Override
public boolean onHover(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
btn2.setBackgroundResource(R.drawable.onfoucsbuttondesign);
}
if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
btn2.setBackgroundResource(R.drawable.buttondesign);
}
return true;
}
});
btn2.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
if ((arg1.getAction() == MotionEvent.ACTION_UP)) {
Toast toast = Toast.makeText(getApplicationContext(), &quot;Button 2 Touched&quot;, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
return true;
}
});
btn2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
btn2.setBackgroundResource(R.drawable.onfoucsbuttondesign);
} else {
btn2.setBackgroundResource(R.drawable.buttondesign);
}
}
});
edittext1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (!s.toString().equals(&quot;&quot;)) {
Toast toast = Toast.makeText(getApplicationContext(), &quot;Edit Text Changed to: &quot; + s, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
}
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// status.setText(&quot;&quot;+s);
}
});
edittext1.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
if ((arg1.getAction() == MotionEvent.ACTION_UP)) {
edittext1.requestFocus();
edittext1.setCursorVisible(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext1, InputMethodManager.SHOW_IMPLICIT);
edittext1.setSelection(edittext1.getText().length());
}
return true;
}
});
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.v(&quot;Switch State=&quot;, &quot;&quot; + isChecked);
Toast toast = Toast.makeText(getApplicationContext(), &quot;Switch Boolean: &quot; + isChecked, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
switch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
toggleSwitchOnTap();
}
});
switch1.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
if ((arg1.getAction() == MotionEvent.ACTION_UP)) {
toggleSwitchOnTap();
}
return true;
}
});
}
public void toggleSwitchOnTap() {
if (switch1.isChecked()) {
switch1.setChecked(false);
} else {
switch1.setChecked(true);
}
}
}

AndroidManifest.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
package=&quot;com.example.remoteandmouseapp&quot;&gt;
&lt;uses-feature android:name=&quot;android.hardware.touchscreen&quot;
android:required=&quot;false&quot; /&gt;
&lt;uses-feature android:name=&quot;android.hardware.faketouch&quot;
android:required=&quot;false&quot;
/&gt;
&lt;application
android:allowBackup=&quot;false&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;Testname&quot;
android:roundIcon=&quot;@mipmap/ic_launcher&quot;
android:hardwareAccelerated=&quot;true&quot;
android:largeHeap=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;
&gt;
&lt;activity android:name=&quot;.MainActivity&quot;
android:label=&quot;testname&quot;
android:configChanges=&quot;keyboard|keyboardHidden|navigation&quot;
&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

答案1

得分: 0

我在从我的MainActivity中移除getSupportActionBar().hide();后解决了这个奇怪的问题。

解决方法说明:我发现当应用程序栏未从主题中移除时,应用程序上的鼠标事件开始工作。

我之前为了实现干净的用户界面而隐藏了应用程序栏,但那只是一个增强功能,所以取消隐藏应用程序栏以使其工作。

我希望这对于你遇到的相同独特情况有所帮助。

英文:

I solved this weird Issue after removing get getSupportActionBar().hide(); from my MainActivity.

Solution Description: I found that when the app bar was not removed from the theme the mouse events on the app started working.

I was hiding app bar for clean UI for my use case but that was just an enhancement so unhide the app bar to make it work.
I hope this helps if you are stuck with the same unique case.

huangapple
  • 本文由 发表于 2020年8月19日 18:26:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63484966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定