AutoCompleteTextView自動提示輸入框問題

實際上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>

效果圖
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章