Android 不要在AutoCompleteTextView中打开下拉列表
我将此代码用于AutoCompleteTextView——因此当您删除最后一个字符以显示列表时。但是mAutoCompleteTextView.showDropDown()不要工作Android 不要在AutoCompleteTextView中打开下拉列表,android,autocomplete,Android,Autocomplete,我将此代码用于AutoCompleteTextView——因此当您删除最后一个字符以显示列表时。但是mAutoCompleteTextView.showDropDown()
mAutoCompleteTextView.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) {
if (s.toString().equals("")) {
mAutoCompleteTextView.showDropDown();
}
}
});
初始化小部件时,我记得类字段中的适配器。
使用此选项设置适配器
mAdapter=(ArrayAdapter)actv.getAdapter();//mAdapter是一个类字段
actv.setText(“Tim Hortons”);
actv.setSelection(0,actv.getText().length());
actv.setAdapter((ArrayAdapter)null);//关闭适配器
actv.requestFocus();
Handler=newhandler(){
公共无效handleMessage(消息消息消息){
((AutoCompleteTextView)msg.obj).setAdapter(mAdapter);
};
Message msg=mHandler.obtainMessage();
msg.obj=actv;
handler.sendMessageDelayed(msg,200);
希望这对你有帮助
为什么将代码放在PostTextChanged中?不在ContextChanged中?不要在ContextChanged中工作可能是因为s.toString从不匹配“”或者,当textfield为空时,可能不会触发PostTextChanged。通常它至少需要1个字符才能启动。是否要在删除所有字符时显示列表?是-如果所有字符都已删除,我想显示列表确定将此属性添加到textview mContactTxt.setFocusable(false);mContactTxt.setFocusableInTouchMode(false);mContactTxt.setText(“”);mContactTxt.setFocusable(true);mContactTxt.setFocusableInTouchMode(true);
mAdapter = (ArrayAdapter<String>)actv.getAdapter(); // mAdapter is a class field
actv.setText("Tim Hortons");
actv.setSelection(0, actv.getText().length());
actv.setAdapter((ArrayAdapter<String>)null); // turn off the adapter
actv.requestFocus();
Handler handler = new Handler() {
public void handleMessage(Message msg) {
((AutoCompleteTextView)msg.obj).setAdapter(mAdapter);
};
Message msg = mHandler.obtainMessage();
msg.obj = actv;
handler.sendMessageDelayed(msg, 200);