一、自动完成文本框的定义:
自动完成文本框是在输入框中根据输入信息提示与其相关信息的UI控件。
二、如何使用自动完成文本框:
【ListView】
1.系统提供的Adapter填充数据
2.自定义Adapter填充数据
【AutoCompleteTextView】
1.系统提供的Adapter填充数据
2.自定义Adapter填充数据
三、使用系统提供的Adapter填充数据:
1)编写布局
2)准备数据
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_text_view);
// 准备数据
String[] books = new String[]{“rollen”,"rollenholt", "rollenren", "roll"};
}
3)关联数据
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_text_view);
// 准备数据
String[] books = new String[]{“rollen”,"rollenholt", "rollenren", "roll"};
// 关联数据
ArrayAdapter av = new
ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, books);
AutoCompleteTextView auto = findViewById(R.id.auto_complete_textview);
auto.setAdapter(av);
}
4)查看效果
四、使用自定义Adapter填充数据
1)编写布局-Activity
2)编写布局-提示项列表
3)准备数据-Adapter
public class AutoCompleteTextViewActivity extends AppCompatActivity {
private ArrayList data = new ArrayList<>();
private String[] emails = {"@qq.com", "@163.com", "@sina.com", "@gmail.com"}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_text_view);
}
…
}
4)关联数据
class SearchEmailAdapter extends BaseAdapter {
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
class ViewHolder {
TextView tvEmail;
}
}
5)关联数据-Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(AutoCompleteTextViewActivity.this)
.inflate(R.layout.email_search_layout, null);
holder.tvEmail = convertView.findViewById(R.id.tv_email_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvEmail.setText(data.get(position));
return convertView;
}
class SearchEmailAdapter extends BaseAdapter implements Filterable {
public Filter getFilter() {
Filter filter = new Filter() {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList newData = new ArrayList<>();
if (constraint != null && !constraint.toString().contains("@")) {
for (String data : emails) {
newData.add(constraint + data);
}
}
results.values = newData;
results.count = newData.size();
return results;
}
protected void publishResults(CharSequence constraint, FilterResults results) {
data = (ArrayList) results.values;
notifyDataSetChanged();
}![在这里插入图片描述](https://img-blog.csdnimg.cn/20190215091225522.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc3ODcyMA==,size_16,color_FFFFFF,t_70)
};
return filter;
}
}
6)关联数据-Activity
public class AutoCompleteTextViewActivity extends AppCompatActivity {
private ArrayList data = new ArrayList<>();
private String[] emails = {"@qq.com", "@163.com", "@sina.com", "@gmail.com"}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_complete_text_view);
AutoCompleteTextView auto = findViewById(R.id.auto_complete_textview);
auto.setAdapter(new SearchEmailAdapter());
}
…
}
7)运行效果
五、自动完成文本框的属性方法:
clearListSelection() 清除所有的提示列表项
getAdapter() 取得数据集
setAdapter() 设置数据集
setOnClickListener() 设置单击事件
setOnItemClickListener() 在选项上设置单击事件
setOnItemSelectedListener() 选项选中时的事件