ใน Android มีบางสถานการณ์ เราควรปิดแป้นพิมพ์เริ่มต้นของ Android อย่างแรง เพื่อให้ตัวอย่างนี้ช่วยคุณได้
ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่
ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml
<?xml version = "1.0" encoding = "utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context = ".MainActivity"> <EditText android:id = "@+id/editext" android:layout_width = "match_parent" android:layout_height = "wrap_content"> </EditText> <Button android:id = "@+id/button" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Click here to hide" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintLeft_toLeftOf = "parent" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toTopOf = "parent" /> </android.support.constraint.ConstraintLayout>
ขั้นตอนที่ 3 เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Handler mHandler;
ProgressDialog mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
EditText editext = findViewById(R.id.editext);
editext.requestFocus();
button.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
hideKeybaord(v);
break;
}
}
private void hideKeybaord(View v) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);
}
} ในโค้ดด้านบนเมื่อคุณคลิกที่ปุ่ม มันจะซ่อนคีย์บอร์ด หากต้องการซ่อนแป้นพิมพ์ ให้ใช้รหัสต่อไปนี้
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);
ขั้นตอนที่ 5 − เพิ่มรหัสต่อไปนี้ลงในไฟล์รายการดังที่แสดงด้านล่าง
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication"> <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" android:windowSoftInputMode = "stateAlwaysVisible"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
ในโค้ดด้านบนจะมี windowSoftInputMode ซึ่งหมายความว่าจะแสดงแป้นพิมพ์เสมอเมื่อแก้ไขข้อความที่ร้องขอได้โฟกัส
มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

ตอนนี้คลิกที่ปุ่ม มันจะซ่อนแป้นพิมพ์ดังที่แสดงด้านล่าง -
