Android输入框自动提示进阶--自定义布局

发现Android的有两种方法AutoCompleteTextViewMultiAutoCompleteTextView提示出来的提示框只是纯文字而且是单条数据,要是想实现加一个图片或者是每一条数据展示两个数据呢,这就需要重写适配器设置布局了

重写适配器:

package com.example.administrator.autocompletetextviewdemo;

/*
 * @Comment :
 * @Created :2018/10/26 14:11
 * @Name : YJ
 */

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weihao on 2018/5/28.
 * 继承BaseAdapter,实现Filterable
 */

public class AutoEditTextAdapter extends BaseAdapter implements Filterable {
    private ArrayFilter mFilter;
    private List<String> mNameList;
    private List<String> mAddressList2;
    private Context context;
    private OnItemClickListener mOnItemClickListener;
    private ArrayList<String> mUnfilteredData;

    public interface OnItemClickListener{
        void onClick(int position);
        void onLongClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this. mOnItemClickListener=onItemClickListener;
    }

    public AutoEditTextAdapter(List<String> mNameList, List<String> mAddressList2, Context context) {
        this.mNameList = mNameList;
        this.context = context;
        this.mAddressList2 = mAddressList2;
    }

    @Override
    public int getCount() {

        return mNameList == null ? 0 : mNameList.size();
    }

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

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = View.inflate(context, R.layout.item, null);
            holder = new ViewHolder();
            holder.mName = (TextView) view.findViewById(R.id.name);
            holder.mAddress=view.findViewById(R.id.address);
            holder.mClick=view.findViewById(R.id.click);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        //item的点击事件
        if( mOnItemClickListener!= null){
            holder.mClick.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(position);
                }
            });
            holder.mClick.setOnLongClickListener( new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
        String pc = mNameList.get(position);
        String pc2=mAddressList2.get(position);
        holder.mName.setText(pc);
        holder.mAddress.setText(pc2);


        return view;
    }

    static class ViewHolder {
        public TextView mName;
        public TextView mAddress;
        public LinearLayout mClick;

    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mUnfilteredData == null) {
                mUnfilteredData = new ArrayList<String>(mNameList);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<String> list = mUnfilteredData;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<String> unfilteredValues = mUnfilteredData;
                int count = unfilteredValues.size();

                ArrayList<String> newValues = new ArrayList<String>(count);

                for (int i = 0; i < count; i++) {
                    String pc = unfilteredValues.get(i);
                    if (pc != null) {

                        if (pc != null && pc.startsWith(prefixString)) {

                            newValues.add(pc);
                        } else if (pc != null && pc.startsWith(prefixString)) {

                            newValues.add(pc);
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            mNameList = (List<String>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }
}

item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/click"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:padding="10dp"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/name"
            android:text="name"
            android:textSize="15sp"
            android:textColor="#000"
            android:layout_marginBottom="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/address"
            android:text="address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f2f2f2"/>
</LinearLayout>

然后在MainyActivity里面把之前的适配器替换掉就行,有一点要注意,由于现在变成了每一条有两个数据,所以要新加一个集合来展示

package com.example.administrator.autocompletetextviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView mAutoCompleteTextView;
    private MultiAutoCompleteTextView mMultiAutoCompleteTextView;
    private String[] ceshi1;
    private List<String> mNamelist;
    private List<String> mAddresslist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAutoCompleteTextView=findViewById(R.id.autocomplete);
        mMultiAutoCompleteTextView=findViewById(R.id.multiautocomplete);
        ceshi1 = new String[]{"北京西站", "北京北站", "北京站", "北京南站", "北京大使馆",
                "北京故宫", "天津电视塔", "天津南站", "天津东站", "天津之眼", "天津劝业场","aaa","bbb",
                "aaa北京","北京aaa","aaaa8989","bbb768","北京ab","sss","aaaaa"};
//        ArrayAdapter adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_activated_1,ceshi1);
        //为上面添加数据
        mNamelist= Arrays.asList(ceshi1);
        mAddresslist=new ArrayList<>();
        //为下面添加数据
        for (int i=0;i<mNamelist.size();i++){
            mAddresslist.add("下面的地址"+i);
        }
        AutoEditTextAdapter adapter=new AutoEditTextAdapter(mNamelist,mAddresslist,MainActivity.this);
        mAutoCompleteTextView.setAdapter(adapter);
        mMultiAutoCompleteTextView.setAdapter(adapter);
        mMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        //提示列表的点击事件
        adapter.setOnItemClickListener(new AutoEditTextAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                mAutoCompleteTextView.setText(mNamelist.get(position));//把点击到的列表赋值给输入框
                mAutoCompleteTextView.dismissDropDown();//关闭提示框
            }
            //长按事件
            @Override
            public void onLongClick(int position) {

            }
        });
        setEdit();
    }
    /**
     * 点击输入框的时候自动弹出提示框
     * */
    public void setEdit(){
        mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mAutoCompleteTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mAutoCompleteTextView.showDropDown();
                    }
                });
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
}

最后MainActivity布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <MultiAutoCompleteTextView
        android:id="@+id/multiautocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:hint="MultiAutoCompleteTextView" />

    <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="AutoCompleteTextView" />
</LinearLayout>

下载地址:https://download.csdn.net/download/lanrenxiaowen/10746730

问题相关:Android输入框自动提示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值