본문 바로가기
안드로이드 자바

[JAVA][Android] EditText 키보드 엔터(Enter)키 기능 변경

by teamnova 2022. 10. 11.

안녕하세요.

오늘은 안드로이드 EditText 키보드 엔터(Enter)키의 기능을 변경하는 방법에 대해서 알아보겠습니다.

 

[Android] EditText 키보드 엔터(Enter)키 기능 변경

 

안드로이드에서 EditText에 텍스트를 입력할 때 소프트 키보드의 엔터키가 상황에 따라 다르게 나타나는 것을 종종 볼 수 있습니다. 예를 들어 아래의 그림처럼 인터넷 브라우져 주소창이면 '이동' 또는 '검색' 으로, 검색창이면 '검색'으로, 정보 입력창이면 '완료' 등을 볼 수 있죠. 이렇게 소프트 키보드의 엔터키를 상황에 맞게 바꿔주는 방법에 대해서 알아보도록 하겠습니다.

 

 

해당 설정은 xml 코드와 java 코드에서 설정 가능한데요.(둘 중 하나만 설정해도 됨)

아래는 그 예시 입니다.

xml 코드 예시

<EditText
...
android:imeOptions="actionSearch"
android:inputType="text"
/>

 

Java 코드 예시 
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setInputType(InputType.TYPE_CLASS_TEXT);

imeOption에 적용될 수 있는 설정값은 아래 목록을 참고해주세요.

Xml 코드

android:imeOptions="normal"   // 특별한 의미 없음
android:imeOptions="actionUnspecified"   // 특별한 의미 없음
android:imeOptions="actionNone"   // 특별한 의미 없음
android:imeOptions="actionGo"   // '이동'의 의미 (예 : 웹 브라우져에서 사용)
android:imeOptions="actionSearch"   // '검색'의 의미 (예 : 네이버 검색창)
android:imeOptions="actionSend"   // '보내기'의 의미 (예 : 메세지 작성시 사용)
android:imeOptions="actionNext"   // '다음'의 의미 (예 : 회원가입시 다음 필드로 이동시)
android:imeOptions="actionDone"   // '완료'의 의미 (예 : 정보 입력창)
android:imeOptions="actionPrevious"   // '이전'의 의미 (예 : 회원가입시 이전 필드로 이동시) - API11부터 가능

 

Java 코드
        
EditorInfo.IME_ACTION_NONE // 특별한 의미 없음
EditorInfo.IME_ACTION_UNSPECIFIED  // 특별한 의미 없음
EditorInfo.IME_ACTION_GO  // '이동'의 의미 (예 : 웹 브라우져에서 사용)
EditorInfo.IME_ACTION_SEARCH   // '검색'의 의미 (예 : 네이버 검색창)
EditorInfo.IME_ACTION_SEND    // '보내기'의 의미 (예 : 메세지 작성시 사용)
EditorInfo.IME_ACTION_NEXT    // '다음'의 의미 (예 : 회원가입시 다음 필드로 이동시)
EditorInfo.IME_ACTION_DONE    // '완료'의 의미 (예 : 정보 입력창)
EditorInfo.IME_ACTION_PREVIOUS // '이전'의 의미 (예 : 회원가입시 이전 필드로 이동시) - API11부터 가능

 

위와 같이 작성해주고 java 에서 EditText에 OnEditorActionListener 인터페이스를 연결해줍니다.

 

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        switch(actionId){
            case EditorInfo.IME_ACTION_SEARCH :
                Toast.makeText(getApplicationContext(), "검색", Toast.LENGTH_LONG).show();
                break;

            default:
                break;
        }
        
        return false;
    }
});

여기까지 다 완료 해주면 소프트 키보드의 엔터키가 검색으로 바뀌고 클릭 되었을 때 이벤트를 받아 올 수 있습니다.

 

 

이제 이걸 사용한 예제를 만들어보도록 하겠습니다.

우선 xml 파일입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:gravity="center"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="전송 : "
            android:textSize="15dp"
            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            />

        <EditText
            android:id="@+id/send_et"
            android:inputType="text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:imeOptions="actionSend"
            />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="검색 : "
            android:textSize="15dp"
            android:textColor="@android:color/black"
            android:layout_marginLeft="5dp"
            />

        <EditText
            android:id="@+id/search_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            />
        
    </LinearLayout>
</LinearLayout>

 

보시다시피 '전송' EditText와 '검색' EditText를 생성하였고,

전송에만

android:imeOptions="actionSend"

설정값을 주었습니다.

그리고 검색은 java코드에서 설정값을 주겠습니다.

 

 

다음으로 java코드입니다.

public class EnterTest extends AppCompatActivity {
    EditText send, search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter_test);

        send = findViewById(R.id.send_et);
        search = findViewById(R.id.search_et);

        search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        search.setInputType(InputType.TYPE_CLASS_TEXT);
        
        send.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch(actionId){
                  case EditorInfo.IME_ACTION_SEND :
                      Toast.makeText(getApplicationContext(), "전송", Toast.LENGTH_LONG).show();
                    break;
                    
                  default: 
                    break;
                }
                
                return false;
            }
        });
        
        search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch(actionId){
                    case EditorInfo.IME_ACTION_SEARCH :
                        Toast.makeText(getApplicationContext(), "검색", Toast.LENGTH_LONG).show();
                        break;

                    default:
                        break;
                }
                
                return false;
            }
        });

    }
}

이번에는 검색에만

search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
search.setInputType(InputType.TYPE_CLASS_TEXT);

설정값을 준 것을 확인할 수 있습니다.

 

다음으로 실행화면 입니다.

 

 

보시는 것과 같이 각 EditText마다 엔터키의 설정값이 변경되는 것을 확인할 수 있습니다.

 

궁금한 점은 댓글로 남겨주세요.

감사합니다.