0

Я хочу, чтобы мягкая клавиатура исчезла, когда пользователь коснулся ничего, кроме EditText на экране. Я уже прошел через SO-сообщения по этому вопросу и использовал использование onFocusChangeListener, и он сделал трюк в других проектах, над которыми я работал, но теперь он не работает, и я не могу понять, что не так. Любая помощь приветствуется.Скрытие мягкой клавиатуры, не работающей с onFocusChangeListener на Android

Мой activity.java (inputGuardian1 является примером EditText):

inputGuardian1.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View view, boolean hasFocus) { 
      if (!hasFocus) { 
       InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       mgr.hideSoftInputFromWindow(inputGuardian1.getWindowToken(), 0); 
      } 
     } 
    }); 

Layout XML файл:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              xmlns:app="http://schemas.android.com/apk/res-auto" 
              android:layout_width="match_parent" 
              android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 


<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" > 
     <!--android:layout_marginTop="?attr/actionBarSize"--> 


    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp"> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_guardian1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:id="@+id/inputGuardian1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:imeOptions="actionDone" 
       android:hint="Guardian 1" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_Guardian2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:id="@+id/inputGuardian2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:imeOptions="actionDone" 
       android:hint="Guardian 2" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
     android:id="@+id/input_layout_Guardian3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/inputGuardian3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:imeOptions="actionDone" 
      android:hint="Guardian 3" /> 
    </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_Guardian4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:id="@+id/inputGuardian4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:imeOptions="actionDone" 
       android:hint="Guardian 4" /> 
     </android.support.design.widget.TextInputLayout> 
     </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingTop="40sp" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp"> 

     <android.support.design.widget.TextInputLayout 
      android:id="@+id/input_layout_ChildName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <EditText 
       android:id="@+id/inputChildName" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:hint="Child's Name" 
       android:layout_marginTop="30sp" 
       android:imeOptions="actionDone" 
       /> 

     </android.support.design.widget.TextInputLayout> 



    </LinearLayout> 

     <Button android:id="@+id/buttonNext" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Next" 
       android:background="@color/colorPrimary" 
       android:layout_marginTop="40dp" 
       android:textColor="@android:color/white" /> 

    </LinearLayout> 
</ScrollView> 

+1

ссылка http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext – Nikhil

+0

Спасибо. Я решил проблему. Мне пришлось добавить два атрибута ко всем контейнерам: 'android: clickable =" true "и' android: focusableInTouchMode = "true" ' – skbrhmn

ответ

0

Установка android:clickable="true" и android:focusableInTouchMode="true" всех контейнеров решена проблема.

Смежные вопросы