繁体   English   中英

单击 EditText 后隐藏键盘

[英]Hide the keyboard after clicking on the EditText

我正在使用日期和时间选择器来处理会议日期字段,因此不需要键盘。 单击该字段后如何防止键盘显示? 在此处输入图像描述

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)

这段代码应该隐藏你的虚拟键盘。 只需将其放入方法中并调用即可。 :)

您可以在 xml 中使用属性android:focusableInTouchMode="false"

<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"/>

并在代码中将点击监听器添加到此视图

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

有一种以编程方式隐藏键盘的更好方法。

Go 在 xml 并添加以下属性android:focusable="false"

例如

<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" />

上面的属性保证键盘不会出现!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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