在Android上滚动ListView后自动隐藏键盘

时间:2014-05-28 05:01:39

标签: android android-listview scroll keyboard

我在Android上的新功能请在滚动listview之后帮我自动隐藏这里是我的代码但无法得到正确的解决方案

xml文件:

<ListView
    android:id="@+id/offline_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#dde1e3"
    android:clickable="true"
    android:focusable="true"
     >
</ListView>

代码:

        lvCustomList.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(!hasFocus)
                hideKeyboard(v);

        }

        private void hideKeyboard(View view) {
            // TODO Auto-generated method stub
            InputMethodManager inputMethodManger = (InputMethodManager)getSystemService(Activity
                    .INPUT_METHOD_SERVICE);
            inputMethodManger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    });

5 个答案:

答案 0 :(得分:31)

试试这个..

为什么不将OnTouchListener用于ListView,如下所示

lvCustomList.setOnTouchListener(new OnTouchListener() {
    @Override
        public boolean onTouch(View v, MotionEvent event) {

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

        return false;
    }
});

答案 1 :(得分:1)

最好使用/silent代替onScrollStateChanged并使用onScroll。因此,当用户真正滚动时,键盘将隐藏。

scrollState == 0

答案 2 :(得分:0)

Recyclerview,在滚动时隐藏键盘-SCROLL_STATE_DRAGGING(由@doubleA提及)

    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if(newState == RecyclerView.SCROLL_STATE_DRAGGING){
            InputMethodManager imm = (InputMethodManager) recyclerView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(recyclerView.getWindowToken(), 0);
        }
    }

答案 3 :(得分:0)

recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            //Hide keyboard code
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            //Find the currently focused view, so we can grab the correct window token from it.
            View view = activity.getCurrentFocus();
            //If no view currently has focus, create a new one, just so we can grab a window token from it
            if (view == null) {
                view = new View(activity);
            }
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    });

答案 4 :(得分:-1)

试试这个。

listview.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view,
                        int scrollState) { // TODO Auto-generated method stub

                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    InputMethodManager inputMethodManger = (InputMethodManager)getSystemService(Activity
                    .INPUT_METHOD_SERVICE);
            inputMethodManger.hideSoftInputFromWindow(view.getWindowToken(), 0);

                }
            });

希望它有所帮助。干杯!