eclipse auto publish中AutoCompleteTextView自动完成输入框为什么不显示字体和内容

eclipse中AutoCompleteTextView自动完成输入框为什么不显示字体和内容_百度知道
提问者采纳
你是想说自动提示吧要配置适配器的,另外,还要设置输入多少个字符触发器下拉提示如果你想总是显示下拉的话,要修改适配器的过虑器
适配器怎么配置?
提问者评价
其他类似问题
为您推荐:
其他1条回答
可能被你删掉了,自己加上就可以了
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁当前访客身份:游客 [
:楼主有没有demo啊。一些初始数据不知道设多少
:写的不错,赞
:多谢! 解决了我自定义link与内置link共存的问题...
:支持一下
:请问 orientation 变量从何而来。如何对其复制...
:楼主,我最近在处理一个问题,把你这三篇文章全部...
:楼主的博文写的很棒,解释的很详细清楚,不知道何...
:楼主为什么不直接继承自LinearLayout呢?...
:学习了,
今日访问:2
昨日访问:23
本周访问:50
本月访问:89
所有访问:27099
使用AutoCompleteTextView实现邮箱地址补全
发表于2年前( 21:45)&&
阅读(989)&|&评论()
0人收藏此文章,
最近学习android时,用到AutoCompleteTextView,感觉挺有意思,于是模仿着网易邮箱地址补全的效果也实现了一个。
AutoCompleteTextView是一个具有自动补全功能的EditView,当用户输入数据后,AutoCompleteTextView就会将用户输入的数据与他自己的adapter中的数据对比,如果用户数据与adapter中的某条数据的开始部分完全匹配,那么adapter中的这条数据就会出现在下拉提示框中。
例如:adapter中有3条数据“abc”,“hjk”,“abd”,而用户输入“ab”,那么下拉提示框中将会出现“abc”和“abd”。(AutoCompleteTextView默认在用户输入两个字符之后才提示,可以通过setThreshold(1)来将它设置为用户输入1个字符后就开始提示)
AutoCompleteTextView在匹配用户输入数据时,会调用performFiltering方法,将用户数据传入,并调用adapter的filter来处理。
因为当用户选中下拉列表中的某一项时,AutoCompleteTextView会使用该项对应的adapter中的数据来填充文本域,这与我们这边的需求不太相同,因为我们的adapter中只有类似于“@”的email地址后缀,下拉框中的数据是我们将用户输入和adapter中的数据拼接而成的。因此我们需要重写replaceText方法,以使AutoCompleteTextView来在用户选中某一项时,用我们指定的文本来填充文本域。
然后我们需要为AutoCompleteTextView设置OnFocusChangeListener来在用户移开焦点后,进行email地址格式检查,并且在再次获得焦点后重启提示功能。
代码如下:(EmailAutoCompleteTextView.java)
public class EmailAutoCompleteTextView extends AutoCompleteTextView {
private static final String TAG = "EmailAutoCompleteTextView";
private String[] emailSufixs = new String[] { "@",
"@", "@" };
public EmailAutoCompleteTextView(Context context) {
super(context);
init(context);
public EmailAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
public EmailAutoCompleteTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init(context);
public void setAdapterString(String[] es) {
if(es != null && es.length & 0)
this.emailSufixs =
private void init(final Context context) {
//adapter中使用默认的emailSufixs中的数据,可以通过setAdapterString来更改
this.setAdapter(new EmailAutoCompleteAdapter(context, R.layout.auto_complete_item, emailSufixs));
//使得在输入1个字符之后便开启自动完成
this.setThreshold(1);
this.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
String text = EmailAutoCompleteTextView.this.getText().toString();
//当该文本域重新获得焦点后,重启自动完成
if(!"".equals(text))
performFiltering(text, 0);
//当文本域丢失焦点后,检查输入email地址的格式
EmailAutoCompleteTextView ev = (EmailAutoCompleteTextView)
String text = ev.getText().toString();
//这里正则写的有点粗暴:)
if(text != null && text.matches("^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$")) {
Toast to = new Toast(context);
ImageView i = new ImageView(context);
i.setBackgroundResource(R.drawable.img_success);
to.setView(i);
to.show();
Toast toast = Toast.makeText(context, "邮件地址格式不正确", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 50);
toast.show();
protected void replaceText(CharSequence text) {
//当我们在下拉框中选择一项时,android会默认使用AutoCompleteTextView中Adapter里的文本来填充文本域
//因为这里Adapter中只是存了常用email的后缀
//因此要重新replace逻辑,将用户输入的部分与后缀合并
Log.i(TAG + " replaceText", text.toString());
String t = this.getText().toString();
int index = t.indexOf("@");
if(index != -1)
t = t.substring(0, index);
super.replaceText(t + text);
protected void performFiltering(CharSequence text, int keyCode) {
//该方法会在用户输入文本之后调用,将已输入的文本与adapter中的数据对比,若它匹配
//adapter中数据的前半部分,那么adapter中的这条数据将会在下拉框中出现
Log.i(TAG + " performFiltering", text.toString() + "
" + keyCode);
String t = text.toString();
//因为用户输入邮箱时,都是以字母,数字开始,而我们的adapter中只会提供以类似于"@"
//的邮箱后缀,因此在调用super.performFiltering时,传入的一定是以"@"开头的字符串
int index = t.indexOf("@");
if(index == -1) {
if(t.matches("^[a-zA-Z0-9_]+$")) {
super.performFiltering("@", keyCode);
this.dismissDropDown();//当用户中途输入非法字符时,关闭下拉提示框
super.performFiltering(t.substring(index), keyCode);
private class EmailAutoCompleteAdapter extends ArrayAdapter&String& {
public EmailAutoCompleteAdapter(Context context, int textViewResourceId, String[] email_s) {
super(context, textViewResourceId, email_s);
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(TAG, "in GetView");
View v = convertV
if (v == null)
v = LayoutInflater.from(getContext()).inflate(
R.layout.auto_complete_item, null);
TextView tv = (TextView) v.findViewById(R.id.tv);
String t = EmailAutoCompleteTextView.this.getText().toString();
int index = t.indexOf("@");
if(index != -1)
t = t.substring(0, index);
//将用户输入的文本与adapter中的email后缀拼接后,在下拉框中显示
tv.setText(t + getItem(position));
Log.i(TAG, tv.getText().toString());
} activity的xml文件如下:
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
&com.example.testautocompletetextview.EmailAutoCompleteTextView
android:id="@+id/act"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Address"
android:textColor="@color/black" /&
&!-- 用于测试移开焦点 --&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:drawableLeft="@drawable/amount_selected" /&
&/LinearLayout& 下拉提示框中每一项(TextView)的xml:
&?xml version="1.0" encoding="utf-8"?&
&TextView xmlns:android="/apk/res/android"
android:id="@+id/tv"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" /&
提示截图:
更多开发者职位上
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读2703人阅读
AutoCompleteTextView是android中自带的实现自动匹配输入的一个控件!具体用法如下:
&1.Activity文件:&&& import android.app.A&&& import android.os.B&&& import android.widget.ArrayA&&& import android.widget.AutoCompleteTextV
&&& public class AutocompleteTV extends Activity {&&& &&& String names[]={"zhangsan","lisi","wangwu","ligang"};&&& public void onCreate(Bundle savedInstanceState) {&&&&&&& super.onCreate(savedInstanceState);&&&&&&& setContentView(R.layout.main);&&&&&&& AutoCompleteTextView autoTV=(AutoCompleteTextView)findViewById(R.id.autocomplete);&&&&&&& ArrayAdapter&String& contact=new ArrayAdapter&String&(this,&&&&& android.R.layout.simple_dropdown_item_1line,names ); &&&&&&& autoTV.setAdapter(contact);&&&&&&& &&&&&& }& }
2.main.XML布局文件:&&&&& &?xml version="1.0" encoding="utf-8"?&&&&& &&LinearLayout xmlns:android=""&&&&&&& &android:orientation="vertical"&&&&&&& &android:layout_width="fill_parent"&&&&& && android:layout_height="wrap_content" &&& &&&&& && &AutoCompleteTextView android:id="@+id/autocomplete"&&&&&& &&&&&&&&&&&&& android:layout_width="fill_parent" &&&&&&&&&&&& &android:layout_height="wrap_content" &&&&&&&&&&&& &android:completionThreshold="1"&&&&&&&&&&&& &android:hint="@string/input"& /&&&&&&&& &/LinearLayout&3.String文件
&&& &&?xml version="1.0" encoding="utf-8"?&&&&&&&resources&&&&&&&&& &&string name="input"&请输入联系人&/string&&&&&&&& &&string name="app_name"&AutoCompleteTV&/string&&&& &&&& &/resources&
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:661097次
积分:7719
积分:7719
排名:第1239名
原创:140篇
转载:249篇
评论:141条
(7)(15)(3)(8)(6)(3)(27)(22)(25)(4)(6)(7)(7)(2)(2)(13)(8)(3)(2)(2)(11)(2)(3)(3)(14)(6)(7)(11)(1)(6)(9)(19)(14)(3)(7)(10)(3)(4)(7)(6)(14)(13)(4)(4)(22)(3)(6)(6)MultiAutoCompleteTextView和AutoCompleteTextView两个自动完成功能控件的使用 -
- ITeye技术网站
博客分类:
MultiAutoCompleteTextView
可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配
可用在发短信,发邮件时选择联系人这种类型当中。
使用时需要执行设置分隔符方法
AutoCompleteTextView
支持基本的自动完成功能,适用在各种搜索功能中,并且可以根据自己的需求设置他的默认显示数据
两个控件都可以很灵活的预置匹配的那些数据,并且可以设置输入多少值时开始匹配等等功能
主布局文件
multi_textview.xml
&MultiAutoCompleteTextView
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/multi_txt"&&/MultiAutoCompleteTextView&
&AutoCompleteTextView
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/auto_txt"&&/AutoCompleteTextView&
数据适配布局文件
multi_item.xml
&TextView android:id="@+id/subject"
xmlns:android="/apk/res/android"
android:layout_width="fill_parent" android:layout_height="30px"
android:singleLine="true"
android:ellipsize="end" /&
string.xml
&resources&
&string-array name="mutilstring"&
&item&test1&/item&
&item&test2&/item&
&item&test3&/item&
&/string-array&
&/resources&
执行数据绑定
//数据适配准备
ArrayAdapter&String& adapter = new ArrayAdapter&String&(this,
R.layout.multi_item, getResources().getStringArray(
R.array.mutilstring));
//多匹配的自动完成
MultiAutoCompleteTextView mtxt = (MultiAutoCompleteTextView) findViewById(R.id.multi_txt);
//设置适配
mtxt.setAdapter(adapter);
// 设置输入多少字符时自动匹配
mtxt.setThreshold(2);
// 设置分隔符
mtxt.setTokenizer(maTokenizer());
//单一的自动完成
AutoCompleteTextView auto_txt = (AutoCompleteTextView) findViewById(R.id.auto_txt);
auto_txt.setAdapter(adapter);
浏览 11421
mingnjintian
浏览: 40717 次
来自: 长沙
[list][*][list][*][*][list][*][ ...
使用匿名引用的Bitmap会造成内存泄漏吧。Bitmap底层调 ...
恩,不错。应该加上这个方法的解说的:
public voi ...}

我要回帖

更多关于 eclipse没有edittext 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信