Android:在 setContentView 之后,软键盘不会出现在文本视图中

发布于 2024-12-08 12:09:48 字数 366 浏览 0 评论 0原文

我的应用程序中的文本视图有问题。当应用程序第一次运行时,它工作得很好,但是当我使用 setContentView 切换到不同的视图然后再次返回时,软键盘将不再打开,但我可以选择文本。

这是我尝试切换回来时的代码片段:

 public void setToMain(String _word)
    {
        setContentView(R.layout.main);
        mWordInput = (TextView) findViewById(R.id.wordInput);
        mWordInput.setText(_word);
    }

即使我不调用 setText 我也会遇到问题。

I am having a problem with a textview in my application. When the application first runs it works perfectly fine, but when I swap to a different view using setContentView and then back again the soft keyboard will no long open but I am able to select the text.

Here is the code snippet of when I try to switch back:

 public void setToMain(String _word)
    {
        setContentView(R.layout.main);
        mWordInput = (TextView) findViewById(R.id.wordInput);
        mWordInput.setText(_word);
    }

Even if I don't call setText I get the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-12-15 12:09:48

我在使用软键盘时也遇到了类似的问题;尽管在我的例子中,即使不使用 setContentView 切换视图,它也不会显示。经过一些实验,我找到了仍然对我有用的解决方案。这个想法是拦截任何 EditText 后代的软键盘显示/隐藏。为此,我覆盖了 Activity 的 onWindowFocusChanged 。

诀窍是在不再需要键盘时将其隐藏起来。

正如您所看到的,我使用了带有SHOW_IMPLICIT的toggleSoftInput,而不是任何HIDE常量。在这种情况下,仅当聚焦视图需要时,IMEManager 才会保留键盘可见,否则它将被隐藏。

private boolean softInputActive;

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    InputMethodManager IMEManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    View focusedView = getCurrentFocus();

    // Find the primitive focused view (not ViewGroup)
    while (focusedView instanceof ViewGroup) {
        focusedView = ((ViewGroup) focusedView).getFocusedChild();
    }


    if (hasFocus) {

        if (focusedView instanceof EditText && focusedView.isEnabled()
                && !IMEManager.isActive(focusedView)) {
            IMEManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            softInputActive = true;
        }
    } else if (softInputActive) {
        if (focusedView != null && IMEManager.isActive()) {
            IMEManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        }
        softInputActive = false;
    }

}

I had a similar problem with the soft keyboard; though in my case it would not show even without switching views with setContentView. After some experimenting I've found the solution which still works for me. The idea was to intercept soft keyboard showing/hiding for any EditText descendant. For this I overrode onWindowFocusChanged of the Activity.

The trick was in hiding the keyboard when it is not needed anymore.

As you can see I used toggleSoftInput with SHOW_IMPLICIT instead of any HIDE constant. In this case IMEManager would retain the keyboard visible only if the focused view requires it otherwise it will be hidden.

private boolean softInputActive;

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    InputMethodManager IMEManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    View focusedView = getCurrentFocus();

    // Find the primitive focused view (not ViewGroup)
    while (focusedView instanceof ViewGroup) {
        focusedView = ((ViewGroup) focusedView).getFocusedChild();
    }


    if (hasFocus) {

        if (focusedView instanceof EditText && focusedView.isEnabled()
                && !IMEManager.isActive(focusedView)) {
            IMEManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            softInputActive = true;
        }
    } else if (softInputActive) {
        if (focusedView != null && IMEManager.isActive()) {
            IMEManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        }
        softInputActive = false;
    }

}
守望孤独 2024-12-15 12:09:48

在清单文件中,您可以在活动声明中使用

android:windowSoftInputMode="stateVisible|adjustPan"

in Manifest file you can use in your Activity declaration

android:windowSoftInputMode="stateVisible|adjustPan"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文