Input del puntatore del mouse dell'app Android Non funziona bene sugli elementi dell'interfaccia utente delle attività

Aug 19 2020

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

Sto lavorando a un'app Android che funziona su cellulari, tablet e TV Box. La mia app dovrà gestire gli input da Touch, Mouse Pointer o Remote.

Attualmente, My Code funziona bene per ottenere la funzionalità, ma il comportamento anomalo nella gestione degli input del mouse si verifica solo su TV Box.

Problema: quando l'app si avvia, il telecomando può navigare facilmente sugli focusableelementi ma il mouse sul puntatore non viene rilevato dagli elementi dell'app. Ora Quando il puntatore del mouse viene portato su Volume Option e poi torna con un clic. L'app inizia a rilevare il mouse ma dopo alcune azioni, si perde nuovamente focusdall'interfaccia utente dell'app e il movimento del puntatore del mouse e i clic non vengono più rilevati.

Guarda il video dal link sottostante per vedere come si sta comportando attualmente l'app. https://jumpshare.com/v/bvyfbqZFWNJfoUsXJbSU

Sintesi del problema: poiché il controller del volume viene visualizzato sopra l'app, il mouse può apportare modifiche al tracker del volume e uscire dopo aver fatto clic con il pulsante sinistro del mouse. L'app diventa sensibile al passaggio del mouse e all'azione del puntatore del mouse e quando viene eseguita un'azione sull'app, questa non risponde nuovamente.

Alcune condizioni che l'app dovrebbe seguire:

  • SDK minimo 19
  • ScrollView

Codice

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">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30sp"
                android:fontFamily="@font/poppinsregular"
                android:gravity="center"
                android:text="Mouse and Remote Input App"
                android:textColor="#000000"
                android:textSize="30sp" />


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

            <Button
                android:id="@+id/btn2"
                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="Button 2"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"

                android:layout_marginTop="20sp"
                android:fontFamily="@font/poppinsregular"
                android:text="Try Switch 1"
                android:textColor="#000000"
                android:textSize="20sp" />

            <Switch
                android:id="@+id/switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="2sp"
                android:focusable="true"
                android:focusableInTouchMode="true"

                />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/poppinsregular"
                android:text="Edit Text 1"
                android:textColor="#000000"
                android:textSize="20sp" />

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


        </LinearLayout>

    </ScrollView>
    </LinearLayout>

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("ClickableViewAccessibility")
    @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("4");


        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("");
                Toast toast = Toast.makeText(getApplicationContext(), "Button 1 Clicked", 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(), "Button 1 Touched", 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("");
                Toast toast = Toast.makeText(getApplicationContext(), "Button 2 Clicked", 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(), "Button 2 Touched", 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("")) {

                    Toast toast = Toast.makeText(getApplicationContext(), "Edit Text Changed to: " + 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(""+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("Switch State=", "" + isChecked);
                Toast toast = Toast.makeText(getApplicationContext(), "Switch Boolean: " + 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

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



</manifest>

Risposte

RahulShyokand Sep 08 2020 at 09:17

Ho risolto questo strano problema dopo aver rimosso get getSupportActionBar().hide();dal mio MainActivity .

Descrizione della soluzione: ho scoperto che quando la barra dell'app non veniva rimossa dal tema, gli eventi del mouse sull'app hanno iniziato a funzionare.

Stavo nascondendo la barra delle app per un'interfaccia utente pulita per il mio caso d'uso, ma era solo un miglioramento, quindi mostra la barra delle app per farlo funzionare. Spero che questo ti aiuti se sei bloccato con lo stesso caso unico.