我认为这是一个错误或功能,尝试向您呈现整个活动,而不会首先用软键盘遮住它。我搜索过一次有关的信息,但不幸的是发现没有什么来自一个真正可靠的来源。
无论如何,要显示软键盘,您可以这样做:
EditText editText = (EditText)findViewById(R.id.edit_text_id);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
我也看到这个代码,强制软键盘在活动开始后变得可见,但从未尝试过:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
如果你想隐藏软键盘,你可以这样做:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
希望有帮助。
编辑:
对于DialogFragment,这应该有效:在onCreateView()方法中,这样做:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_id, container);
EditText editText = (EditText)view.findViewById(R.id.edit_text_id);
// show soft keyboard
editText.requestFocus();
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return view;
}