У меня есть EditText, и я хочу поймать событие, когда пользователь использует «Готово» или «Ввод». В настоящее время я тестирую на эмуляторе с Pixel API 26
. Я перепробовал множество решений, найденных в StackOverFlow, таких как добавление setSingleLine или редактирование XML с помощью
android:singleLine="true"
android:inputType="text"
android:maxLines="1""
, но ничего не работает. Я действительно не знаю, в чем проблема.
Это XML:
<EditText
android:id="@+id/input_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/ic_magnify"
android:background="@null"
android:hint="Enter Address, City or Zip Code"
android:imeOptions="actionGo|actionSearch|actionNext|actionSend"
android:singleLine="true"
android:inputType="text"
android:maxLines="1"
android:textColor="#000"
android:textSize="15sp" />
В моем классе MapFragment я получаю ссылку на EditBox внутри "onCreateView"function:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.activity_maps, container,
false);
mSearchText = v.findViewById(R.id.input_search);
mSearchText.setSingleLine();
init();
// Hiding the action bar
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
return v;
}
и затем в функции "init" я делаю это:
private void init() {
Log.d("Yolo2", "Before");
//Search field
mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_SEARCH
|| actionId == EditorInfo.IME_ACTION_DONE
|| keyEvent.getAction() == KeyEvent.ACTION_DOWN
|| keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){
Log.d("Yolo2", "Yes");
//execute our method for searching
geoLocate();
}
Log.d("Yolo2", "No");
return false;
}
});
}
Я перепробовал много других событий, и ни одно из них не сработало. Я ничего не получаю от журналов отладки. Я пытался отследить события смены ключа и набрать ключ внутри EditText.