2012-01-10 11 views
9

Tengo EditText en mi Activity. Tan pronto como se inicie el Activity forzaré el teclado suave para que se abra utilizando.Android: ¿Cómo fuerzo al teclado virtual a cerrarse cuando se ha forzado a abrirlo?

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
if (imm != null) { 
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
} 

Ahora, si el teclado suave está abierto y presiono el botón de inicio, permanece abierto. ¿Cómo puedo forzarlo en la prensa local?

+0

ver este enlace http://stackoverflow.com/questions/7200281/programatically-hide-show-android-soft-keyboard –

Respuesta

20
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
mgr.hideSoftInputFromWindow(Your Button.getWindowToken(), 0); 
1
@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    View view = getCurrentFocus(); 
    boolean ret = super.dispatchTouchEvent(event); 

    if (view 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; 
} 

Este código se cierra el teclado al tocar cualquier parte de la pantalla.

+0

@ rashmi La solución que ha sugerido es cerrar el teclado virtual cuando se lo llama implícitamente. aquí lo obligo a abrir cuando se abre mi actividad imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, 0); Entonces, esta solución no funciona – Vibhuti

Cuestiones relacionadas