[英]Login Form: Keyboard hides on button click, but does not show again after click on edittext
我有一個帶有編輯文本和登錄按鈕(連接)的登錄表單。 當用戶單擊編輯文本時,鍵盤會打開,當他們單擊登錄按鈕(連接)時,鍵盤會消失。 問題是如果用戶在單擊登錄按鈕后單擊編輯文本,鍵盤會顯示一秒鍾,然后消失。 使用 rootview 計算鍵盤是否顯示,因為根據 stackoverflow,這似乎是最簡單的方法。 我如何制作它以便在單擊按鈕后可以顯示鍵盤?
點擊監聽器
boolean clicked=false;
connect.setOnClickListener(v -> {
clicked=true;
keyboard();
});
鍵盤法
void keyboard(){
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
final View activityRootView = findViewById(R.id.wrap);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
if (clicked) {
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 0.25 * activityRootView.getRootView().getHeight()) {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
});
}
每次布局更新時都會調用您的 onGlobalLayout 偵聽器,如果單擊該按鈕后每次布局更新后都會導致鍵盤關閉
if (heightDiff > 0.25 * activityRootView.getRootView().getHeight())
您可以直接在 ButtonClickListener 中關閉鍵盤
connect.setOnClickListener(v -> {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.