ใช่มันเป็นไปได้อย่างสมบูรณ์ที่จะแสดงและซ่อนแป้นพิมพ์และดักฟังการโทรไปที่ปุ่มย้อนกลับ มันเป็นความพยายามพิเศษเล็กน้อยเนื่องจากมีการกล่าวถึงไม่มีวิธีโดยตรงในการดำเนินการนี้ใน API กุญแจสำคัญคือการแทนที่boolean dispatchKeyEventPreIme(KeyEvent)ภายในเค้าโครง สิ่งที่เราทำคือสร้างเค้าโครงของเรา ฉันเลือก RelativeLayout เนื่องจากเป็นฐานของกิจกรรมของฉัน
<?xml version="1.0" encoding="utf-8"?>
<com.michaelhradek.superapp.utilities.SearchLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.michaelhradek.superapp"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">
ภายในกิจกรรมของเราเราตั้งค่าช่องป้อนข้อมูลและเรียกใช้setActivity(...)ฟังก์ชัน
private void initInputField() {
mInputField = (EditText) findViewById(R.id.searchInput);
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
mInputField.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
SearchLayout.setSearchActivity(this);
}
เห็นได้ชัดว่าinitInputField()ฟังก์ชันตั้งค่าช่องป้อนข้อมูล นอกจากนี้ยังเปิดใช้งานปุ่ม Enter เพื่อเรียกใช้ฟังก์ชันการทำงาน (ในกรณีของฉันคือการค้นหา)
@Override
public void onBackPressed() {
DataHelper.cancelSearch();
hideKeyboard();
super.onBackPressed();
}
ดังนั้นเมื่อonBackPressed()มีการเรียกใช้ภายในเลย์เอาต์ของเราเราสามารถทำอะไรก็ได้ที่ต้องการเช่นซ่อนแป้นพิมพ์:
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInputField.getWindowToken(), 0);
}
อย่างไรก็ตามนี่คือการแทนที่ RelativeLayout ของฉัน
package com.michaelhradek.superapp.utilities;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
public class SearchLayout extends RelativeLayout {
private static final String TAG = "SearchLayout";
private static Activity mSearchActivity;;
public SearchLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchLayout(Context context) {
super(context);
}
public static void setSearchActivity(Activity searchActivity) {
mSearchActivity = searchActivity;
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
if (mSearchActivity != null &&
event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
mSearchActivity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
}
น่าเสียดายที่ฉันไม่สามารถรับเครดิตทั้งหมดได้ หากคุณตรวจสอบแหล่งที่มาของ Android สำหรับช่อง SearchDialog อย่างรวดเร็วคุณจะเห็นว่าแนวคิดนี้มาจากที่ใด