[英]SoftKeyboard does not display for a newly displayed Fragment
我有一个FragmentActivity,最初显示一个片段,上面有几个按钮。 单击其中一个按钮时,FragmentActivity将显示一个包含某些editText字段的新片段。 当我显示带有editText字段的新片段时,我似乎无法显示软输入键盘。
在清单上使用windowSoftInput模式,因为它立即显示键盘。
android:windowSoftInputMode="stateUnchanged"
我试过用
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
无济于事。 以下是我在Activity中显示新片段的方法:
public void clickHandler(View view) {
switch (view.getId()) {
case R.id.login:
loginFragment = new LoginFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, loginFragment);
transaction.addToBackStack(null);
transaction.commit();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
break;
...
我也尝试从片段的onCreate中调用setSoftInputMode,但是效果不佳。 认为这是一个时间问题,我用handler.postDelayed尝试了它,但也没有用。 它看起来像这样:
onResume...
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
};
handler.postDelayed(runnable, 1000);
任何帮助将不胜感激。 谢谢。
在你的onResume上你可以这样做:
EditText someEditText = (EditText)getActivity().findViewById(R.id.someEditText);
someEditText.requestFocus();
InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(someEditText, InputMethodManager.SHOW_IMPLICIT);
我有一个非常相似的问题。 片段A调用片段B,其中片段B(使用TABS的布局)中包含EditText。 除非我点击其他内容,否则键盘不会出现在此EditText框中。
我在这里尝试了解决方案,以及许多其他stackoverflow解决方案。 许多。 唯一对我有用的是在单击EditText时清除焦点在EditText上。 在OnFocusChangeListener中,我能够强制键盘打开和关闭。
此问题仅发生在Android 2.34设备上,而不是2.2或3.0。 模拟器也没有问题。 清单只有adjustResize。
所以这是一个适合我的解决方案(希望其他人认为这很有用):
在onCreateView(...)
//
//Inflate Your Layout Here
//
//set on focus to force keyboard
editText.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
Log.d(TAG,"On Foucs. Has Focus = " + hasFocus);
if (hasFocus)
{
//open keyboard
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(v,
InputMethodManager.SHOW_FORCED);
}
else
{
//close keyboard
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
v.getWindowToken(), 0);
}
}
});
//Set on click listener to clear focus
editText.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View clickedView)
{
clickedView.clearFocus();
clickedView.requestFocus();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.