[英]Enter key on softkeyboard not triggering event when pressed
I want to trigger the searching when I press enter from softkeyboard, but it isn't working. 我想在按下软键盘上的回车键时触发搜索,但是它不起作用。 It works fine if the key enter is pressed on a physical keyboard (through emulator).
如果在物理键盘上通过仿真器按了Enter键,则可以正常工作。 Perhaps I'm the wrong listener?
也许我听错了声音?
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.
}
});
Tried also: 也尝试过:
search.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
keyCode == KeyEvent.KEYCODE_ENTER)
Try adding the following code to your R.id.search_view
: 尝试将以下代码添加到您的
R.id.search_view
:
android:imeOptions="actionSearch"
android:inputType="text"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.