我有一个EditText
,它附带有一个ListPopupWindow
,并附有最近列出的搜索查询。当用户点击EditText
时,弹出窗口应该显示,然后当用户开始输入时,弹出窗口应该消失。如何在继续使用时使用EditText onClick方法
这主要是使用OnFocusChangeListener
,OnTextChangedListener
和OnEditorActionListener
。但是,如果用户点击EditText
,开始键入,然后再次点击EditText
,我需要弹出窗口回来。我曾尝试使用OnClickListener
而不是OnFocusChangeListener
,但无法使用OnClickListener
显示弹出窗口。
如何让ListPopUpWindow
显示使用时点击EditText
是否已经有焦点?
searchBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean gainFocus) {
//onFocus
if (gainFocus) {
popUpWindow.show();
}
}
});
要关闭一旦用户开始输入弹出的EditText上现场
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();
}
});
要明确重点,解除弹出,并执行搜索
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;
}
});
这并没有为我工作。我也确保setFocusableInTouchMode为true。 – audiophile121