簡體   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