【Android】EditTextの外側をタップしたときにソフトキーボードを非表示にする方法
はじめに
Androidアプリでは、EditText(テキスト入力欄)への入力を終えた後もソフトキーボードが開いたままになっていると、操作性が損なわれることがあります。本記事では、InputMethodManagerを使ってソフトキーボードを非表示にする方法を、サンプルプロジェクトを通じてわかりやすく解説します。
実装手順
ステップ1:新規プロジェクトの作成
Android Studioを起動し、「File」→「New Project」を選択して新しいプロジェクトを作成します。必要な項目をすべて入力し、プロジェクトのセットアップを完了させてください。
ステップ2:レイアウトファイルの作成
res/layout/activity_main.xml に以下のコードを記述します。TextView・EditText・Buttonを縦方向に並べたシンプルなレイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Enter text here"
android:textSize="20sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="setText"
android:text="Set Text"
android:textStyle="bold"/>
</LinearLayout>ステップ3:MainActivityの実装
src/MainActivity.java に以下のコードを記述します。
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
}
public void setText(View view){
String newText = editText.getText().toString();
textView.setText(newText);
closeKeyBoard();
}
private void closeKeyBoard(){
View view = this.getCurrentFocus();
if (view != null){
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}ポイントは closeKeyBoard() メソッドです。getCurrentFocus() で現在フォーカスを持っているViewを取得し、そのWindowTokenを指定して InputMethodManager の hideSoftInputFromWindow() を呼び出すことで、表示中のソフトキーボードを閉じることができます。
ステップ4:AndroidManifest.xmlの確認
androidManifest.xml は以下のようになります。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>アプリの実行と動作確認
それではアプリを実行してみましょう。実機のAndroid端末をパソコンに接続し、Android Studioのツールバーにある「Run」アイコンをクリックします。デバイス選択画面で接続したモバイル端末を選択すると、端末にアプリが起動します。
EditTextにテキストを入力してキーボードを表示させた状態でボタンをタップすると、入力内容がTextViewに反映され、同時にソフトキーボードが非表示になります。


補足:EditTextの外側タップでキーボードを閉じるには
「EditText以外の画面領域をタップしたときにキーボードを閉じたい」場合は、ルートレイアウトにタッチリスナーを設定する方法が一般的です。
findViewById(R.id.rootLayout).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
closeKeyBoard();
return false;
}
});また、Activityの dispatchTouchEvent() をオーバーライドして、画面タッチのたびにキーボードを閉じる実装もよく使われます。
なお、本記事のサンプルコードは旧サポートライブラリ(android.support.v7)を使用していますが、現在のAndroid Studioで作成するプロジェクトではAndroidX(androidx.appcompat.app.AppCompatActivity)が標準です。import文を読み替えてご利用ください。
-
Android用ソフトキーボード開発の基本を解説!入力アプリ作成のステップガイド
はじめに この記事では、Androidでソフトキーボードを使ったテキスト入力アプリを開発する手順を、ステップ形式でわかりやすく解説します。完成したアプリでは、EditTextに入力した文字列をボタン操作でTextViewに表示でき、入力時にはシステムのソフトキーボードが自動的に立ち上がります。 ステップ1:Android Studioで新規プロジェクトを作成する まずAndroid Studioを起動し、メニューからFile ⇒ New Projectを選択してください。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 ステップ2:レイアウトファイル(activity_mai
-
【Android】アクティビティでナビゲーションバーを完全に非表示にする方法
本記事では、Androidアプリのアクティビティでナビゲーションバー(画面下部の戻る・ホーム・最近使ったアプリの各ボタンが並ぶ領域)を完全に非表示にする方法を解説します。ゲームや動画再生アプリなど、没入感のある全画面UIを実装したい場合に役立つテクニックです。 手順1:Android Studioで新規プロジェクトを作成 Android Studioを起動し、メニューから「File」→「New Project」を選択して新しいプロジェクトを作成します。必要事項を入力して、プロジェクトのセットアップを完了させましょう。 手順2:レイアウトファイル(activity_main.xml)の作成 re