【Kotlin】EditTextの外側をクリックしたときにAndroidのソフトキーボードを非表示にする方法
概要
この記事では、Kotlinを使用してAndroidアプリでEditTextの外側をクリックした際にソフトキーボードを非表示にする方法を解説します。
入力欄以外の場所をタップしたときにキーボードを閉じる処理は、ユーザー体験(UX)を向上させるために多くのアプリで採用されている定番の実装です。InputMethodManagerを使えば簡単に実現できます。
実装手順
ステップ1:新規プロジェクトの作成
Android Studioを起動し、「File」→「New Project」を選択します。必要な情報をすべて入力して、新しいプロジェクトを作成してください。
ステップ2:レイアウトファイル(activity_main.xml)の編集
res/layout/activity_main.xml に以下のコードを追加します。
<?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:textColor="@android:color/background_dark" 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.ktの実装
src/MainActivity.kt に以下のコードを追加します。
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var editText: EditText
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editText = findViewById(R.id.editText)
textView = findViewById(R.id.textView)
title = "KotlinApp"
}
fun setText(view: View) {
val newText = editText.text.toString()
textView.text = newText
closeKeyBoard()
}
private fun closeKeyBoard() {
val view = this.currentFocus
if (view != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
}
ステップ4:AndroidManifest.xmlの設定
androidManifest.xml に以下のコードを記述します。
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.q11"> <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>
コードのポイント
この実装の核となるのは closeKeyBoard() メソッドです。処理の流れは以下のとおりです。
currentFocusで現在フォーカスを持っているViewを取得するgetSystemService(Context.INPUT_METHOD_SERVICE)で InputMethodManager のインスタンスを取得するhideSoftInputFromWindow()を呼び出してソフトキーボードを非表示にする
また、「Set Text」ボタンをタップすると、EditTextに入力された文字列がTextViewに反映されると同時に、キーボードも自動的に閉じられます。
アプリの実行と動作確認
それでは、アプリケーションを実行してみましょう。ここでは、実際のAndroid端末がパソコンに接続されているものとして説明を進めます。
Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーにある実行アイコン
をクリックします。ドロップダウンから自分のモバイルデバイスを選択すると、端末の画面にアプリが起動します。

-
【Android】EditTextの外側をタップしたときにソフトキーボードを非表示にする方法
はじめにAndroidアプリでは、EditText(テキスト入力欄)への入力を終えた後もソフトキーボードが開いたままになっていると、操作性が損なわれることがあります。本記事では、InputMethodManagerを使ってソフトキーボードを非表示にする方法を、サンプルプロジェクトを通じてわかりやすく解説します。実装手順ステップ1:新規プロジェクトの作成Android Studioを起動し、「File」→「New Project」を選択して新しいプロジェクトを作成します。必要な項目をすべて入力し、プロジェクトのセットアップを完了させてください。ステップ2:レイアウトファイルの作成res/layo
-
Android用ソフトキーボード開発の基本を解説!入力アプリ作成のステップガイド
はじめに この記事では、Androidでソフトキーボードを使ったテキスト入力アプリを開発する手順を、ステップ形式でわかりやすく解説します。完成したアプリでは、EditTextに入力した文字列をボタン操作でTextViewに表示でき、入力時にはシステムのソフトキーボードが自動的に立ち上がります。 ステップ1:Android Studioで新規プロジェクトを作成する まずAndroid Studioを起動し、メニューからFile ⇒ New Projectを選択してください。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 ステップ2:レイアウトファイル(activity_mai