Android:在 setContentView 之后,软键盘不会出现在文本视图中
我的应用程序中的文本视图有问题。当应用程序第一次运行时,它工作得很好,但是当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用软键盘时也遇到了类似的问题;尽管在我的例子中,即使不使用 setContentView 切换视图,它也不会显示。经过一些实验,我找到了仍然对我有用的解决方案。这个想法是拦截任何 EditText 后代的软键盘显示/隐藏。为此,我覆盖了 Activity 的 onWindowFocusChanged 。
诀窍是在不再需要键盘时将其隐藏起来。
正如您所看到的,我使用了带有SHOW_IMPLICIT的toggleSoftInput,而不是任何HIDE常量。在这种情况下,仅当聚焦视图需要时,IMEManager 才会保留键盘可见,否则它将被隐藏。
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.
在清单文件中,您可以在活动声明中使用
in Manifest file you can use in your Activity declaration