我的主活动代码:
// 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”并在后面按空格时,建议就会消失。 我希望这些建议能够保持在那里。 我已经尝试了各种方法,但没有成功。 有人可以帮忙吗?编辑: 请问有人可以在它们的模拟器上尝试此代码吗?只需在xml中添加一个
AutoCompleteTextView
,然后运行即可。