Il seguente codicelavorato per me, dopo le altre due rispostenon ha funzionato per me:
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
//passwordInput.requestFocus(); <-- that doesn't work
passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen
}
Dove ShowKeyboard
è
private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
// passwordInput.requestFocusFromTouch();
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
Dopo un inserimento riuscito, mi assicuro anche di nascondere la tastiera
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Tecnicamente, ho appena aggiunto 300 ms di ritardo prima di eseguire la richiesta di visualizzazione della tastiera virtuale. Strano, vero? Anche modificato requestFocus()
a requestFocusFromTouch()
.
MODIFICA: non utilizzare requestFocusFromTouch()
dà un evento tocco al programma di avvio. Attacca con requestFocus()
.
EDIT2: nelle finestre di dialogo (DialogFragment
), utilizzare il seguente
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
invece di
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
stesso risultato di requestFocus() da solo ... Il campo si concentrerà ma la tastiera virtuale non è attivato – fiddler
Ho finalmente risolto il problema chiamando 'field.requestFocus()' nel metodo 'onResume()' dell'attività (invece di 'onCreate()'). Non so esattamente perché funzioni ... – fiddler
Una vista non può essere messa a fuoco prima che sia effettivamente disposta sullo schermo. Questo non può essere fatto mentre onCreate() mantiene il thread dell'interfaccia utente, quindi la vista viene visualizzata direttamente dopo onCreate() e prima onResume(). :) – pgsandstrom