我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
editText.post(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
其他回答
以下是我从Square得到的一个更可靠的解决方案:
fun View.focusAndShowKeyboard() { /** * This is to be called when the window already has focus. */ fun View.showTheKeyboardNow() { if (isFocused) { post { // We still post the call, just in case we are being notified of the windows focus // but InputMethodManager didn't get properly setup yet. val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) } } } requestFocus() if (hasWindowFocus()) { // No need to wait for the window to get focus. showTheKeyboardNow() } else { // We need to wait until the window gets focus. viewTreeObserver.addOnWindowFocusChangeListener( object : ViewTreeObserver.OnWindowFocusChangeListener { override fun onWindowFocusChanged(hasFocus: Boolean) { // This notification will arrive just before the InputMethodManager gets set up. if (hasFocus) { this@focusAndShowKeyboard.showTheKeyboardNow() // It’s very important to remove this listener once we are done. viewTreeObserver.removeOnWindowFocusChangeListener(this) } } }) } }
代码从这里开始。
我使用这个扩展函数来显示Kotlin的键盘。
fun EditText.requestFocusWithKeyboard() {
post {
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0))
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0))
setSelection(length())
}
}
你可以把它叫做:
editText.requestFocusWithKeyboard()
除了非常方便之外:
您不必担心在所有可能的情况下关闭它,例如当您使用InputMethodManager.SHOW_FORCED导航返回时。 除了请求焦点,它还设置选择(光标到文本末尾)。
您还可以创建EditText的自定义扩展,该扩展在接收到焦点时知道打开软键盘。这就是我最后要做的。以下是对我有效的方法:
public class WellBehavedEditText extends EditText {
private InputMethodManager inputMethodManager;
private boolean showKeyboard = false;
public WellBehavedEditText(Context context) {
super(context);
this.initializeWellBehavedEditText(context);
}
public WellBehavedEditText(Context context, AttributeSet attributes) {
super(context, attributes);
this.initializeWellBehavedEditText(context);
}
public WellBehavedEditText(Context context, AttributeSet attributes, int defStyleAttr) {
super(context, attributes, defStyleAttr);
this.initializeWellBehavedEditText(context);
}
public WellBehavedEditText(Context context, AttributeSet attributes, int defStyleAttr, int defStyleRes) {
super(context, attributes, defStyleAttr, defStyleRes);
this.initializeWellBehavedEditText(context);
}
private void initializeWellBehavedEditText(Context context) {
this.inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
final WellBehavedEditText editText = this;
this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if(showKeyboard) {
showKeyboard = !(inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_FORCED));
}
}
});
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(!focused) this.showKeyboard = false;
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
boolean result = super.requestFocus(direction, previouslyFocusedRect);
this.showKeyboard = true;
final WellBehavedEditText self = this;
this.post(new Runnable() {
@Override
public void run() {
showKeyboard = !(inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_FORCED));
}
});
return result;
}
}
android:windowSoftInputMode="stateAlwaysVisible" - manifest文件中的>。
edittext.requestFocus ();-代码为>。
这将打开软键盘上的编辑文本有请求焦点作为活动出现。
在添加了这些线条后,对我来说很有用。
globalSearchBarMainActivity.setEnabled(true);
globalSearchBarMainActivity.requestFocus();
因为我的自动完整文本视图方法被隐藏,即可见:消失
所以需要加上上面两行
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(globalSearchBarMainActivity, InputMethodManager.SHOW_IMPLICIT);