在安卓系统中,当我点击EditText控件时键盘没有弹出?

14

当我点击EditTextView时,有时会显示键盘,有时则不会。

在Android 2.1中,当我点击EditTextView时,它会显示键盘。

但是当我在Android 2.2上启动相同的应用程序时,它不会显示键盘。

请帮助我解决这个问题。

9个回答

26

好的,可能有点晚了,但这个方法确实有效。

我在Android 2.1和2.3.x上遇到了这个问题(未在其他版本的SDK上测试)。

我注意到一个奇怪的事情,当我点击EditText无法打开键盘时,我按下返回按钮显示一个警报对话框,然后取消(关闭)它,再次点击EditText,现在键盘又重新出现了。

由此我可以得出结论:如果EditText没有先前获取焦点(在EditText视图上显示警报对话框会使EditText失去焦点),则键盘将始终显示在EditText上。

因此,当您的EditText被带到前台时,请调用下面的函数:

mEditText.clearFocus();
或者
parentViewThatContainsEditTextView.clearFocus();

10

我在Galaxy S3上遇到了类似的问题(在PopupWindow中显示EditText控件时,键盘从未出现)。这是解决我的问题的方法:

final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();

加冕这个家伙,从未想过弹出窗口焦点。 - Vineeth_Bhanukoti

5

我不想使用 editText.clearFocus() 使 EditText 失去焦点。找到了这个解决方案。

@Override
public void onResume() {
    super.onResume();

    if (Build.VERSION.SDK_INT < 11) {
        editText.clearFocus();
        editText.requestFocus();
    }
}

4

这里提供一种可能的解决方案:

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);
                }
            });
        }
    }
});

代码基于以下链接:

http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/

当EditText获得焦点时,此代码将自动显示软键盘。

3
我在显示DialogFragment中的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"/>

2
在我的情况下,它是在一个PopupWindow中,我只需要调用popupWindow.setFocusable(true)

1
它非常有效,如果您甚至想在单击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);
    }
}

是的,但是采用这种方法会出现意料之外的行为。例如,如果你在强制打开输入法管理器时将应用置于后台,它将保持打开状态,即使在主屏幕上也是如此。 :) - worked

1
可能的情况:
1)点击EditText时,通常会弹出键盘。但是如果在模拟器中按下返回键,键盘(而不是屏幕键盘)将消失。
2)在代码中,您可以通过设置标志来禁用点击EditText时的键盘。
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 

0
在您的父视图中检查是否有android:descendantFocusability="blocksDescendants",如果有,请将其删除。

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