Java 按下软键盘上的Enter键时不触发事件
我想在软键盘上按enter键时触发搜索,但它不起作用。如果在物理键盘上(通过emulator)按enter键,则工作正常。也许我是错的听众Java 按下软键盘上的Enter键时不触发事件,java,android,Java,Android,我想在软键盘上按enter键时触发搜索,但它不起作用。如果在物理键盘上(通过emulator)按enter键,则工作正常。也许我是错的听众 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { readJson(); final View view= inflater.inflate
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
readJson();
final View view= inflater.inflate(R.layout.fragment_main, container, false);
EditText search = (EditText) view.findViewById(R.id.search_view);
search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_GO ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (!event.isShiftPressed()) {
int found=0;
String inserted = v.getText().toString();
for(int i=0;i<city_list.size();i++){
if(inserted.equalsIgnoreCase(city_list.get(i).get("name").toString())){
found=1;
TextView id = (TextView) view.findViewById(R.id.city_id);
id.setText(city_list.get(i).get("id").toString());
TextView lat = (TextView) view.findViewById(R.id.city_lat);
lat.setText(city_list.get(i).get("lat").toString());
TextView lon = (TextView) view.findViewById(R.id.city_long);
lon.setText(city_list.get(i).get("lon").toString());
}
}
if(found==0) {
Toast.makeText(getContext(), "City not found", Toast.LENGTH_SHORT).show();
}
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return true; // consume.
}
}
return false; // pass on to other listeners.
}
});
尝试将以下代码添加到您的
R.id.search\u视图中
:
android:imeOptions="actionSearch"
android:inputType="text"
使用setOnKeyListener?我以前试过,但也不起作用
android:imeOptions="actionSearch"
android:inputType="text"