Tôi đã triển khai Android search widget
trong ứng dụng dựa trên navigation drawer
của mình. Tôi đã đặt nó để mở bàn phím và lấy nét editText
khi nhấp vào biểu tượng tìm kiếm. Tôi muốn đặt nút quay lại (nút lên) để ẩn bàn phím. Tôi đã tìm kiếm trên web cho số R.id
của nút lên và tìm thấy số này android.R.id.home
. Vì vậy, tôi đã đặt nó vào được:Đặt chức năng nút quay lại tiện ích tìm kiếm
@Override
public boolean onOptionsItemSelected(MenuItem item) {
...
case android.R.id.home:
hideKeyboard();
break;
...
}
tôi sửa lỗi mã và nhận thấy rằng cách nhấp vào biểu tượng navigation bar
cháy lên android.R.id.home
, nhưng nhấn lên nút của widget tìm kiếm thậm chí không nhập onOptionsItemSelected(MenuItem item)
chức năng. Tôi cũng đã thử điều này:
searchView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
});
Nhưng không hoạt động.
Làm cách nào để ẩn bàn phím khi nhấn nút quay lại (lên)?
Thiết quan điểm tìm kiếm:
private void setSearchView(Menu menu) {
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
Home.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
}
Bạn đã kiểm tra chuỗi này chưa? [OnView của FindView không hoạt động] (http://stackoverflow.com/questions/9327826/searchviews-oncloselistener-doesnt-work/12975254#12975254). Tuy nhiên, phần thứ hai, tác giả đó không nên dựa vào 'android.R.id.home' là hoàn toàn đúng đắn. –