![](/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.