I have an EditText
that has a ListPopupWindow
attached to it with recent search queries listed. When a user clicks the EditText
, the popup should show, then when the user starts typing, the popup should disappear.
This is mostly working using OnFocusChangeListener
, OnTextChangedListener
, and OnEditorActionListener
. However, if a user has clicked on the EditText
, began to type, then clicks the EditText
again, I need the popup to come back up. I have tried using an OnClickListener
instead of an OnFocusChangeListener
but can never get the popup to show with an OnClickListener
.
How can I get the ListPopUpWindow
to show when the use clicks the EditText
if it already has focus?
searchBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean gainFocus) {
//onFocus
if (gainFocus) {
popUpWindow.show();
}
}
});
To dismiss popup once user start typing in EditText field
searchBox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
popUpWindow.dismiss();
}
});
To clear focus, dismiss popup, and execute search
searchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & v.getImeOptions()) == actionId) {
if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
if (searchBox.getText().toString().length() == 0) return true;
searchBox.clearFocus();
popUpWindow.dismiss();
fetchResults();
Util.hideKeyboard(v);
return true;
}
}
return false;
}
});
You can try replacing the focus change listener with an on touch listener and showing the popup when a MOTION_UP event occurs:
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_UP) {
// Show popup here
}
return false;
}
});
I found the problem. It turns out I wasn't actually using an EditText
object, but rather a custom class called MaterialEditText
that provides a wrapper around an EditText
object to give it Material UI features. The class passes the listeners I had been using onto it's EditText member, however, it was not doing so for either OnClickListener
or OnTouchListener
, so these listeners were never getting set for the EditText
object. Once I added methods to pass these listeners on to the EditText
object, I fixed my issue by keeping the code listed above and adding the following:
searchBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show Popup
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.