안드로이드
Android EditText Enter 이벤트 처리
SourceTree
2021. 11. 20. 09:23
반응형
EditText에서 엔터키 이벤트 처리하는 예제입니다.
레이아웃
- id, password를 입력받는 형태이며, id입력시의 키패드는 엔터키가 다음으로 설정
- password를 입력받은 후 엔터키를 누르면 이벤트를 받아 로그인 처리를 하는 예제임
<EditText
android:id="@+id/edit_id"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="아이디"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:singleLine="true"
android:imeOptions="actionNext"/>
<EditText
android:id="@+id/edit_pass"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:hint="비밀번호"
android:password="true"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:singleLine="true"
android:imeOptions="actionGo"/>
엔터키 이벤트 처리
- imeOptions가 actionDone인 경우는 이벤트가 오지 않음!
//엔터키 이벤트 처리
editPass.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//Enter key Action
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
//키패드 내리기
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binding.editPass.getWindowToken(), 0);
//처리
prcss();
return true;
}
return false;
}
});
반응형