Android/자료정리
[Android]EditText focus 제거 /키패드 숨기기/
썩소천사
2017. 8. 28. 11:14
반응형
EditText Focus 제거에 대한 실험!!
- Dialog 호출 시 키패드 활성화 되는 문제를 해결하고자 구글링 후 최종 결과 기록.
1. EditText를 감싸고 있는 LinearLayout에 옵션 추가.
android:focusable="true"
android:focusableInTouchMode="true"
-> 포커스는 사라지지만 키패드는 활성화 됨.
2. edittext.clearFocus() 호출.
-> 변화없음.
3. Edittext XML에 옵션을 주고 키패드 닫힘 매서드 호출.
android:focusable="true"
android:focusableInTouchMode="true"
-> 변화없음.
4. delay를 통한 focus제거.
-> 키패드 내려감!!!
-> 원인은 화면이 그려지기 전 keypad 내리는 코드 호출 시 view를 찾지 못하기 때문에 코드가 먹히지 않음.
최종 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | protected void onCreate(Bundle savedInstanceState) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { hideKeypad(); } }, 100); } public void setFocus() { if (mEditText.requestFocus()) ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mEditText, 0); } public void hideKeypad() { View view = getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } } | cs |
반응형