滑动列表时隐藏软键盘

5

以下是xml代码:

enter image description here

我想要实现这样一个功能:当我点击edittext时,软键盘会显示出来。当我滚动listview(不是滚动到OnScrollListener.SCROLL_STATE_IDLE状态)时,软键盘会隐藏。

我使用了android:windowSoftInputMode="adjustResize"属性。


请尝试使用:android:windowSoftInputMode="adjustPan" - krunal patel
使用 adjustPan,标题栏将会超出屏幕。@krunal patel - Cruisehu
在ListView的滚动事件中编写以下代码: InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); - krunal patel
2个回答

11

使用这个链接检测滚动, 它实现了onScrollListener,你需要将其设置到你的ListView中,在它的onScrollStateChanged()方法中放入以下代码 -

setOnScrollListener(new OnScrollListener(){
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      // TODO Auto-generated method stub
    }
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState !=0){
           InputMethodManager inputMethodManager = (InputMethodManager) 
           getSystemService(Activity.INPUT_METHOD_SERVICE);     
           inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(‌​), 0);
        }
    }
});

这正是我正在寻找的。谢谢。 - Cruisehu
我一直遇到这个问题。然后我重新启动了Android Studio并重新打开了我的项目,所有的问题都得到了解决。Android Studio有时候会出现一些错误。 - CHarris
需要API 23或更高版本。 - Yaroslav Dukal

1
InputMethodManager inputMethodManager = (InputMethodManager) 
           getSystemService(Activity.INPUT_METHOD_SERVICE);     
           inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(‌​), 0);

在AS中存在一个bug...

在onScrollStateChange中使用这个替代方法。

InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(absListView.getApplicationWindowToken(), 0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接