Android AutoCompleteTextView使用

AutoCompleteTextView是一个android提供的可以补全输入的textview,效果类似于PC端百度一下那个输入框下面会带出来搜索历史。

先看下效果图:
在这里插入图片描述
我采用了自定义adapter的方式给他增加了删除的功能(单击x会删除那一条记录)
结合上SharedPerferences很容易就实现了输入历史记录提示功能。

实现步骤:
xml布局中添加AutoCompleteTextView

<AutoCompleteTextView
            android:id="@+id/actv_text"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入服务器地址"
            android:layout_margin="5dp"
            android:singleLine="true"
            android:imeOptions="actionNext"/>

Activity中对其进行设置

		//我自定义的适配器下面有代码
        AutoAdapter adapter = new AutoAdapter(this,R.layout.rv_item_auto,list);
        //也可以使用系统自带的  
        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,list);
        acText.setAdapter(adapter);
        acText.setThreshold(1);//设置输入几个字符后开始出现提示 默认是2
        //我想在控件准备输入时直接弹出 所以设置了onFocusChange事件
        acText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){//获取焦点时
                    acText.showDropDown();
                }
            }
        });

在写自定义适配器的时候一定要实现Filterable,要不然会失效。
其他地方和正常写Baseadapter是一样的

适配器的布局代码很简单 一个textview(显示提示内容)一个imageview(显示x)就不再贴代码了
AutoAdpater自定义适配器的代码

public class AutoAdapter extends BaseAdapter implements Filterable {

    Context mContext;
    List<String> mList;
    List<String> data;
    int mResource;


    public AutoAdapter(@NonNull Context context, int resource, List<String> list) {
        mContext = context;
        mList = list;
        mResource = resource;
    }

    @Override
    public int getCount() {
        return mList==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(mResource,null);
        TextView tvAuto = view.findViewById(R.id.tv_auto);
        ImageView ivClose = view.findViewById(R.id.iv_delete);
        tvAuto.setText(mList.get(position));
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.remove(position);
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                List<String> list = new ArrayList<>();
                if(constraint != null){
                    for (String s:mList) {
                        list.add(s);
                    }
                }
                results.count = list.size();
                results.values = list;
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                data = (List<String>) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }
}
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
AutocompleteTextView is a subclass of EditText view in Android that shows suggestions automatically while the user is typing. It is commonly used in search functionalities and other similar scenarios. To use AutocompleteTextView in Android Studio, you first need to add the AutoCompleteTextView to your layout XML file. Here's an example: ``` <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter a city name" android:textColorHint="#999" android:completionThreshold="1" android:popupBackground="@color/white" android:dropDownVerticalOffset="10dp" android:elevation="2dp"/> ``` In the above example, `completionThreshold` attribute specifies the minimum number of characters required to trigger the suggestion popup. `popupBackground` attribute specifies the background color of the suggestion popup, and `dropDownVerticalOffset` attribute specifies the vertical offset of the suggestion popup. To provide suggestions to the AutocompleteTextView, you need to set an adapter that provides data to the suggestion popup. Here's an example: ``` AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView); String[] cities = {"New York", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, cities); autoCompleteTextView.setAdapter(adapter); ``` In the above example, we create an array of cities and provide it to the ArrayAdapter, which then sets the adapter to the AutocompleteTextView. Now, when the user types in the AutocompleteTextView, it will show suggestions based on the provided data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值