Android 安卓:软键盘赢了';在setContentView之后不显示文本视图
我的应用程序中的文本视图有问题。当应用程序第一次运行时,它工作得非常好,但是当我使用setContentView切换到另一个视图,然后再返回时,软键盘将不再打开,但我可以选择文本 以下是我尝试切换回时的代码片段:Android 安卓:软键盘赢了';在setContentView之后不显示文本视图,android,android-softkeyboard,textview,Android,Android Softkeyboard,Textview,我的应用程序中的文本视图有问题。当应用程序第一次运行时,它工作得非常好,但是当我使用setContentView切换到另一个视图,然后再返回时,软键盘将不再打开,但我可以选择文本 以下是我尝试切换回时的代码片段: public void setToMain(String _word) { setContentView(R.layout.main); mWordInput = (TextView) findViewById(R.id.wordInput);
public void setToMain(String _word)
{
setContentView(R.layout.main);
mWordInput = (TextView) findViewById(R.id.wordInput);
mWordInput.setText(_word);
}
即使我不给setText打电话,我也会遇到问题。我的软键盘也有类似的问题;尽管在我的例子中,即使不使用setContentView切换视图,它也不会显示。经过一些实验,我找到了仍然适合我的解决方案。其想法是拦截任何EditText后代的软键盘显示/隐藏。为此,我覆盖了活动的WindowFocusChanged 诀窍是在不再需要键盘时隐藏键盘 如您所见,我使用SHOW_IMPLICIT来切换SoftInput,而不是任何隐藏常量。在这种情况下,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;
}
}
在清单文件中,您可以在活动声明中使用
android:windowSoftInputMode="stateVisible|adjustPan"
据我所知,您不应该多次使用setContentView,请参阅感谢您的帮助。这些类型的android编程技巧有什么好的来源吗?我是android编程新手,我不知道我应该做什么,不应该做什么。