Come posso chiudere la tastiera quando si preme un pulsante?
Come posso chiudere la tastiera quando si preme un pulsante?
Risposte:
Vuoi disabilitare o eliminare una tastiera virtuale?
Se vuoi semplicemente scartarlo, puoi usare le seguenti righe di codice nel tuo pulsante quando fai clic su Evento
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity()) getCurrentFocus().getWindowToken()per il primo argomento hideSoftInputFromWindow(). Inoltre, fallo nel onPause()e non nel onStop()caso tu stia cercando di farlo sparire quando cambi attività.
La soluzione sopra non funziona per tutti i dispositivi e inoltre utilizza EditText come parametro. Questa è la mia soluzione, basta chiamare questo semplice metodo:
private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if(imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
isAcceptingText()ha reso questa risposta migliore di altre
Questa è la mia soluzione
public static void hideKeyboard(Activity activity) {
View v = activity.getWindow().getCurrentFocus();
if (v != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
Ecco una soluzione Kotlin (mescolando le varie risposte nel thread)
Creare una funzione di estensione (forse in una classe ViewHelpers comune)
fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}
Quindi consuma semplicemente usando:
// from activity
this.dismissKeyboard()
// from fragment
activity.dismissKeyboard()
La prima soluzione con InputMethodManager ha funzionato come un campione per me, il metodo getWindow (). SetSoftInputMode non su Android 4.0.3 HTC Amaze.
@Ethan Allen, non avevo bisogno di rendere definitivo il testo di modifica. Forse stai usando una classe interna EditText che hai dichiarato il metodo di contenimento? È possibile rendere EditText una variabile di classe dell'attività. O semplicemente dichiarare un nuovo EditText all'interno della classe / metodo interno e utilizzare di nuovo findViewById (). Inoltre, non ho trovato che avevo bisogno di sapere quale EditText nel modulo era attivo. Potrei semplicemente sceglierne uno arbitrariamente e usarlo. Così:
EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
public static void hideSoftInput(Activity activity) {
try {
if (activity == null || activity.isFinishing()) return;
Window window = activity.getWindow();
if (window == null) return;
View view = window.getCurrentFocus();
//give decorView a chance
if (view == null) view = window.getDecorView();
if (view == null) return;
InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null || !imm.isActive()) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Throwable e) {
e.printStackTrace();
}
}
Questa soluzione si assicura che nasconda anche la tastiera non fa nulla se non viene aperta. Utilizza l'estensione in modo che possa essere utilizzato da qualsiasi classe di proprietario del contesto.
fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}
Utilizzando il contesto della vista, possiamo ottenere il risultato desiderato con i seguenti metodi di estensione in Kotlin:
/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}
return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)
Una volta che questi sono a posto, basta chiamare:
editTextFoo.dismiss()