Spinners and Focus

I have a problem with spinners in form activity.

I expected the spinner to start focusing when the user β€œtouches” him, but that doesn't seem to be happening. It seems that the spinner only focuses if I use my tracker ball (on the Nexus One) to move between the various components.

This is annoying because I use the android: selectAllOnFocus = "true" attribute on the first view of the EditText in the form. Since spinners are never distracted from the EditText component, its contents are always welcome (which is an ugly IMO).

I tried using

spinner.requestFocus();

but this (seemingly) has no effect.

I tried to pay attention to the counter in AdapterView.OnItemSelectedListener, but its just the results in

Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44cb0380

Can anyone explain this strange behavior and / or possible ways to use it.

Thank you very much,

Tim

+2
source share
1 answer

You must use setFocusableInTouchMode() . Then you encounter another problem: you need to double-tap the counter to change it (once to set the focus, and then see the list of options again). My solution is to create my own subclass of Spinner, which causes an increase in focus from the first tap to simulate the second:

 class MySpinnerSubclass extends Spinner { private final OnFocusChangeListener clickOnFocus = new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { // We don't want focusing the spinner with the d-pad to expand it in // the future, so remove this listener until the next touch event. setOnFocusChangeListener(null); performClick(); } }; // Add whatever constructor(s) you need. Call // setFocusableInTouchMode(true) in them. @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { // Only register the listener if the spinner does not already have // focus, otherwise tapping it would leave the listener attached. if (!hasFocus()) { setOnFocusChangeListener(clickOnFocus); } } else if (action == MotionEvent.ACTION_CANCEL) { setOnFocusChangeListener(null); } return super.onTouchEvent(event); } } 

To give credit, I received inspiration from Kaptkaos answer to this question .

+1
source

Source: https://habr.com/ru/post/1490565/


All Articles