簡體   English   中英

從recyclerview中選擇一個項目后如何隱藏軟鍵盤?

[英]How to hide soft keyboard after selecting an item from recyclerview?

我有一個帶有recyclerview的edittext,其中搜索時將顯示一個位置列表。單擊列表中的任何項目時,鍵盤應隱藏。 請幫忙

使用以下代碼將鍵盤隱藏在Item的onClick()中。

View view = this.getCurrentFocus();
if (view != null) {  
       InputMethodManager imm = 
       (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
       imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

在搜索列表中單擊時調用此方法

void hideKeybord() {
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

解決方案:只需在您的RecyclerView設置onTouchListener()並隱藏鍵盤即可:

yourRecycleView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        input.hideSoftInputFromWindow(v.getWindowToken(), 0);

        return false;
    }
});

希望這會派上用場。

試試這個-在回收站ViewHolder的onClick中調用此方法

public void hideKeyboard(){
                edittext.clearFocus()
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)
                if (imm != null) {
                    imm.hideSoftInputFromWindow(edittext.windowToken, 0)
                }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM