我试图在屏幕上弹出一个软键盘,首先以编程方式加载(而不是在Manifest中更改windowSoftInputMode )。
有趣的是,在屏幕上第一次加载,它根本没有工作。这是代码块。
mEDT.requestFocus();
mEDT.requestFocusFromTouch();
mImm.showSoftInput(mEDT, InputMethodManager.SHOW_IMPLICIT);showSoftInput是返回假的,这导致软键盘没有显示。
但是当我点击EditText的时候。showSoftInput返回true,并显示软键盘。
有人能向我解释一下发生了什么吗?
发布于 2016-03-25 22:02:58
你在用碎片吗?我发现showSoftInput()在片段上是不可靠的。
在检查了源代码之后,我发现在onCreate()/onCreateView()或onResume()中调用onCreateView()并不会立即导致对象聚焦。这很可能是因为尚未创建内容视图。因此,焦点稍后会在活动或片段的初始化过程中发生。
我在onViewCreated()中调用onViewCreated()获得了更多的成功。
public class MyFragment extends Fragment {
private InputMethodManager inputMethodManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
EditText text1 = (EditText) view.findViewById(R.id.text1);
text1.requestFocus();
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view.findFocus(), InputMethodManager.SHOW_IMPLICIT);
super.onViewCreated(view, savedInstanceState);
}
}即使你不使用片段,我打赌同样的规则也适用。因此,确保在调用showSoftInput()之前创建了视图。
发布于 2016-01-27 04:05:54
在manifest.xml文件中,添加
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />到您的活动名称,您希望在其启动上显示键盘。
发布于 2016-01-27 04:17:13
试试这个:
<activity
...
android:windowSoftInputMode="stateVisible" >
</activity>或
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);https://stackoverflow.com/questions/35028676
复制相似问题