在android中单击edittextview时键盘未显示?
当我点击edittextview时,有时显示键盘,有时不显示键盘 在android 2.1中,当我点击edittextview时,它会显示键盘 但当我在安卓2.2上启动同一个应用程序时,它不会显示键盘 请帮助我如何显示该问题。可能的情况: 1) 单击编辑文本时,通常会出现键盘。但是,如果在模拟器中按back键,键盘(而不是屏幕键盘)会变小 2) 在代码中,您可以通过设置标志在单击编辑文本时禁用键盘在android中单击edittextview时键盘未显示?,android,android-edittext,Android,Android Edittext,当我点击edittextview时,有时显示键盘,有时不显示键盘 在android 2.1中,当我点击edittextview时,它会显示键盘 但当我在安卓2.2上启动同一个应用程序时,它不会显示键盘 请帮助我如何显示该问题。可能的情况: 1) 单击编辑文本时,通常会出现键盘。但是,如果在模拟器中按back键,键盘(而不是屏幕键盘)会变小 2) 在代码中,您可以通过设置标志在单击编辑文本时禁用键盘 InputMethodManager inputmethodmgr= (InputMethodMa
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
好的,这可能是一个迟来的反应,但它起了作用 我在android 2.1和2.3.x上遇到过这个问题(没有在其他版本的SDK上测试) 我注意到一件奇怪的事情,当我点击编辑文本无法打开键盘时,我按下后退按钮显示一个警告对话框,然后取消(关闭)它,再次点击编辑文本,现在键盘又恢复了活力 由此我可以得出结论,如果EditText之前没有自己的焦点,键盘将始终显示EditText(在EditText视图上显示警报对话框将使EditText失去焦点) 因此,当EditText出现在前面时,调用下面的函数:
mEditText.clearFocus();
或
在我的例子中,它位于一个
PopupWindow
中,我只需要调用PopupWindow.setFocusable(true)
以下是一个可能的解决方案:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
@Override
public void run() {
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
代码基于下一个链接:
我在Galaxy S3上遇到了类似的问题(在弹出窗口上显示EditText控件-键盘从未显示)。这解决了我的问题:
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();
我不想使用
EditText.clearFocus()
使EditText失去焦点。我想出了这个解决办法
@Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT < 11) {
editText.clearFocus();
editText.requestFocus();
}
}
@覆盖
恢复时公开作废(){
super.onResume();
if(Build.VERSION.SDK_INT<11){
editText.clearFocus();
editText.requestFocus();
}
}
如果您甚至想在单击edittextView时隐藏,它就像一个符咒
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayKeyboard();
}
});
private void displayKeyboard(){
if (textView != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(textView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
在对话框片段中显示EditText时,我遇到了同样的问题。尽管EditText获得焦点(即单击时,它显示闪烁的插入符号),但键盘没有显示 我的解决方案是在DialogFragment的最上方视图中添加一个伪EditText
<EditText
android:id="@+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
是的,但使用这种方法,您会出现意想不到的行为。例如,如果在IMM被强制打开时将应用程序设置为后台,则即使在主屏幕上,应用程序也将保持打开状态。:)啊,这终于对我起作用了,谢谢。
<EditText
android:id="@+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>