How to hide the keyboard when clicking on an EditText?

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, by clicking on this item, the keyboard would hide or the dateTimePicker would appear, but for this, I have to click 2x on the item. I made the following code, but it did not 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();

}
    
asked by anonymous 26.05.2017 / 20:14

2 answers

2

Using the setOnTouchListener method can 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;
    }
});
    
26.05.2017 / 20:42
0

I did this in my project:

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