AutoCompleteTextView诡异之处

最近使用AutoCompleteTextView发现很多诡异之处,简单探究绕开方法
1.使用布局activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
    <AutoCompleteTextView  android:id="@+id/keyWord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5.0dip" android:completionThreshold="1" android:dropDownVerticalOffset="1.0dip" android:hint="请输入关键字" android:imeOptions="actionDone" android:inputType="text|textAutoComplete" android:maxLength="20" android:paddingRight="37.0dip" android:singleLine="true" android:textColor="#000000" android:textSize="16.0sp" />
</RelativeLayout>

方法1

2.MainActivity.java


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

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

public class MainActivity extends Activity implements TextWatcher {
    AutoCompleteTextView autoCompleteTextView;
    ArrayAdapter<String> aAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.keyWord);
        autoCompleteTextView.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(s.toString())) {
            List<String> listString = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                listString.add("谷传" + i);
            }

            aAdapter = new ArrayAdapter<>(MainActivity.this,
                    android.R.layout.simple_dropdown_item_1line, listString);
            autoCompleteTextView.setAdapter(aAdapter);
            aAdapter.notifyDataSetChanged();

        }
    }
}

步骤:
1. 在AutoCompleteTextView输入文本
2. 在TextWatcher.afterTextChanged callback中获取事件
3. 定义Adapter&&给Adapter设置数据
4. AutoCompleteTextView setAdapter
5. 调用Adapter.notifyDataSetChanged
这样就可以对输入的数据进行过滤了!

当然了,只有输入“谷”才有效!

方法二

MainActivity.java


import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

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

public class MainActivity extends Activity {
    AutoCompleteTextView autoCompleteTextView;
    ArrayAdapter<String> aAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.keyWord);
            List<String> listString = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                listString.add("谷传" + i);
            }
            aAdapter = new ArrayAdapter<>(MainActivity.this,
                    android.R.layout.simple_dropdown_item_1line, listString);
        autoCompleteTextView.setAdapter(aAdapter);


    }

}

步骤:
1. 定义Adapter&&给Adapter设置数据
2. AutoCompleteTextView setAdapter
3. 在AutoCompleteTextView输入文本

这样就可以对输入的数据进行过滤了!

当然了,只有输入“谷”才有效!

源码解析:

持续更新中,敬请期待!

你可能感兴趣的:(源码浅析)