Ocultar fragmento en el interior del teclado cuando entra en el botón

Kawazaki:

Estoy dentro de un fragmento y cuando hago clic en la tecla Intro (en el teclado), quiero ocultar el teclado. He intentado esto pero no funciona

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cursorVisible="false"
    />


  edittext.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                getActivity().getWindow().setSoftInputMode(
                        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
Jakob:

En primer lugar crear un hideSoftKeyboard()vacío.

public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

A continuación, establezca OnEditorActionListenerla edittexty llamada hideSoftKeyboard().

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))){
                hideSoftKeyboard();
                return true;
            }
            else{
                return false;
            }
        }
    });

Por último añadimos algunos atributos XML al edittext.

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/edittext"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:imeOptions="actionDone"
    android:singleLine="true"
    android:lines="1"
    android:inputType="text"/>

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=235287&siteId=1
Recomendado
Clasificación