简体   繁体   中英

Hide the keyboard after clicking on the EditText

I'm using Date and Time Picker to handle the meeting date field, so there is no need for the keyboard. How can I prevent the keyboard from showing after clicking on the field? enter image description here

val activity: Activity = this //if you are in the Activity
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            var view = activity.currentFocus
            if (view == null) {
                view = View(activity)
            }
            imm.hideSoftInputFromWindow(view.windowToken, 0)

this piece of code should hide your virtual keyboard. Just put it in a method and call. :)

You can use property android:focusableInTouchMode="false" in xml

<com.google.android.material.textfield.TextInputEditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false"
    android:inputType="text|date"/>

And add click listener to this view in code

editText.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
       //show date picker
    }
});

There is a better way of programatically hiding the keyboard.

Go in xml and add the following attribute android:focusable="false"

Eg

<EditText
    android:id="@+id/time_date_et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/prompt_light"
    android:focusable="false"
    android:textSize="17sp" />

The above attribute ensures that the keyboard won't appear!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM