AutoCompleteTextView是自动完成输入内容控件

其常用属性定义如下

<AutoCompleteTextView
android:id="@+id/mp002_top_place_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_marginTop="5dp">
</AutoCompleteTextView>

其中android:completionThreshold定义了从第几个字符开始显示候补列表

默认值为2

使用例:

AutoCompleteTextView mPlace =(AutoCompleteTextView)findViewById(R.id.mp002_top_place_input);

ArrayList<String> result = newArrayList<String>();
result.add("1111111");
result.add("1222222");
mPlace.setAdapter(newArrayAdapter<String>(
MP002TopActivity.this,
android.R.layout.simple_dropdown_item_1line,
result)
);

局限性是completionThreshold设定的最小值是1

小于1的情况下,会默认变成1。

所以要在不输入任何字符的条件下显示候补列表

就必须重载AutoCompleteTextView这个控件。

public class MyAutoCompleteTextView extendsAutoCompleteTextView{

public MyAutoCompleteTextView(Contextcontext) {
super(context);
}

public MyAutoCompleteTextView(Contextcontext, AttributeSet attrs) {
super(context,attrs);
}

public MyAutoCompleteTextView(Contextcontext, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean enoughToFilter() {
returntrue;
}

@Override
protected void onFocusChanged(boolean focused,int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction,previouslyFocusedRect);

performFiltering(getText(), KeyEvent.KEYCODE_UNKNOWN);
}

}

enoughToFilter()是判断输入文字列长度是否满足现实候补列表的要求的方法。

onFocusChanged()是当控件获得焦点时让其显示候补列表。

更多相关文章

  1. Android通过泛型来简化findViewById
  2. Android(安卓)控件之ImageSwitcher图片切换器
  3. popupwindow与listview结合使用时,点击后退键无效问题
  4. Android(安卓)PopupWindow显示在控件上方或者下方(demo)
  5. Android(安卓)解决异步任务下载图片错位问题
  6. Android(安卓)网络连接是否可用的
  7. Android(安卓)Layout的可视化编辑器Droiddaw
  8. arm上的参数列表传递的分析(以android为例)
  9. 当ScrollView嵌套RecyclerView 或者EditText的时候,进去页面总会

随机推荐

  1. 顶会「扩招」,一地鸡毛:ICLR2020近半数审稿
  2. 英特尔AI医疗实战曝光:10倍加速辅助诊断、
  3. 半年21项全新发布和升级,百度飞桨团队「码
  4. DeepMind星际争霸AI登上Nature,超越99.8%活
  5. 在图像生成领域里,GAN这一大家族是如何生
  6. 顶会抄顶会:SIGIR 2019论文被爆抄袭,部分内
  7. ECC内存与普通内存之间的区别
  8. Shell 流程控制
  9. VB.NET和Python有什么不同?前景分析!
  10. 如何提取视频声音,将视频中的音乐提取出来