Pregunta

Just wondering how to get the soft keyboard to go away right after the app collects input from an EditText. Right now, the "Save" button collects the text and successfully sets the string in the EditText back to blank, but the soft keyboard doesn't go away until the orientation switches. I tried programmatically hiding the soft keyboard, but it's not working.

Here's the Save button code:

    private OnClickListener saveButtonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (newListEditText.getText().length() > 0) {  //Make sure the user actually wrote something
            addStore(newListEditText.getText().toString());  //Adds a button to view with name from EditText
            newListEditText.setText("");
            ((InputMethodManager) getSystemService(
                       Context.INPUT_METHOD_SERVICE)).hideSoftInputFromInputMethod(
                            newListEditText.getWindowToken(), 0);  //Supposed to hide soft keyboard but doesn't do it

I also tried inserting the code [android:imeOptions="actionDone"] into the xml layout file, as was suggested in an answer to a similar question, but nothing changed.

Thanks in advance for your input!

¿Fue útil?

Solución

Just replace this:

Context.INPUT_METHOD_SERVICE)).hideSoftInputFromInputMethod(  

with this:

Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(

Otros consejos

Use this code to do so..

InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);

You can ignore and place "null' in place of url.getWindowToken().

FYI - In my case/code, I've used url as the string received through the editText like for e.g.

inputText = (EditText) findViewById (R.id.edittext1); 
String url = inputText.getText().toString();

Try this code, so whenever user touch on other part pf screen other then edittext, soft keyboard will disappear.

So in your case if user is done with text and try to touch button, this will work.

    @Override
public boolean dispatchTouchEvent(MotionEvent event) {

    View v = getCurrentFocus();
    boolean ret = super.dispatchTouchEvent(event);

    if (v instanceof EditText) {
        View w = getCurrentFocus();
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

         Log.d("Activity",
         "Touch event " + event.getRawX() + "," + event.getRawY()
         + " " + x + "," + y + " rect " + w.getLeft() + ","
         + w.getTop() + "," + w.getRight() + ","
         + w.getBottom() + " coords " + scrcoords[0] + ","
         + scrcoords[1]);
        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
                        .getBottom())) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                    .getWindowToken(), 0);
        }
    }
    return ret;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top