본문 바로가기
개발

[Android] EditText 입력 시 키보드 엔키 기능 변경 하는 두가지 방법

by 테크냥이 2020. 5. 13.
반응형

안녕하세요 찌빠냥입니다.

EditText를 클릭하면 하단 키보드 팝업이 올라오는데요 여기에 일반 엔터키에 해당하는 부분이 상황에 따라 검색, 엔터, 기타 등등 변경됩니다. 기능을 어떻게 적용하는지 알아보겠습니다.

 

왼쪽부터 imeOptions이 normal, actionSearch, actionGo 일 때 키보드 팝업

1. 안드로이드 레이아웃 xml 파일에서 변경

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:imeOptions="actionSearch"
        android:id="@+id/searchText"
        />

 

2. java에서 변경

EditText searchText = (EditText)findViewById(R.id.searchText);
searchBtn.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

 

3. 해당 키를 눌렀을 때 동작 구현

        EditText searchText = (EditText)findViewById(R.id.searchText);
        searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch(actionId){
                    case EditorInfo.IME_ACTION_SEARCH:
                        // 검색동작 구현
                        break;
                    case EditorInfo.IME_ACTION_GO:
                        // '이동' 에 대한 내용 구현
                        break;
                    default:
                    	// 기본 동작 구현
                    	break;
                }
                return true;
            }
        });

 

그 외 imeOptions 옵션 값에 대해서는 따로 검색해 주시면 되겠습니다.

감사합니다.

반응형

댓글