Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีซ่อน Soft Keyboard เมื่อคลิกนอก EditText ใน Android ด้วย Kotlin

ซ่อนคีย์บอร์ดอัตโนมัติเมื่อคลิกนอกพื้นที่ EditText

ตัวอย่างนี้สาธิตวิธีซ่อน Soft Keyboard เมื่อผู้ใช้คลิกที่พื้นที่ว่างหรือ View อื่นนอก EditText ใน Android โดยใช้ InputMethodManager ใน Kotlin

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: เลย์เอาต์ (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: โค้ดหลัก (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() {
    private lateinit var editText: EditText
    private 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 = currentFocus
        if (view != null) {
            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }
}

ขั้นตอนที่ 4: Manifest

<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>

วิธีการอื่นๆ ที่นิยมใช้

  • dispatchTouchEvent: Override ใน Activity เพื่อตรวจจับการสัมผัสนอก EditText
  • OnFocusChangeListener: ตรวจจับเมื่อ EditText เสีย Focus
  • android:focusableInTouchMode="true" บน Layout หลัก เพื่อให้ Layout รับ Focus แทน
  • WindowSoftInputMode: ตั้งค่าใน Manifest android:windowSoftInputMode="stateHidden"

วิธี InputMethodManager นี้เหมาะสำหรับการซ่อนคีย์บอร์ดเมื่อมีการกระทำเฉพาะ (เช่น กดปุ่ม Submit)