Đoạn mã sau phù hợp với tôi , sau khi hai câu trả lời khác không hoạt động với tôi :
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
passwordInput.postDelayed(new ShowKeyboard(), 300);
}
Trong trường hợp ShowKeyboard
là
private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
Sau khi nhập thành công, tôi cũng đảm bảo rằng tôi ẩn bàn phím
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Về mặt kỹ thuật, tôi chỉ thêm 300 ms trễ trước khi chạy yêu cầu hiển thị bàn phím mềm. Kỳ lạ, phải không? Cũng được đổi requestFocus()
thành requestFocusFromTouch()
.
CHỈNH SỬA: Không sử dụng requestFocusFromTouch()
nó cung cấp một sự kiện cảm ứng cho trình khởi chạy. Gắn bó với requestFocus()
.
EDIT2: Trong Hộp thoại ( DialogFragment
), sử dụng như sau
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
thay vì
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);