AutoCompleteTextView自动提示输入框问题

本文介绍如何自定义Android的AutoCompleteTextView组件,包括调整下拉框布局、改变显示文本格式及匹配规则等。同时探讨了样式调整的方法,如通过Style设置背景和间距。

实际上AutoCompleteTextView是一个重新定义的edittext,使用edittext+ListPopupWindow来实现自定提示的功能。
listPopupWindow的点击事件,设置获取list中字符给edittext
1)自定义 AutoCompleteTextView的下拉框的布局,重新convertSelectionToString即可,即可以根据自己的自定义的布局,选择需要提示的部分文字给edittext。

重写convertSelectionToString即可

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

    private ListPopupWindow mPopup;

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /** Returns the country name corresponding to the selected item */
    @Override
    protected CharSequence convertSelectionToString(Object selectedItem) {
    /**
    * Each item in the autocompetetextview suggestion list is a hashmap
    * object
    */
    HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
        return hm.get("txt");
    }
}

Acitivity中使用

autoCompleteTextView.addTextChangedListener(new TextWatcherListener());//可以自定义格式
autoCompleteTextView.setOnItemClickListener(this);
autoCompleteTextView.setOnClickListener(this);
autoCompleteTextView.setOnEditorActionListener(this);

2)自定义item的显示文字格式,自定义匹配规则

private class TextWatcherListener implements TextWatcher {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
    int count) {
    //由于项目需要,对输入的字进行处理,即添加域名后缀,不需要的可以不用这个,直接设置adapter
        if (domianName.contains(".")) {
                        return;
                    }
                    whoisList.clear();
                    for (int i = 0; i < suffix.length; i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("txt", domianName + suffix[i]);
                        whoisList.add(map);
                    }
        String[] from = { "txt" };
        int[] to = { R.id.content };
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
        whoisList, R.layout.layout_whois_adapter, from, to);
        autoCompleteTextView.setAdapter(adapter);
        autoCompleteTextView.showDropDown();
    }
}

或者直接设置list,直接设置adapter

SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
whoisList, R.layout.layout_whois_adapter, from, to);
autoCompleteTextView.setAdapter(adapter);

设置点击的事情

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
    //设置点击item之后的操作
    ...
}

设置键盘点击事情

@Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH
                || actionId == EditorInfo.IME_NULL) {
                //具体操作
                ...
        }
        return false;
    }

3) AutoCompleteTextView样式问题
下拉框默认是显示在edittext的正下方,并且宽度与 AutoCompleteTextView的宽度一致,高度与list中的item的总高度一致。(如果要改变下拉框默认的宽高可以进行第二步的设置)

1.可以通过style设置 AutoCompleteTextView的背景,以及下拉框与 AutoCompleteTextView的距离,如下所示:

<style name="custom_autocompelete_textview_style" parent="@android:style/Widget.AutoCompleteTextView">
  <item name="android:popupBackground">@drawable/textcolor_white</item>
  <item name="android:dropDownVerticalOffset">@dimen/widget_spacing_15px</item>
  </style>

查看源码发现下拉框是ListPopupWindow,在构造方法里设置的宽高属性,且为私有变量
2.通过反射调取父类的私有变量来改变下拉框的宽高样式
反射方法如下所示:

public static Object get(Object instance, String variableName)
{
    Class targetClass = instance.getClass().getSuperclass();
    // YourSuperClass 替换为实际的父类名字
    AutoCompleteTextView superInst = (AutoCompleteTextView)targetClass.cast(instance);
    Field field;
    try {
        field = targetClass.getDeclaredField(variableName);
        //修改访问限制
        field.setAccessible(true);
        // superInst 为 null 可以获取静态成员
        // 非 null 访问实例成员
        return field.get(superInst);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

在构造方法中调用如下代码:
这里我设置的是宽是屏幕的宽度,高度是根据warp_content

mPopup=(ListPopupWindow) get(this, "mPopup");
WindowManager wm = (WindowManager) getContext()
  .getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
mPopup.setWidth(width);

即可改变下拉框的宽度

附adapter 子布局
layout_whois_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_listitem"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="@dimen/widget_spacing_72px"
        android:layout_marginLeft="@dimen/widget_spacing_40px"
        android:gravity="center_vertical"
        android:textColor="@drawable/textcolor_gray6"
        android:textSize="@dimen/text_size_32px" />

    <View
        style="@style/setting_line_left"
        android:layout_marginLeft="@dimen/widget_spacing_30px"
        android:layout_marginRight="@dimen/widget_spacing_30px" />

</LinearLayout>

效果图
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值