Android TextField : 프로그래밍 방식으로 포커스 + 소프트 입력 설정
제 생각에는 EditText 검색이 있고 필드에서 클릭 이벤트의 동작을 프로그래밍 방식으로 트리거하고 싶습니다. 즉, 텍스트 필드에 포커스를두고 필요한 경우 소프트 키보드를 표시하고 싶습니다 (사용 가능한 하드 키보드가없는 경우).
나는 시도했다 field.requestFocus()
. 필드는 실제로 포커스를 받지만 소프트 키보드는 표시되지 않습니다.
나는 시도했다 field.performClick()
. 하지만 필드의 OnClickListener 만 호출합니다.
어떤 생각?
좋은 선생님, 이것을 시도하십시오 :
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
확실하지 않지만 일부 휴대 전화 (일부 구형 기기)에서 필요할 수 있습니다.
final InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
다음은 나를 위해 일한 코드입니다.
edittext.post(new Runnable() {
public void run() {
edittext.requestFocusFromTouch();
InputMethodManager lManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
lManager.showSoftInput(edittext, 0);
}
});
그게 다야! 즐겨 ;)
다음 코드는 나를 위해 일한 애프터, 다른 두 답변 나를 위해 작동하지 않았다 :
@Override
public void onResume() {
super.onResume();
SingletonBus.INSTANCE.getBus().register(this);
//passwordInput.requestFocus(); <-- that doesn't work
passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen
}
어디 ShowKeyboard
있다
private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
// passwordInput.requestFocusFromTouch();
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
After a successful input, I also make sure I hide the keyboard
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Technically, I just added 300 ms of delay before running the soft keyboard display request. Weird, right? Also changed requestFocus()
to requestFocusFromTouch()
.
EDIT: Don't use requestFocusFromTouch()
it gives a touch event to the launcher. Stick with requestFocus()
.
EDIT2: In Dialogs (DialogFragment
), use the following
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
instead of
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
field.post(new Runnable() {
@Override
public void run() {
field.requestFocus();
field.onKeyUp(KeyEvent.KEYCODE_DPAD_CENTER, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}
});
In my case, I wanted to display the virtual keyboard with no reference to a specific textbox, so I used the first part of the accepted answer to focus :
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
Then I show the virtual keyboard :
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
참고URL : https://stackoverflow.com/questions/8080579/android-textfield-set-focus-soft-input-programmatically
'program tip' 카테고리의 다른 글
awk를 사용하여 특정 번호 뒤에있는 모든 열을 인쇄하는 방법은 무엇입니까? (0) | 2020.10.18 |
---|---|
bash 스크립트를 사용하여 텔넷 세션 자동화 (0) | 2020.10.18 |
인증 프록시 뒤의 Windows에서 pip를 사용하는 방법 (0) | 2020.10.18 |
Android 버튼 개체 내에서 텍스트 주변의 안쪽 여백을 줄이려면 어떻게해야합니까? (0) | 2020.10.18 |
변형없이 객체에서 값 제거 (0) | 2020.10.18 |