Có, hoàn toàn có thể hiển thị và ẩn bàn phím và chặn các cuộc gọi đến nút quay lại. Đó là một chút nỗ lực hơn vì nó đã được đề cập là không có cách trực tiếp nào để thực hiện việc này trong API. Điều quan trọng là ghi đè boolean dispatchKeyEventPreIme(KeyEvent)trong một bố cục. Những gì chúng tôi làm là tạo bố cục của chúng tôi. Tôi đã chọn RelativeLayout vì nó là cơ sở cho Hoạt động của tôi.
<?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">
Bên trong Activity của chúng tôi, chúng tôi thiết lập các trường đầu vào và gọi setActivity(...)hàm.
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);
}
Rõ ràng, initInputField()hàm thiết lập trường đầu vào. Nó cũng cho phép phím enter để thực thi chức năng (trong trường hợp của tôi là tìm kiếm).
@Override
public void onBackPressed() {
DataHelper.cancelSearch();
hideKeyboard();
super.onBackPressed();
}
Vì vậy, khi onBackPressed()được gọi trong bố cục của chúng ta, chúng ta có thể làm bất cứ điều gì chúng ta muốn như ẩn bàn phím:
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInputField.getWindowToken(), 0);
}
Dù sao, đây là ghi đè của tôi đối với 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);
}
}
Thật không may, tôi không thể nhận tất cả các tín dụng. Nếu bạn kiểm tra nguồn Android cho hộp SearchDialog nhanh, bạn sẽ thấy ý tưởng đến từ đâu.