Android中AutoCompleteTextView完整示例(二)


MainActivity如下:

package cc.testautocompletetextview2;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述
* 利用自定義AutoCompleteTextView完成郵箱自動補全功能
*
* 參考資料:
* 1 http://blog.csdn.net/fx_sky/article/details/9326129
* 2 http://blog.csdn.net/jwzhangjie/article/details/15771953
* 3 http://my.eoe.cn/eoe_haozi/archive/519.html
* 4 http://gundumw100.iteye.com/blog/1056728
* Thank you very much
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

}


AutoCompleteTextViewSubClass如下:

package cc.testautocompletetextview2;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class AutoCompleteTextViewSubClass extends AutoCompleteTextView {
//可補全的郵箱后綴
private static final String[] emailSuffix = { "@qq.com","@gmail.com", "@sina.com","@sohu.com"};
public AutoCompleteTextViewSubClass(Context context){
super(context);
init(context);
}
public AutoCompleteTextViewSubClass(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

private void init(Context context){
final AutoCompleteTextViewAdatper adapter = new AutoCompleteTextViewAdatper(context);
setAdapter(adapter);
//設置閾值
//等同於布局文件中的 android:completionThreshold="2"
//setThreshold(2);
addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
String inputString = editable.toString();
System.out.println("--->輸入的內容 inputString="+inputString);
adapter.mList.clear();
if (inputString.length() > 0) {
for (int i = 0; i < emailSuffix.length; ++i) {
adapter.mList.add(inputString + emailSuffix[i]);
}
}
adapter.notifyDataSetChanged();
showDropDown();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {

}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
}
});
}



//定義AutoCompleteTextView的Adapter繼承自BaseAdapter且實現Filterable接口
class AutoCompleteTextViewAdatper extends BaseAdapter implements Filterable {
List<String> mList;
private Context mContext;
private FilterSubClass mFilter;

public AutoCompleteTextViewAdatper(Context context) {
mContext = context;
mList = new ArrayList<String>();
}

@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}

@Override
public Object getItem(int position) {
return mList == null ? null : mList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
TextView textView = new TextView(mContext);
textView.setTextColor(Color.RED);
textView.setTextSize(30);
convertView = textView;
}
TextView textView = (TextView) convertView;
textView.setText(mList.get(position));
return textView;
}

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

// 只有滿足setThreshold(2)這個閾值時才會執行performFiltering()
private class FilterSubClass extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
System.out.println("--->過濾 constraint=" + constraint.toString());
FilterResults results = new FilterResults();
if (mList == null) {
mList = new ArrayList<String>();
}
results.values = mList;
results.count = mList.size();
return results;
}

@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
}


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"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定義AutoCompleteTextView使用示例"
android:layout_centerHorizontal="true"/>

<cc.testautocompletetextview2.AutoCompleteTextViewSubClass
android:id="@+id/autoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:completionHint="請選擇:"
android:dropDownVerticalOffset="2dip"
android:dropDownHorizontalOffset="2dip"
android:completionThreshold="2"
/>

</RelativeLayout>




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com