我试图在程序上首先加载屏幕上的软键盘(不要在Manifest中更改windowSoftInputMode)。
有趣的是在屏幕上首次加载,它根本不起作用。这是代码块。
mEDT.requestFocus();
mEDT.requestFocusFromTouch();
mImm.showSoftInput(mEDT, InputMethodManager.SHOW_IMPLICIT);
showSoftInput返回false,这导致软键盘没有显示。
但是当我点击EditText时。 showSoftInput返回true并显示软键盘。
任何人都可以向我解释发生了什么事吗?
你在使用Fragments吗?我发现showSoftInput()
在片段中不可靠。
在检查源代码后,我发现在requestFocus()
/ onCreate()
或onCreateView()
中调用onResume()
不会立即导致对象聚焦。这很可能是因为尚未创建内容视图。因此,焦点会在Activity或Fragment初始化期间的某个时间发生。
我在showSoftInput()
打电话给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()之前创建了View。
在你的manifest.xml
文件中,添加
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
到您要在其启动时显示键盘的活动名称
试试这个:
<activity
...
android:windowSoftInputMode="stateVisible" >
</activity>
要么
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
您也可以通过编程方式执行此操作
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
你只需要打电话给他,试试这个:
EditText editText= (EditText) findViewById(R.id.editText);
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);