ฉันจะยกเลิกคีย์บอร์ดเมื่อกดปุ่มได้อย่างไร?
ฉันจะยกเลิกคีย์บอร์ดเมื่อกดปุ่มได้อย่างไร?
คำตอบ:
คุณต้องการปิดการใช้งานหรือยกเลิกคีย์บอร์ดเสมือน?
หากคุณต้องการยกเลิกมันคุณสามารถใช้บรรทัดของรหัสต่อไปนี้ในปุ่มของคุณเมื่อคลิกกิจกรรม
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity()) สำหรับครั้งแรกที่จะหาเรื่องgetCurrentFocus().getWindowToken() hideSoftInputFromWindow()นอกจากนี้ทำในonPause()และไม่onStop()ถ้าคุณพยายามทำให้มันหายไปเมื่อเปลี่ยนกิจกรรม
โซลูชันด้านบนใช้ไม่ได้กับอุปกรณ์ทั้งหมดและยิ่งกว่านั้นใช้ EditText เป็นพารามิเตอร์ นี่คือวิธีแก้ปัญหาของฉันเพียงเรียกวิธีง่าย ๆ นี้:
private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if(imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
isAcceptingText()ทำให้คำตอบนี้ดีกว่าผู้อื่น
นี่คือทางออกของฉัน
public static void hideKeyboard(Activity activity) {
View v = activity.getWindow().getCurrentFocus();
if (v != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
คุณยังสามารถใช้รหัสนี้ในเหตุการณ์คลิกปุ่ม
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
นี่คือวิธีแก้ปัญหา Kotlin (ผสมคำตอบต่าง ๆ ในชุดข้อความ)
สร้างฟังก์ชั่นส่วนขยาย (อาจอยู่ในคลาส ViewHelpers ทั่วไป)
fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}
จากนั้นก็กินโดยใช้:
// from activity
this.dismissKeyboard()
// from fragment
activity.dismissKeyboard()
ทางออกแรกกับ InputMethodManager ทำงานเหมือนแชมป์สำหรับฉันเมธอด getWindow (). setSoftInputMode ไม่ได้อยู่บน Android 4.0.3 HTC Amaze
@Ethan Allen ฉันไม่จำเป็นต้องแก้ไขข้อความให้เป็นที่สุด บางทีคุณกำลังใช้คลาสภายใน EditText ที่คุณประกาศเมธอดประกอบด้วย? คุณสามารถทำให้ EditText เป็นตัวแปรคลาสของกิจกรรมได้ หรือเพียงแค่ประกาศ EditText ใหม่ภายในคลาส / เมธอดและใช้ findViewById () อีกครั้ง นอกจากนี้ฉันไม่พบว่าฉันต้องการรู้ว่า EditText ใดในแบบฟอร์มที่มีโฟกัส ฉันสามารถเลือกได้เองและใช้มัน ชอบมาก
EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
public static void hideSoftInput(Activity activity) {
try {
if (activity == null || activity.isFinishing()) return;
Window window = activity.getWindow();
if (window == null) return;
View view = window.getCurrentFocus();
//give decorView a chance
if (view == null) view = window.getDecorView();
if (view == null) return;
InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null || !imm.isActive()) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Throwable e) {
e.printStackTrace();
}
}
โซลูชันนี้ตรวจสอบให้แน่ใจว่าซ่อนแป้นพิมพ์ด้วยเช่นกันหากไม่ได้เปิดขึ้นมา มันใช้ส่วนขยายเพื่อให้สามารถใช้จากชั้นเจ้าของบริบทใด ๆ
fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}
ด้วยการใช้บริบทของมุมมองเราสามารถบรรลุผลลัพธ์ที่ต้องการด้วยวิธีการต่อไปนี้ใน Kotlin:
/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}
return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)
เมื่ออยู่ในสถานที่เพียงแค่โทร:
editTextFoo.dismiss()