在“循环器视图”中的“编辑文本”滚动到屏幕外时隐藏键盘

时间:2017-01-22 16:00:55

标签: android android-edittext android-recyclerview

我有一个包含RecyclerView个子元素的EditText。我想在屏幕上滚动选定的EditText时隐藏软键盘。如何判断EditText何时不在屏幕上?是否有一些事件监听器可以附加到EditText元素来告诉?

5 个答案:

答案 0 :(得分:19)

像这样实施onTouchListener

yourRecycleView.setOnTouchListener(new OnTouchListener() {

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

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

        return false;
    }
});

答案 1 :(得分:0)

yourRecycleView.setOnTouchListener(new OnTouchListener() {

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

            edittext.clearFocus(); //hidden keyboard 
            return false;
      }
});

答案 2 :(得分:0)

我的解决方案:

editText.setOnFocusChangeListener((v, hasFocus) -> {
            Handler handler = new Handler();
            if (!hasFocus) {
                handler.postDelayed(() -> {
                    if (!editText.hasFocus()) {
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(0, 0);
                    }
                }, 200);

            }
        });

答案 3 :(得分:0)

这对我有用:我使用RecyclerView.OnScrollListenerPublishRelay来消除事件。

class RecyclerViewActivity : Activity(){
    ...
    private val scrollableRelay = PublishRelay.create<Unit>()
    private val disposable = CompositeDisposable()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                scrollableRelay.accept(Unit)
            }
        })
        scrollableRelay
            .debounce(100, TimeUnit.MILLISECONDS)
            .subscribe({
                if (currentFocus == recyclerView) {
                    hideKeyboard()
                }
             })
            .addTo(disposable)
    }

    override fun onDestroy() {
        disposable.onDestroy()
    }
}

一旦视图从屏幕上滚动,焦点就会上升到recyclerView。 因此,我们可以使用RecyclerView.OnScrollListener来实现此功能。 onScrolled甚至跟踪视图的最细微滚动。这就是为什么我们需要添加debounce来避免收到很多事件的原因。

hideKeyboardaddTo是扩展功能:

fun Disposable.addTo(compositeDisposable: CompositeDisposable) {
    compositeDisposable.add(this)
}

fun Activity.hideKeyboard() =
    currentFocus?.let {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(it.windowToken, 0)
    }

答案 4 :(得分:0)

在以下情况下解散键盘的Kotlin解决方案:

  • 聚焦的ViewRecyclerView滚动下来
  • 或者如果用户按下 Enter 键,并且操作系统无法找到RecyclerView ...
  • 内部要关注的内容
// dismiss the keyboard when it is no longer focusing on anything inside the recyclerview
recyclerView.viewTreeObserver.addOnGlobalFocusChangeListener { _, newFocus: View? ->
    if (newFocus == recyclerView || recyclerView !in (newFocus?.ancestors ?: emptySequence())) {
        recyclerView.context.inputService.hideSoftInputFromWindow(recyclerView.windowToken, 0)
    }
}

val Context.inputService get() = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

val View.ancestors: Sequence<View> get() = generateSequence(this) { it.parent as? View }