原帖地址:http://blog.sina.com.cn/s/blog_54109a5801012pmi.html


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 = new ArrayList<String>();
result.add("1111111");
result.add("1222222");
mPlace.setAdapter(new ArrayAdapter<String>(
MP002TopActivity.this,
android.R.layout.simple_dropdown_item_1line,
result)
);

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

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

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

就必须重载AutoCompleteTextView这个控件。

public class MyAutoCompleteTextView extends AutoCompleteTextView{

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

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

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

@Override
public boolean enoughToFilter() {
return true;
}

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

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

}

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

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

更多相关文章

  1. Android列表控件Spinner简单用法示例
  2. 高级控件1
  3. Android ImageView控件的MaxWidth、MaxHeight不起作用
  4. android中列表选项、单选选项、多选选项对话框的使用
  5. android 圆形头像—— android开源系列:CircleImageView自定义圆
  6. Android 日期和时间选择控件的开发
  7. Android中自定义控件之飞入飞出布局及随机布局实现方式
  8. 完美解决Android Studio在写XML布局的时候没有了控件代码提示的
  9. Android 子控件高度超出父布局的限制

随机推荐

  1. greendao接入sql和android cursor的简单
  2. android 文件上传
  3. Android(安卓)使用AlarmManager设置闹钟
  4. Writing an Android(安卓)Sync Provider
  5. android一种较为复杂的布局参考(xml文件)
  6. android setComponentEnabledSetting
  7. appium启动APP配置参数:
  8. android获取屏幕信息
  9. Android(安卓)Cursor遍历
  10. Activity切换 窗口绘制显示