![](/img/trans.png)
[英]How to hide soft keyboard on android after clicking outside EditText? in fragment
[英]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.