何谓autoXXX 其实说白了就是自动提示 大家在上百度或者谷歌(需要翻墙)这类的搜索引擎网站的时候 比如
我输入android s... 他就会弹出一个android studio这类的提示文 这就是网页版的autoXXX 你可以这么看做 当然
html端的当然不是这个名字 不过功能相近 同样在移动端也同样存在这样的需求 而且很多 因此掌握autoXXX
成了开发的必修课 来看下autoXXX这个组件
前面咱说过 如果想快速的掌握一个组件的使用 你可以去baidu google 但是个人更加推荐的是从谷歌开发者的官网上去
学 来看下官方的一些说明。
截取一下
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
这段代码可不是我写的哦 这里官方提供了写法可以看到 auto插件是基于适配器模式的
而下面设置了一些简单的数据
然后把国家的数据显示出来
然后咱们接着看 此插件提供了两个内部类接口
interface AutoCompleteTextView.OnDismissListener
interface AutoCompleteTextView.Validator
有人可能会纳闷 这两东西是干嘛的 首先第一个
看一个词 dismiss假如你不清楚这个词的意思 你也应该知道 我们在前面讲对话框的时候 调用过这个
方法 干嘛的 简单的来说就是alert的finish 那么既然是消失的接口 肯定有一个消失的方法咯 实践是检验真理的唯一标准
果然 此接口里只有一个方法
比如我们做一个搜索功能 但是一般情况是我们比如输入 XXXXXX(此处略去N个字)
然后一按回车 或者搜索按钮搜索开始 但是也有比如说我不按搜索就搜索的情况 有人可能要说了
这不是脑残么 我想说的是需求是千变万化的 恩 仅此而已。
看下一个接口
This interface is used to make sure that the text entered in this TextView complies to a certain format. Since there is no foolproof way to prevent the user from leaving this View with an incorrect value in it, all we can do is try to fix it ourselves when this happens.
这句话有点长 有人可能不理解这是什么意思 没关系 咱们就看一下他的字面意思
valitor 这是什么 前面有个makesure 根据我们的经验 我们应该知道 这哥们应该是确保什么 或者检验什么 没错 这个接口就是个验证器
既然是验证器 那么自然会有一个boolean返回的方法咯 来让我们看看这里的推理是否正确
abstract CharSequence |
fixText(
CharSequence invalidText)
Corrects the specified text to make it valid.
|
abstract boolean |
isValid(
CharSequence text)
Validates the specified text.
|
假如你还是不了解的话 你可以讲这玩意想象成一种墙 咳咳 言多必失。
上面还有一个方法叫做 fixText 也就是改正你的text 有人可能会纳闷 这还需要改正?
我们可能会遇到这么一种情况 比如我们快速的去输一些词 但是假如咱写错一个 搜索引擎会提示 您要输入的是不是XXX
这就是一种校验加改正的功能 返回的是一个charSeXXX你可以将其就看作为改正后的text
现在咱就有一种概念 假如!isValid(text) 调用fixText(text)然后继续回调直到通过
再看一个 前面有人有疑问 fill-parent 和 match-parent明明是一个东西那么为什么会出现两个呢
有人直到是历史问题 但是具体是什么版本 恐怕咱还不清楚 来看这个
Constant | Value | Description |
---|---|---|
fill_parent | -1 | The dropdown should fill the width of the screen. This constant is deprecated starting from API Level 8 and is replaced by match_parent . |
match_parent | -1 | The dropdown should fit the width of the screen. Introduced in API Level 8. |
wrap_content | -2 | The dropdown should fit the width of its anchor. |
API8 这就够了