我的主要活动代码:
// here you put all your data.
String[] dataArray = { "Amit sharma Kumar", "Hisham Kumar Munner",
"Vineet John Chaturvedi", "Lucky Kumar Verma" };
ArrayList<String> alAutoCompleteList;
AutoCompleteTextView acTV;
ArrayAdapter<String> adapter1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// etAuto = (EditText) findViewById(R.id.etAuto);
acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
// Arraylist
alAutoCompleteList = new ArrayList<String>();
adapter1 = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, alAutoCompleteList);
acTV.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (acTV.enoughToFilter()) {
acTV.showDropDown();
acTV.bringToFront();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
alAutoCompleteList.clear();
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String acText = acTV.getText().toString().trim();
for (String item : dataArray) {
if (item.toLowerCase().contains(acText.toLowerCase())) {
alAutoCompleteList.add(item);
}
}
acTV.setThreshold(4);
acTV.setAdapter(adapter1);
acTV.showDropDown();
}
});
当我搜索“sharma”并按下空格后,建议就会消失。我希望这些建议留在那里。我什么都试过了,但没有成功。有人可以帮忙吗?
编辑:有人可以在他们的模拟器上试试这个代码吗?只需AutoCompleteTextView
在 xml 中添加一个并运行它。