我有一個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