코딩스토리

[Android/안드로이드] 키보드 보이기/키보드 숨기기를 제어하기 본문

Android/유용한 기술

[Android/안드로이드] 키보드 보이기/키보드 숨기기를 제어하기

라크라꾸 2020. 9. 9. 22:16

Android에서 키보드는 EditText가 포커스를 받는 순간 자동으로 올라오게됩니다. 

이번 포스팅에서는 자바코드를 통해 특정 순간에 키보드를 나타나게 하거나 사라지게 하는 기능을 구현해보겠습니다.

 

키보드 올리기

	
InputMethodManager manager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    

 

키보드 내리기

	
InputMethodManager manager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    

 

주의할점은 키보드가 보여질 때는 showSoftInput함수의 첫번째 매개변수로 글이 입력될 뷰를 가르키게되는데, 입력 대상이 되는 EditText에 포커스가 없는 상태면 키보드는 보여지지않습니다.

 


Sample Code

 

MainActivity.java

	
    package com.lakue.keyboardsample;
	
	import androidx.appcompat.app.AppCompatActivity;
	
	import android.os.Bundle;
	import android.view.View;
	import android.view.inputmethod.InputMethodManager;
	import android.widget.Button;
	import android.widget.EditText;
	
	public class MainActivity extends AppCompatActivity {
	
	    EditText editText;
	    Button btn_show_keyboard, btn_hide_keyboard;
	
	
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	
	        editText = findViewById(R.id.editText);
	        btn_show_keyboard = findViewById(R.id.btn_show_keyboard);
	        btn_hide_keyboard = findViewById(R.id.btn_hide_keyboard);
	
	        final InputMethodManager manager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE) ;
	
	
	
	        btn_show_keyboard.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
	
	            }
	        });
	
	        btn_hide_keyboard.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	
	
	            }
	        });
	    }
	}
	

 

activity_main.xml

	
    <?xml version="1.0" encoding="utf-8"?>
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    xmlns:app="http://schemas.android.com/apk/res-auto"
	    xmlns:tools="http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:orientation="vertical"
	    tools:context=".MainActivity">
	
	    <EditText
	        android:id="@+id/editText"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        app:layout_constraintBottom_toBottomOf="parent"
	        app:layout_constraintLeft_toLeftOf="parent"
	        app:layout_constraintRight_toRightOf="parent"
	        app:layout_constraintTop_toTopOf="parent" />
	
	    <Button
	        android:id="@+id/btn_show_keyboard"
	        android:text="키보드 보이기"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"/>
	
	    <Button
	        android:id="@+id/btn_hide_keyboard"
	        android:text="키보드 숨기기"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"/>
	
	</LinearLayout>
	
Comments