I think it's a bug or a feature which tries to present the whole activity to you without obscuring it with the soft keyboard at first. I've searched once for information regarding that but unfortunately found nothing coming from a really reliable source.
Anyway, to show the soft keyboard you can do this:
EditText editText = (EditText)findViewById(R.id.edit_text_id);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
I've also seen this code that should force the soft keyboard to become visible just after activity start, but never tried it:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
And if you want to hide soft keyboard you can do this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Hope that helps.
Edit:
For a DialogFragment
this should work: in the onCreateView()
method do this:
@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;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…