How to hide the keyboard when clicking an Edittext?

Asked

Viewed 1,054 times

1

I have following XML:

        <android.support.design.widget.TextInputLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_weight="6"
            android:maxLength="10"
            android:textColorHint="#FFF8E1"
            android:theme="@style/StyledTilEditText">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/edt_DtNasc"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:clickable="true"
                android:fontFamily="sans-serif-condensed"
                android:hint="Data Nascimento"
                android:text="dd/MM/AAAA"
                android:inputType="date"
                android:maxLength="10"
                android:maxLines="1"
                android:singleLine="true"
                android:textColor="#D3D3D3"
                android:textSize="13dp" />

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

I would like, when clicking on this item, the keyboard to hide or the dateTimePicker to appear, but to do so, I have to click 2x on the item. I made the following code, but it didn’t work:

   edt_dtnasc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edt_dtnasc.setInputType(InputType.TYPE_NULL);
            hideSoftKeyboard(getActivity());
            dateTimePicker();
        }
    });

hideSoftKeyboard:

public void hideSoftKeyboard(Activity activity) {
    try {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View currentFocus = activity.getCurrentFocus();
        if (currentFocus != null) {
            inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Datetime:

private void dateTimePicker() {
    // Get Current Date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    date_time = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
                    edt_dtnasc.setText(date_time);
                }
            }, mYear, mMonth, mDay);
    datePickerDialog.show();

}

2 answers

2


Use the method setOnTouchListener may be a solution. See:

edt_dtnasc.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        hideSoftKeyboard(MainTest.this);
        int inType = edt_dtnasc.getInputType(); // guarda informação do inpu type 
        edt_dtnasc.setInputType(InputType.TYPE_NULL); // desabilita o soft input
        edt_dtnasc.onTouchEvent(event); // chama handler nativo
        edt_dtnasc.setInputType(inType); //restaura o input type

        // aqui você chama o DatePickerDialog

        return true;
    }
});
  • 1

    excellent!!!!!!!!

0

I did it in my project:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);//metodo para fechar teclado virtual

Browser other questions tagged

You are not signed in. Login or sign up in order to post.