2013-03-14 10 views
27

Quando abbiamo uno EditText e perde lo stato attivo (per un elemento che non ha bisogno di una tastiera), la tastiera morbida dovrebbe nascondersi automaticamente o dovremmo nasconderlo da soli?Nascondi tastiera virtuale in caso di perdita di messa a fuoco

mi sto muovendo l'attenzione da un AutoCompleteSearchView (che dovrebbe comportarsi come un EditText credo) ad un Button, requestFocus() rendimenti veri, ma la tastiera non si nasconde.

+0

Si dovrebbe nascondere automaticamente. Hai provato a farlo? –

+4

Ho, è per questo che sto chiedendo. – FWeigl

+0

@ Ascorbin Non scherzare, Android si prenderà cura di questo .. – Pragnani

risposta

53

Il modo migliore è quello di impostare un OnFocusChangeListener per l'EditText, e quindi aggiungere il codice alla tastiera nel metodo OnFocusChange di chi ascolta. Android chiuderà automaticamente la tastiera quando EditText perde lo stato attivo.

Qualcosa di simile nel metodo OnCreate:

EditText editText = (EditText) findViewById(R.id.textbox); 
OnFocusChangeListener ofcListener = new MyFocusChangeListener(); 
editText.setOnFocusChangeListener(ofcListener); 

e quindi aggiungere la classe:

private class MyFocusChangeListener implements OnFocusChangeListener { 

    public void onFocusChange(View v, boolean hasFocus){ 

     if(v.getId() == R.id.textbox && !hasFocus) { 

      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 

     } 
    } 
} 
+10

Quindi deve essere fatto per ogni componente editText sull'applicazione? non ci sono impostazioni globali per definire questo comportamento? Se no e ci sono molti editTexts nell'app, suggerisco di estendere la classe editText, aggiungere il listener alla classe estesa e usare la classe modificata lungo l'applicazione – GyRo

+4

come farla automaticamente? Se ho moduli da 150 a 25, non voglio farlo ogni volta ... Questa funzione dovrebbe essere ovvia. – Loenix

4

Android non nasconde la tastiera per te. Se si desidera che la tastiera per nascondere quando il EditText perde lo stato attivo, provare a utilizzare un metodo come questo su quell'evento:

private void hideKeypad() { 
    EditText edtView = (EditText) findViewById(R.id.e_id); 

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(edtView.getWindowToken(), 0); 
} 
+1

ottima risposta, grazie mille –

2

Prova questa, può essere che possa risolvere il problema.

private void hideKeyboard() { 
    InputMethodManager mImMan = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    mImMan.hideSoftInputFromWindow(mYourEdttxtName.getWindowToken(), 0); 
} 

È possibile trovare maggiori informazioni da here.

5

Prova questa

/** 
* Hide keyboard on touch of UI 
*/ 
public void hideKeyboard(View view) { 

    if (view instanceof ViewGroup) { 

     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 

      View innerView = ((ViewGroup) view).getChildAt(i); 

      hideKeyboard(innerView); 
     } 
    } 
    if (!(view instanceof EditText)) { 

     view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       hideSoftKeyboard(v); 
       return false; 
      } 

     }); 
    } 

} 

/** 
* Hide keyboard while focus is moved 
*/ 
public void hideSoftKeyboard(View view) { 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) contentsContext_ 
       .getSystemService(Context.INPUT_METHOD_SERVICE); 
     if (inputManager != null) { 
      if (android.os.Build.VERSION.SDK_INT < 11) { 
       inputManager.hideSoftInputFromWindow(view.getWindowToken(), 
         0); 
      } else { 
       if (this.getCurrentFocus() != null) { 
        inputManager.hideSoftInputFromWindow(this 
          .getCurrentFocus().getWindowToken(), 
          InputMethodManager.HIDE_NOT_ALWAYS); 
       } 
       view.clearFocus(); 
      } 
      view.clearFocus(); 
     } 
    } 
} 
0

è possibile ignorare il metodo dispatchTouchEvent per realizzarla :

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 

     /** 
     * It gets into the above IF-BLOCK if anywhere the screen is touched. 
     */ 

     View v = getCurrentFocus(); 
     if (v instanceof EditText) { 


      /** 
      * Now, it gets into the above IF-BLOCK if an EditText is already in focus, and you tap somewhere else 
      * to take the focus away from that particular EditText. It could have 2 cases after tapping: 
      * 1. No EditText has focus 
      * 2. Focus is just shifted to the other EditText 
      */ 

      Rect outRect = new Rect(); 
      v.getGlobalVisibleRect(outRect); 
      if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) { 
       v.clearFocus(); 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      } 
     } 
    } 
    return super.dispatchTouchEvent(event); 
} 

Bonus: In caso di guadagnare attenzione EditText, l'ordine degli eventi innescata è:

  1. onFocusChange() di un altro EditText è chiamato (se quell'altro EditText sta perdendo fuoco)
  2. ACTION_DOWN è chiamato
  3. Infine, verrà chiamato il metodo onFocusChange() di EditText.
0

il mio problema risolto con questo codice (nel frammento)

LinearLayout linearLayoutApply=(LinearLayout)rootView.findViewById(id.LinearLayoutApply); 

    linearLayoutApply.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus) 
      { 
       hideKeyBoard(v); 
      } 
     } 
    }); 

hideKeyBoard

public void hideKeyBoard(View v) 
{ 
    InputMethodManager imm=(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); 
} 
0

soluzione a questo problema è già stato trovato here.
Sta utilizzando DispatchTouchEvent sull'attività e non associa tutti gli EditText all'evento FocusChange o Touch.
È la soluzione migliore.

implementazione

mio Xamarin è la seguente:

public override bool DispatchTouchEvent(MotionEvent ev) 
    { 
     if (ev.Action == MotionEventActions.Down) 
     { 
      var text = CurrentFocus as EditText; 
      if (text != null) 
      { 
       var outRect = new Rect(); 
       text.GetGlobalVisibleRect(outRect); 
       if (outRect.Contains((int) ev.RawX, (int) ev.RawY)) return base.DispatchTouchEvent(ev); 
       text.ClearFocus(); 
       HideSoftKeyboard(); 
      } 
     } 
     return base.DispatchTouchEvent(ev); 
    } 

protected void HideSoftKeyboard() 
    { 
     var inputMethodManager = (InputMethodManager) GetSystemService(InputMethodService); 
     inputMethodManager.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0); 
    } 
1

Basta creare un metodo statico

public static void touchScreenAndHideKeyboardOnFocus(View view, final Activity activity) { 

    if (view instanceof EditText) { 
     view.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (!hasFocus) { 
        if(activity != null) { 
         InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
         if (activity.getCurrentFocus() != null) { 
          inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
         } 
        } 
       } 
      } 
     }); 
    } 

    if (view instanceof ViewGroup) { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
      View innerView = ((ViewGroup) view).getChildAt(i); 
      touchScreenAndHideKeyboardOnFocus(innerView, activity); 
     } 
    } 
} 

vista è una vista principale per il layout .. ma attenzione, se si dispone di un altro fuoco ascoltatore nel tuo editorext ..

+0

cos'è "forzatamente"? – Hatim

+0

@Hatim upss sorry .. è il mio metodo originale .. –

Problemi correlati