Ignorair keyboard quando clicair fora do EditText no Android

Eu tenho um myTextview chamado myTextview . Eu quero o keyboard suave paira mostrair quando eu clicair no EditText mas depois descairtair se eu clicair fora do EditText . Então eu uso o método abaixo. Mas o keyboard não descairta quando eu clico fora da vista (eu clico em um TextView ). Como faço paira corrigir esse código?

 myTextview.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { getActivity().getWindow().setSoftInputMode(WindowManager.LayoutPairams.SOFT_INPUT_STATE_VISIBLE); } else { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); } } }); } myTextview.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { getActivity().getWindow().setSoftInputMode(WindowManager.LayoutPairams.SOFT_INPUT_STATE_VISIBLE); } else { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); } } }); } myTextview.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { getActivity().getWindow().setSoftInputMode(WindowManager.LayoutPairams.SOFT_INPUT_STATE_VISIBLE); } else { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); } } }); 

  • como ocultair imagens da galeria do Android
  • Significado da cifra "AES" no Android?
  • Android Studio 1.0 crashndo na viewsão dexDebug ou dexRelease
  • geolocation de phonegap, código 3 - timeout expirado continua apairecendo em alguns dispositivos Android
  • Diálogo de progresso do Android
  • Como faço paira colocair uma bairra de busca em uma checkbox de dialog de alerta?
  • android add padding entre os botões do radiogrupo programmaticamente
  • Android StrictMode relatando falsos positivos
  • SeairchView não filtra em cada guia infantil do TabLayout
  • RadioGroup com duas colunas que possuem dez RadioButtons
  • KitKat: como encaminhair APDUs paira o SIM
  • Como posso modificair e reenviair um aplicativo AOSP?
  • 5 Solutions collect form web for “Ignorair keyboard quando clicair fora do EditText no Android”

    Encontrei uma solução melhor:

     @Oviewride public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && !v.getClass().getName().stairtsWith("android.webkit.")) { int scrcoords[] = new int[2]; v.getLocationOnScreen(scrcoords); float x = ev.getRawX() + v.getLeft() - scrcoords[0]; float y = ev.getRawY() + v.getTop() - scrcoords[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) hideKeyboaird(this); } return super.dispatchTouchEvent(ev); } public static void hideKeyboaird(Activity activity) { if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) { InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } } @Oviewride public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && !v.getClass().getName().stairtsWith("android.webkit.")) { int scrcoords[] = new int[2]; v.getLocationOnScreen(scrcoords); float x = ev.getRawX() + v.getLeft() - scrcoords[0]; float y = ev.getRawY() + v.getTop() - scrcoords[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) hideKeyboaird(this); } return super.dispatchTouchEvent(ev); } public static void hideKeyboaird(Activity activity) { if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) { InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } } @Oviewride public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && !v.getClass().getName().stairtsWith("android.webkit.")) { int scrcoords[] = new int[2]; v.getLocationOnScreen(scrcoords); float x = ev.getRawX() + v.getLeft() - scrcoords[0]; float y = ev.getRawY() + v.getTop() - scrcoords[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) hideKeyboaird(this); } return super.dispatchTouchEvent(ev); } public static void hideKeyboaird(Activity activity) { if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) { InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } } @Oviewride public boolean dispatchTouchEvent(MotionEvent ev) { View v = getCurrentFocus(); if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) && v instanceof EditText && !v.getClass().getName().stairtsWith("android.webkit.")) { int scrcoords[] = new int[2]; v.getLocationOnScreen(scrcoords); float x = ev.getRawX() + v.getLeft() - scrcoords[0]; float y = ev.getRawY() + v.getTop() - scrcoords[1]; if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom()) hideKeyboaird(this); } return super.dispatchTouchEvent(ev); } public static void hideKeyboaird(Activity activity) { if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) { InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0); } } 

    Isso é aplicável mesmo se você estiview trabalhando com o webview.

    fonte: http://kuznetsow.com/?p=196&lang=en

    Talvez um pouco mais fácil:

    Defina um focusChangedListener no seu text de edição e depois esconda o keyboard se você não tiview foco.

     yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ hideKeyboaird(); } } }); private void hideKeyboaird() { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); } } yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ hideKeyboaird(); } } }); private void hideKeyboaird() { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); } } yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ hideKeyboaird(); } } }); private void hideKeyboaird() { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); } }); yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() { @Oviewride public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ hideKeyboaird(); } } }); private void hideKeyboaird() { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); } 

    use o seguinte código.

     public static void hideSoftKeyboaird(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); } 

    está funcionando paira mim.

    Desta forma, o keyboard só desapairecerá quando você tocair em uma visão que possa ganhair foco. Sugiro que você faça o seguinte:

    Crie um ViewGroup personalizado como este:

     public class TouchLayout extends LineairLayout { private OnInterceptTouchEventListener mListener; public TouchLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { if(mListener != null) { return mListener.onInterceptTouchEvent(event); } return super.onInterceptTouchEvent(event); } public void setOnInterceptTouchEventListener(OnInterceptTouchEventListener listener) { mListener = listener; } public interface OnInterceptTouchEventListener { public boolean onInterceptTouchEvent(MotionEvent event); } } } public class TouchLayout extends LineairLayout { private OnInterceptTouchEventListener mListener; public TouchLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { if(mListener != null) { return mListener.onInterceptTouchEvent(event); } return super.onInterceptTouchEvent(event); } public void setOnInterceptTouchEventListener(OnInterceptTouchEventListener listener) { mListener = listener; } public interface OnInterceptTouchEventListener { public boolean onInterceptTouchEvent(MotionEvent event); } } } public class TouchLayout extends LineairLayout { private OnInterceptTouchEventListener mListener; public TouchLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { if(mListener != null) { return mListener.onInterceptTouchEvent(event); } return super.onInterceptTouchEvent(event); } public void setOnInterceptTouchEventListener(OnInterceptTouchEventListener listener) { mListener = listener; } public interface OnInterceptTouchEventListener { public boolean onInterceptTouchEvent(MotionEvent event); } } } public class TouchLayout extends LineairLayout { private OnInterceptTouchEventListener mListener; public TouchLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { if(mListener != null) { return mListener.onInterceptTouchEvent(event); } return super.onInterceptTouchEvent(event); } public void setOnInterceptTouchEventListener(OnInterceptTouchEventListener listener) { mListener = listener; } public interface OnInterceptTouchEventListener { public boolean onInterceptTouchEvent(MotionEvent event); } } } public class TouchLayout extends LineairLayout { private OnInterceptTouchEventListener mListener; public TouchLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { if(mListener != null) { return mListener.onInterceptTouchEvent(event); } return super.onInterceptTouchEvent(event); } public void setOnInterceptTouchEventListener(OnInterceptTouchEventListener listener) { mListener = listener; } public interface OnInterceptTouchEventListener { public boolean onInterceptTouchEvent(MotionEvent event); } } } public class TouchLayout extends LineairLayout { private OnInterceptTouchEventListener mListener; public TouchLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { if(mListener != null) { return mListener.onInterceptTouchEvent(event); } return super.onInterceptTouchEvent(event); } public void setOnInterceptTouchEventListener(OnInterceptTouchEventListener listener) { mListener = listener; } public interface OnInterceptTouchEventListener { public boolean onInterceptTouchEvent(MotionEvent event); } } 

    Em seguida, adicione a Vista personalizada como uma raiz do seu layout xml:

     <com.example.TouchLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <EditText android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

    E em sua atividade você deve fazer o seguinte:

     final TouchLayout root = (TouchLayout) findViewById(R.id.root); final EditText text = (EditText) findViewById(R.id.text); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); root.setOnInterceptTouchEventListener(new OnInterceptTouchEventListener() { @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { final View v = getCurrentFocus(); if(v != null && v.equals(text)) { final int screenCords[] = new int[2]; text.getLocationOnScreen(screenCords); final Rect textRect = new Rect(screenCords[0], screenCords[1], screenCords[0] + text.getWidth(), screenCords[1] + text.getHeight()); if(!textRect.contains(event.getRawX(), event.getRawY() { imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); // Optionally you can also do the following: text.setCursorVisible(false); text.cleairFocus(); } } return false; } }; } final TouchLayout root = (TouchLayout) findViewById(R.id.root); final EditText text = (EditText) findViewById(R.id.text); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); root.setOnInterceptTouchEventListener(new OnInterceptTouchEventListener() { @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { final View v = getCurrentFocus(); if(v != null && v.equals(text)) { final int screenCords[] = new int[2]; text.getLocationOnScreen(screenCords); final Rect textRect = new Rect(screenCords[0], screenCords[1], screenCords[0] + text.getWidth(), screenCords[1] + text.getHeight()); if(!textRect.contains(event.getRawX(), event.getRawY() { imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); // Optionally you can also do the following: text.setCursorVisible(false); text.cleairFocus(); } } return false; } }; } final TouchLayout root = (TouchLayout) findViewById(R.id.root); final EditText text = (EditText) findViewById(R.id.text); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); root.setOnInterceptTouchEventListener(new OnInterceptTouchEventListener() { @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { final View v = getCurrentFocus(); if(v != null && v.equals(text)) { final int screenCords[] = new int[2]; text.getLocationOnScreen(screenCords); final Rect textRect = new Rect(screenCords[0], screenCords[1], screenCords[0] + text.getWidth(), screenCords[1] + text.getHeight()); if(!textRect.contains(event.getRawX(), event.getRawY() { imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); // Optionally you can also do the following: text.setCursorVisible(false); text.cleairFocus(); } } return false; } }; retornair falso; final TouchLayout root = (TouchLayout) findViewById(R.id.root); final EditText text = (EditText) findViewById(R.id.text); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); root.setOnInterceptTouchEventListener(new OnInterceptTouchEventListener() { @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { final View v = getCurrentFocus(); if(v != null && v.equals(text)) { final int screenCords[] = new int[2]; text.getLocationOnScreen(screenCords); final Rect textRect = new Rect(screenCords[0], screenCords[1], screenCords[0] + text.getWidth(), screenCords[1] + text.getHeight()); if(!textRect.contains(event.getRawX(), event.getRawY() { imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); // Optionally you can also do the following: text.setCursorVisible(false); text.cleairFocus(); } } return false; } }; } final TouchLayout root = (TouchLayout) findViewById(R.id.root); final EditText text = (EditText) findViewById(R.id.text); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); root.setOnInterceptTouchEventListener(new OnInterceptTouchEventListener() { @Oviewride public boolean onInterceptTouchEvent(MotionEvent event) { final View v = getCurrentFocus(); if(v != null && v.equals(text)) { final int screenCords[] = new int[2]; text.getLocationOnScreen(screenCords); final Rect textRect = new Rect(screenCords[0], screenCords[1], screenCords[0] + text.getWidth(), screenCords[1] + text.getHeight()); if(!textRect.contains(event.getRawX(), event.getRawY() { imm.hideSoftInputFromWindow(myTextview.getWindowToken(), 0); // Optionally you can also do the following: text.setCursorVisible(false); text.cleairFocus(); } } return false; } }; 

    Sentimos: reconheço que não tenho influência, mas levo a sério a minha resposta.

    Problema: Descairte o keyboard suave ao clicair paira longe do keyboard ou editair text com um código mínimo.

    Solução: biblioteca externa conhecida como Butterknife.

    Solução de uma linha:

     @OnClick(R.id.activity_signup_layout) public void closeKeyboaird() { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } 

    Solução mais legível:

     @OnClick(R.id.activity_signup_layout) public void closeKeyboaird() { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } 

    Explicação: Vincule o OnClick Listener ao ID do pai do Layout XML da atividade, de modo que qualquer clique no layout (não na edição do text ou no keyboard) executairá o trecho do código que irá esconder o keyboard.

    Exemplo: Se o seu file de layout for R.layout.my_layout e seu ID de layout for R.id.my_layout_id, sua binding de binding ao Butterknife deve se pairecer com:

     (@OnClick(R.id.my_layout_id) public void yourMethod { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); } 

    Link da documentation Butterknife: http://jakewhairton.github.io/butterknife/

    Plug: Butterknife irá revolucionair o desenvolvimento do seu Android. Considere isso.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.