我面臨一個問題,但無法找到答案。我有一個帶有TextWatcher的editText在數據庫中查找結果列表。我在PopupMenu中顯示結果。它看起來像這樣:PopupMenu避免點擊鍵盤上的解僱
final List<SearchResult> searchResults = new ArrayList<>();
final PopupMenu menu = new PopupMenu(getActivity(), binding.bottomSheetWriteNews.editTextNewsSubCategory);
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
binding
.bottomSheetWriteNews
.editTextNewsSubCategory
.setText(item.getTitle());
return false;
}
});
final Realm realm = Realm.getDefaultInstance();
binding.bottomSheetWriteNews.editTextNewsSubCategory.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchResults.clear();
menu.getMenu().clear();
RealmResults<SubCategory> subCategoriesResult = realm.where(SubCategory.class).contains("name", s.toString(), Case.INSENSITIVE).findAll();
for(SubCategory subCategory : subCategoriesResult) {
searchResults.add(new SearchResult(subCategory.getId(), subCategory.getName(), false));
menu.getMenu().add(subCategory.getName());
}
menu.show();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
我的問題是每一次我在鍵盤上點擊的彈出菜單是解僱而不是鍵入我想要的角色。這對用戶來說很煩人和不可用。我想要點擊鍵盤不要關閉PopupMenu。我知道在官方文檔中它的編寫爲Touching outside of the popup will dismiss it
,但我使用的是Google Keep,它們具有相同的功能(鍵入文本顯示一個PopupMenu來過濾結果),但沒有我的問題。
感謝您的幫助!