Android开发学习
Android开发中,经常需要使用像百度等搜索引擎上,搜索自动下拉关联
新建布局文件:activity_main.xml,使用AutoCompleteTextView自动关联,下面介绍自动关联成邮箱地址实例,代码内容:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<AutoCompleteTextView
android:id="@+id/auto_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dip"
android:padding="5dip"
android:hint="请输入您的邮箱"
android:textSize="16sp" />
</RelativeLayout>
实现的MainActivity.java代码如下:
package com.zhengdecai.autocomplete;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.AutoCompleteTextView;
/**
* 自动关联成邮箱地址
*
* @author 郑德才
*
*/
public class MainActivity extends Activity implements TextWatcher {
private AutoCompleteTextView autoview;
private AutoTextViewAdapter adapter;
private static final String[] AUTO_EMAILS = { "@163.com", "@sina.com",
"@qq.com", "@126.com", "@gmail.com", "@apple.com" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoview = (AutoCompleteTextView) findViewById(R.id.auto_tv);
adapter = new AutoTextViewAdapter(this);
autoview.setAdapter(adapter);
autoview.setThreshold(1);// 输入1个字符时就开始检测,默认为2个
autoview.addTextChangedListener(this);// 监听autoview的变化
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String input = s.toString();
adapter.mList.clear();
autoAddEmails(input);
adapter.notifyDataSetChanged();
autoview.showDropDown();
}
/**
* 自动填充邮箱列表
*
* @param input
*/
private void autoAddEmails(String input) {
System.out.println("input-->" + input);
String autoEmail = "";
if (input.length() > 0) {
for (int i = 0; i < AUTO_EMAILS.length; ++i) {
if (input.contains("@")) {// 包含“@”则开始过滤
String filter = input.substring(input.indexOf("@") + 1,
input.length());// 获取过滤器,即根据输入“@”之后的内容过滤出符合条件的邮箱
System.out.println("filter-->" + filter);
if (AUTO_EMAILS[i].contains(filter)) {// 符合过滤条件
autoEmail = input.substring(0, input.indexOf("@"))
+ AUTO_EMAILS[i];// 用户输入“@”之前的内容加上自动填充的内容即为最后的结果
adapter.mList.add(autoEmail);
}
} else {
autoEmail = input + AUTO_EMAILS[i];
adapter.mList.add(autoEmail);
}
}
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
使用到的AutoTextViewAdapter显示关联的列表实例:
package com.zhengdecai.autocomplete;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
public class AutoTextViewAdapter extends BaseAdapter implements Filterable {
public List<String> mList;
private Context mContext;
private MyFilter mFilter;
public AutoTextViewAdapter(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 tv = new TextView(mContext);
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
convertView = tv;
}
TextView txt = (TextView) convertView;
txt.setText(mList.get(position));
return txt;
}
public Filter getFilter() {
if (mFilter == null) {
mFilter = new MyFilter();
}
return mFilter;
}
private class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
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();
}
}
}
}
模拟器运行效果:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。