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

วิธีแสดง Soft Keyboard อัตโนมัติเมื่อ EditText ได้ Focus ใน Android

บทความนี้สาธิตวิธีบังคับแสดง Soft Keyboard (On-screen Keyboard) เมื่อ EditText ได้ Focus โดยใช้ InputMethodManager

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

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="Type here..."
        android:padding="16dp"
        android:textSize="18sp" />

</RelativeLayout>

ขั้นตอนที่ 3: 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;

public class MainActivity extends AppCompatActivity {
    EditText editText;

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

        editText = findViewById(R.id.editText);

        // แสดง Keyboard ทันทีเมื่อ Activity เริ่มต้น
        showSoftKeyboard(editText);
    }

    public void showSoftKeyboard(View view) {
        if (view.requestFocus()) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        }
    }

    // เมธอดสำหรับซ่อน Keyboard
    public void hideSoftKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

วิธีอื่นๆ ในการแสดง/ซ่อน Keyboard

1. ใน AndroidManifest.xml (สำหรับ Activity ทั้งหมด)

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="stateVisible|adjustResize" >
    <!-- stateVisible = แสดง keyboard ตอนเริ่ม Activity -->
    <!-- stateAlwaysVisible = แสดงตลอดเวลา -->
    <!-- stateHidden = ซ่อน -->
</activity>

2. ใช้ Extension Function (Kotlin)

fun EditText.showKeyboard() {
    requestFocus()
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}

fun EditText.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(windowToken, 0)
}

// ใช้งาน
editText.showKeyboard()
editText.hideKeyboard()

3. แสดง Keyboard หลัง Delay (แก้ปัญหาไม่ขึ้นบ้าง)

editText.postDelayed(() -> {
    showSoftKeyboard(editText);
}, 200); // รอ 200ms ให้ Layout พร้อม

⚠️ ปัญหาที่พบบ่อยและวิธีแก้

ปัญหาสาเหตุวิธีแก้
Keyboard ไม่ออกView ยังไม่ได้ Focus หรือ Layout ยังไม่เสร็จใช้ postDelayed หรือ view.requestFocus() ก่อน
Keyboard ไม่ออกใน Dialog/FragmentContext ผิด หรือ Window ไม่ได้ Focusใช้ dialog.getWindow().setSoftInputMode(...)
Keyboard บัง Layoutไม่มี adjustResizeเพิ่ม android:windowSoftInputMode="adjustResize" ใน Manifest

WindowSoftInputMode Options

  • stateVisible - แสดงเมื่อ Activity เริ่ม
  • stateAlwaysVisible - แสดงตลอด
  • stateHidden - ซ่อน
  • adjustResize - ปรับขนาด Layout ให้พอดี
  • adjustPan - เลื่อนหน้าจอขึ้น (ไม่ปรับ Layout)