用于标记用户的Android CustomAutoCompleteTextView

时间:2014-12-21 16:03:53

标签: android autocompletetextview

当我插入@来标记用户时,我需要一个向我显示建议的EditText,你能告诉我一种实现它的方法吗?

1 个答案:

答案 0 :(得分:0)

你可以使用编辑文本和它下面的列表视图(或listPopupWindow) 1-创建mainActivity类(注意:为editText定义一个文本观察器,如果editText中的字符串以“@”开头,则将此字符串发送到适配器的过滤器方法,稍后解释)

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

ListPopupWindow listPopupWindow;
EditText myEditText;
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> matchedTitles = new ArrayList<String>();
myAdapter mAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getActionBar().hide();
    myEditText = (EditText) findViewById(R.id.myedittext);
    listPopupWindow = new ListPopupWindow(this);
    titles.add("aaaa");
    titles.add("bbbbb");
    titles.add("abc");
    titles.add("adc");
    titles.add("bbs");
    titles.add("cdf");
    mAdapter = new myAdapter(this,titles);

    listPopupWindow.setAdapter(mAdapter);
    listPopupWindow.setAnchorView(myEditText);
    listPopupWindow.setWidth(300);
    listPopupWindow.setHeight(400);

    listPopupWindow.setModal(true);
    listPopupWindow.setOnItemClickListener(this);
    myEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String input = myEditText.getText().toString();
            if(input.contains("@") && input.lastIndexOf("@")<input.length()-1) {
               List<String> result = mAdapter.filter(input.substring(input.lastIndexOf("@")+1),titles);
                matchedTitles = (ArrayList<String>) result;
                if(result!=null && result.size()>0)
                    listPopupWindow.show();
            }


        }
    });





    }

@Override
public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    String input =  myEditText.getText().toString();
    String prevouisText = input.substring(0,input.lastIndexOf("@"));
    String newInput= prevouisText + matchedTitles.get(position);
    myEditText.setText(newInput);
    listPopupWindow.dismiss();
    myEditText.setSelection(myEditText.getText().toString().length());
}

}

为listpopupwidnow(或listview)创建你的costum适配器,但是你可以创建一个类似filter的方法,如下所示获取所有数据(这里是标题)和key(editText中的输入数据)并返回必须匹配的数据在列表中显示:

public class myAdapter  extends ArrayAdapter<String>{

List<String> titles;
ArrayList<String> matchedTitles = new ArrayList<String>();
Context context;
public static final int resource  = R.layout.list_item;
public myAdapter(Context context,List<String> objects) {
    super(context, resource, objects);
    titles = objects;
    this.context = context;
}

public List<String> filter(String key,List<String> allTitles){
    matchedTitles.clear();
    for(String s : allTitles){

        if(s.startsWith(key))
            matchedTitles.add(s);

    }

    titles = matchedTitles;
    notifyDataSetChanged();
    return  matchedTitles;
}


@Override
public int getCount() {
    return  titles.size();
}

@Override
public String getItem(int position) {
    return titles.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater lf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null){
        convertView =  lf.inflate(resource,parent,false);

        ViewHolder vh = new ViewHolder(position,convertView);
        convertView.setTag(vh);

    }
    else{
        View v =  lf.inflate(resource,parent,false);
        ViewHolder vh = (ViewHolder) convertView.getTag();
        vh.setData(position,convertView);

    }
    return  convertView;
}

private class ViewHolder{


    TextView title_view;
    public ViewHolder(int position,View v){
        title_view = (TextView) v.findViewById(R.id.title_view);
        setData(position,v);

    }
    public  void setData(int position, View v){


        title_view.setText(matchedTitles.get(position));
    }
}



}

在我的程序中:activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>



<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/myedittext"
    android:layout_centerHorizontal="true" />

和list_item.xml:

  <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/title_view" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="6dp" android:textSize="15dp" android:textStyle="bold" />

希望这有助于你;)

screenshot of this program on emulator