Hide keyboard when launching activity with EditText and show it again

1

Using the line TuEditText.setInputType(InputType.TYPE_NULL); I managed to hide the EdiText keyboard.

However, I have put TuEditText.setInputType(InputType.TYPE_CLASS_TEXT); in the OnClickListener of EditText but it still does not exit the keyboard.

How can I make it appear when clicking but not when launching the activity?

    
asked by pepito 26.07.2017 в 11:36
source

2 answers

4

To avoid being shown the keyboard when entering the activity, you do not have to do it as you put it, you have to remove it.

To avoid showing it, you have to do it in the Manifest. You have to include the following lines in the activity in which you want to hide the keyboard:

android:configChanges="screenSize|orientation"
android:windowSoftInputMode="stateHidden"

When you enter the activity, it will not show it to you but when you select the EditText if.

To indicate if you want to only be able to put text I would do it in the definition of the layout, in the area where you define the Edittext you have to put this:

android:inputType="text"

If you want to put another type of entry you can see here those that are: link

I hope it serves you.

    
answered by 26.07.2017 / 11:56
source
1

You have several ways to solve this

-In your AndroidManifest.xml : You can hide the keyboard when you enter the activity using the windowSoftInputMode property that you can find in the documentation . This configuration hides the virtual keyboard when the user opens a new Activity. The keyboard will be displayed only when the user clicks on the EditText.

Example:

<activity android:name="com.tu.paquete.Activity"
          android:windowSoftInputMode="stateHidden"  />

-With the InputMethodManager : if you want to make sure that the input method is visible / hidden in any part of the life cycle of your activity you can use InputMethodManager to display it, as indicated by < a href="https://developer.android.com/training/keyboard-input/visibility.html#ShowOnDemand"> documentation .

Example:

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}

Code extracted from the following response from SOEn and the documentation

    
answered by 26.07.2017 в 12:05