在我的布局中,我有一个editText
和一个button
。
当editText
获得或失去焦点时,键盘显示或隐藏的内容。为此,我在editText
中添加了一个OnFocusChangeListener
,并在查看hasFocus
标志时隐藏或显示键盘。
问题是,当editText
有焦点,并且如果我触摸按钮,键盘是可见的,focusChangeListener
被调用,我隐藏键盘,但button
onClickListener
不被调用。隐藏键盘后,我必须再次单击该按钮。
相反,如果在editText失去焦点时出现延迟(postDelayed({ hideKeyboard() }, 500)
),则会调用onClickListener
。
添加延迟看起来像是一个hack。我怎样才能在隐藏键盘的同时仍然收到按钮的点击?
发布于 2019-11-06 11:32:28
使用此方法单击按钮时手动隐藏键盘,并移除焦点侦听器
public void hideKeyboard(View view) {
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
要在屏幕启动时关注EditText
,可以在xml中使用<requestFocus/>
https://stackoverflow.com/questions/58728621
复制相似问题