프로그래밍/안드로이드

[안드로이드] 키보드 보이기(올리기) / 숨기기(내리기) - android keyboard show/hide

오치리일상 2017. 8. 14.

안드로이드 앱 개발에 있어서 EditText에 소프트키보드(가상키보드)을 터치가 아닌 동적으로 보이거나 숨기는 기능을 구현해야 할 때가 있다.

 

여기서 소프트키보드(가상키보드)를 강제로 보이거나 숨기는 방법에 대해서 알아본다.

 

 

 

이 기능을 구현하는 코드는 간단하다.

 

 

 

 

 

1. InputMethodManager 객체를 선언한다. 

 

 

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
EditText input1 = (EditText) findViewById(R.id.input1);

우선 위와 같이 InptMethodManager 객체를 선언하고 getSystemService(INPUT_METHOD_SERVCIE) 로 입력에 관한 Manager를 반한받는다.

 

그리고 소프트키보드의 입력 포커스를 맞출 EditText 객체변수를 선언한다.

 

 

 

 

2. 키보드 보이기/올리기 

 

imm

.showSoftInput(input

, 0

)

;

첫번째 매개변수로 소프트키보드 입력 포커스가 수행될 EditText의 객체변수를 넣어준다.

 

두번째 매개변수는 별다른 Flag는 없으므로 0을 넘겨준다.

 

주의 : 첫버째 매개변수가 다른 EditText에 포커스 되어있다면 키보드가 보여지지 않는다.

 

 

 

3. 키보드 숨기기/내리기

 

imm.hideSoftInputFromWindow(input1.getWindowToken(), 0);

첫번째 매개변수에 소프트키보드 입력 포커스에 해당되는 EditText의 객체변수의 getWindowToken()을 넣어준다.

 

두번째 매개변수에는 별다른 Flag는 없으므로 0을 넘겨준다.

 

 

 

 

 

아래는 샘플 소스

* activity_main.xml

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

    <EditText
        android:id="@+id/input1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000">

    </EditText>

    <Button
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="키보드 보이기"/>
    <Button
        android:id="@+id/hide"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="키보드 숨기기"/>

</LinearLayout>

 

* MainActivity.java

package com.studio572.samplekeyboardshowhide;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener{

    private EditText input1;
    private Button show, hide;
    private InputMethodManager imm;

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

        input1 = (EditText) findViewById(R.id.input1);


        // input 창과 키보드를 숨기고 보이게 할 Button을 정의한다.
        show = (Button) findViewById(R.id.show);
        hide = (Button) findViewById(R.id.hide);

        // 입력받는 방법을 관리하는 Manager객체를  요청하여 InputMethodmanager에 반환한다.
        imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

        // 키보드를 보이게 할 버튼 클릭 이벤트 정의
        show.setOnClickListener(this);

        // 키보드를 숨기게 할 버튼 클릭 이벤트 정의
        hide.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.show:
                // 키보드를 보여준다.
                // 첫번째 매개변수에 해당하는(여기서는 input) 곳에 포커스가 주어진다.
                // 두번째 매개변수에는 해당 Flag없이 0을 넘겨준다.
                // 주의 : 첫번째 매개변수가 다른 EditText에 포커스 되어있다면 키보드가 보여지지 않는다.
                imm.showSoftInput(input1, 0);
                break;
            case R.id.hide:
                // 키보드를 숨겨준다.
                // 첫번째 매개변수에 해당하는(여기서는 input) 곳에 키보드가 보이면 키보드를 숨긴다
                // 두번째 매개변수에는 해당 Flag없이 0을 넘겨준다.
                imm.hideSoftInputFromWindow(input1.getWindowToken(), 0);
                break;
        }
    }
}

 

 

* 안드로이드 매니페스트(AndroidManifest.xml) 에서의 소프트키보드 제어하기

 

위 방법 외에 안드로이드 매니페스트(AndroidManifest.xml)에 설정값을 넣어줌으로써, 액티비티 시작시에 자동으로 소프트키보드를 보이거나 숨길 수 있다.

 

activity 설정에서 android:windowSoftInputmode 의 value에  "stateAlwaysVisible" 과 "stateAlwaysHidden" 를 넣어주면 된다.

 

* 안드로이드 매니페스트에서 소프트키보드 보이기

<activity android:name=".MainActivity" android:windowSoftInputMode="stateAlwaysVisible"/>

 

* 안드로이드 매니페스트에서 소프트키보드 숨기기

<activity android:name=".MainActivity" android:windowSoftInputMode="stateAlwaysHidden"/>

 

 

 

 

  

 

댓글

💲 추천 글