How to hide keyboard when exiting EditText? [closed]

0

I have three edittext, I want it when I click out of them or finish filling my keyboard sum. Being that they are a fragment. How to do this?

My XML with the editText.

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:paddingBottom="10dp"
        android:background="@drawable/shadow_grey"
        android:id="@+id/cadastroAvaliador"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <AutoCompleteTextView
        android:id="@+id/colaboradoresAmc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:hint="Avaliador">

        </AutoCompleteTextView>


        <AutoCompleteTextView
            android:id="@+id/contratadasAmc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Contratada"
            android:layout_below="@+id/colaboradoresAmc"
            android:layout_alignStart="@+id/colaboradoresAmc">
        </AutoCompleteTextView>


        <EditText
            android:id="@+id/dataRealizada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/contratadasAmc"
            android:layout_alignStart="@+id/contratadasAmc"
            android:ems="6"
            android:inputType="date"
            android:hint="Data Realizada"
            android:clickable="true"
            android:focusableInTouchMode="true" />

        <Spinner
            android:id="@+id/spinnerTipo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/dataRealizada"
            android:layout_alignTop="@+id/dataRealizada"
            android:layout_alignBottom="@+id/dataRealizada"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/contentTipo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/cadastroAvaliador"
        android:layout_alignEnd="@+id/cadastroAvaliador"
        android:layout_alignStart="@+id/cadastroAvaliador">

    </RelativeLayout>

</RelativeLayout>

When I clicked the marked / scraped area, I wanted the keyboard to hide.

Thank you in advance.

    
asked by anonymous 09.06.2017 / 19:22

1 answer

3

If you want to hide the keyboard when you click in that area, set an OnClickListener to it and use the InputMethodManager to hide the keyboard.

Put in onCreateView method after "inflating" the layout.

    RelativeLayout contentTipo = (RelativeLayout) view.findViewById(R.id.contentTipo);
    contentTipo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hideSoftKeyboard();
        }
    }); 

Replace view with the name of the variable where you have "inflated" the layout of the fragment.

Method hideSoftKeyboard() :

private void hideSoftKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getView().getWindowToken(),
                                                   InputMethodManager.HIDE_NOT_ALWAYS);
}
    
12.06.2017 / 16:30