입력 방법 유형 지정

Compose 방법 사용해 보기
Jetpack Compose는 Android에 권장되는 UI 도구 키트입니다. Compose에서 터치 및 입력을 사용하는 방법을 알아보세요.

모든 텍스트 필드에는 이메일 주소, 전화번호 또는 일반 텍스트와 같은 특정 유형의 텍스트 입력이 필요합니다. 시스템이 터치 키보드와 같은 적절한 소프트 입력 방법을 표시하도록 앱의 각 텍스트 필드에 입력 유형을 지정해야 합니다.

입력 방법에 사용할 수 있는 버튼 유형 외에도 입력 방법이 맞춤법 제안을 제공하는지, 새 문장을 대문자로 표기하는지, 캐리지 리턴 버튼을 완료 또는 다음과 같은 작업 버튼으로 대체할지와 같은 동작을 지정할 수 있습니다. 이 페이지에서는 이러한 특성을 지정하는 방법을 설명합니다.

키보드 유형 지정

<EditText> 요소에 android:inputType 속성을 추가하여 항상 텍스트 필드의 입력 방법을 선언합니다.

전화 입력
그림 1. phone 입력 유형

예를 들어 전화번호 입력을 위한 입력 방법을 원하는 경우 다음과 같이 "phone" 값을 사용합니다.

<EditText
    android:id="@+id/phone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/phone_hint"
    android:inputType="phone" />
textPassword 입력 유형
그림 2. textPassword 입력 유형

비밀번호를 입력하는 텍스트 필드인 경우 텍스트 필드에 사용자의 입력이 숨겨지도록 "textPassword" 값을 사용합니다.

<EditText
    android:id="@+id/password"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    ... />

android:inputType 속성으로 기록되는 값은 여러 가지가 있으며, 일부 값을 결합하여 입력 방법의 모양과 추가 동작을 지정할 수 있습니다.

맞춤법 제안 및 기타 동작 사용 설정

자동 수정
그림 3. textAutoCorrect를 추가하면 맞춤법 오류 시 자동 수정을 제공합니다.

android:inputType 속성을 사용하면 입력 방법의 다양한 동작을 지정할 수 있습니다. 가장 중요한 점은 텍스트 필드가 문자 메시지와 같은 기본 텍스트 입력용인 경우 "textAutoCorrect" 값을 사용하여 맞춤법 자동 수정을 사용 설정한다는 것입니다.

android:inputType 속성을 사용하여 서로 다른 동작과 입력 방법 스타일을 결합할 수 있습니다. 예를 들어 다음은 문장의 첫 단어를 대문자로 표기하고 맞춤법 오류를 자동으로 수정하는 텍스트 필드를 만드는 방법입니다.

<EditText
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType=
        "textCapSentences|textAutoCorrect"
    ... />

입력 방법 작업 지정하기

대부분의 소프트 입력 방법에서는 하단 모서리에 현재 텍스트 필드에 적합한 사용자 작업 버튼을 제공합니다. 기본적으로 시스템은 이 버튼을 다음 작업이나 완료 작업에 사용합니다. 단, 텍스트 필드가 여러 줄 텍스트(예: android:inputType="textMultiLine" 사용)를 지원하는 경우는 예외입니다. 이 경우 작업 버튼은 캐리지 리턴입니다. 하지만 텍스트 필드에 더 적절할 수 있는 다른 작업(예: 보내기 또는 이동)을 지정할 수 있습니다.

키보드 작업 버튼을 지정하려면 android:imeOptions 속성을 "actionSend" 또는 "actionSearch"와 같은 작업 값과 함께 사용합니다. 예를 들면 다음과 같습니다.

보내기 버튼
그림 4. android:imeOptions="actionSend"를 선언하면 Send 버튼이 표시됩니다.
<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

그러면 EditText 요소의 TextView.OnEditorActionListener를 정의하여 작업 버튼 누르기를 수신 대기할 수 있습니다. 리스너에서 다음 예와 같이 EditorInfo 클래스에 정의된 적절한 IME 작업 ID(예: IME_ACTION_SEND)에 응답합니다.

Kotlin

findViewById<EditText>(R.id.search).setOnEditorActionListener { v, actionId, event ->
    return@setOnEditorActionListener when (actionId) {
        EditorInfo.IME_ACTION_SEND -> {
            sendMessage()
            true
        }
        else -> false
    }
}

Java

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

자동 완성 제안 제공

사용자가 입력할 때 추천 단어를 제공하려면 EditText의 서브클래스인 AutoCompleteTextView를 사용하면 됩니다. 자동 완성을 구현하려면 텍스트 추천을 제공하는 Adapter를 지정해야 합니다. 데이터의 출처(예: 데이터베이스 또는 배열)에 따라 여러 어댑터를 사용할 수 있습니다.

텍스트 추천
그림 5. 텍스트 추천이 포함된 AutoCompleteTextView의 예

다음 절차에서는 ArrayAdapter를 사용하여 배열에서 추천을 제공하는 AutoCompleteTextView를 설정하는 방법을 설명합니다.

  1. 레이아웃에 AutoCompleteTextView를 추가합니다. 다음은 텍스트 필드만 있는 레이아웃입니다.
    <?xml version="1.0" encoding="utf-8"?>
    <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/autocomplete_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
  2. 모든 텍스트 제안을 포함하는 배열을 정의합니다. 예를 들어 다음은 국가 이름 배열입니다.
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="countries_array">
            <item>Afghanistan</item>
            <item>Albania</item>
            <item>Algeria</item>
            <item>American Samoa</item>
            <item>Andorra</item>
            <item>Angola</item>
            <item>Anguilla</item>
            <item>Antarctica</item>
            ...
        </string-array>
    </resources>
    
  3. Activity 또는 Fragment에서 다음 코드를 사용하여 추천을 제공하는 어댑터를 지정합니다.

    Kotlin

    // Get a reference to the AutoCompleteTextView in the layout.
    val textView = findViewById(R.id.autocomplete_country) as AutoCompleteTextView
    // Get the string array.
    val countries: Array<out String> = resources.getStringArray(R.array.countries_array)
    // Create the adapter and set it to the AutoCompleteTextView.
    ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries).also { adapter ->
        textView.setAdapter(adapter)
    }
    

    Java

    // Get a reference to the AutoCompleteTextView in the layout.
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    // Get the string array.
    String[] countries = getResources().getStringArray(R.array.countries_array);
    // Create the adapter and set it to the AutoCompleteTextView.
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
    textView.setAdapter(adapter);
    

    이전 예에서는 countries_array 문자열 배열의 각 항목을 simple_list_item_1 레이아웃에 있는 TextView에 바인딩하도록 새 ArrayAdapter가 초기화됩니다. 이는 목록에 있는 텍스트의 표준 모양을 제공하는 Android에서 제공하는 레이아웃입니다.

  4. setAdapter()를 호출하여 AutoCompleteTextView에 어댑터를 할당합니다.