Hello Views之Auto Complete(yaozq翻译,仅供参考)

简介: 为了实现一个具有自动完成提示的文本输入框,可以使用AutoCompleteTextView组件(widget)。

为了实现一个具有自动完成提示的文本输入框,可以使用AutoCompleteTextView组件(widget)。组件中用来提示的字符串集合是通过ArrayAdapter实现的。

在这个教程中,你将创建一个为国家的名字提供提示的AutoCompleteTextView。具体步骤如下:

1,创建一个名为HelloAutoComplete的新项目。

2,创建一个名为list_item.xmlXML文件,保存在res/layout目录下,并按照下面的方式对其进行编辑:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000">
</TextView>

这个文件定义了一个简单的用在suggestions列表中的每一itemTextView

3,打开res/layout/main.xml这个文件,并插入如下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="5dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Country" />
    <AutoCompleteTextView android:id="@+id/autocomplete_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>
</LinearLayout>

文件中的TextVIewAutoCompleteTextView组件的label

4,打开HelloAutoComplete.java文件,并在onCreate()插入如下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
    textView.setAdapter(adapter);
}
代码中先将 main.xml 设为 content view ,又通过 findViewById(int) 方法来获得 AutoCompleteTextView 组件。然后一个新的 ArrayAdapter 被初始化,来把 list_item.xml 布局文件与字符数组 COUNTRIES 中的每一个 item 绑定起来。最后,通过调用 setAdapter() 来吧 ArrayAdapter AutoCompleteTextView 组件联系在一起,这样字符串数组中的字符便会存在于 suggestions 列表中。

5,在HelloAutoComplete类中,增加一个字符数组:

static final String[] COUNTRIES = new String[] {
  "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
  "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
  "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
  "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
  "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
  "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
  "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
  "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
  "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
  "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
  "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic",
  "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea",
  "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland",
  "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia",
  "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
  "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau",
  "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary",
  "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica",
  "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",
  "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
  "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands",
  "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova",
  "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
  "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand",
  "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas",
  "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru",
  "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",
  "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena",
  "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon",
  "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal",
  "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
  "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea",
  "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden",
  "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas",
  "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
  "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",
  "Ukraine", "United Arab Emirates", "United Kingdom",
  "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan",
  "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara",
  "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
};


当用户向AutoCompleteTextView中键入时,弹出的一个drop-down suggestions列表中的内容就是由上面的字符串数组提供的。

6,运行这个程序,将会出现如下结果:



更多内容More Information

注意使用硬编码的字符串数组并不是一种值得推荐的编码方式,因为你的程序代码应该focus on behavior,而不是content。诸如strings这样的application content不应该在代码中具体化,这样做的好处是使修改和定位content更容易。教程中的对strings的硬编码(hard-coded)只是为了方便和便于把焦点放在AutoCompleteTextView widget上。而你的程序应该在XML文件中声明这样的Strings数组。在你的项目的res/vlues/strings.xml文件中,可以通过<string-array>来完成。比如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries_array">
        <item>Bahrain</item>
        <item>Bangladesh</item>
        <item>Barbados</item>
        <item>Belarus</item>
        <item>Belgium</item>
        <item>Belize</item>
        <item>Benin</item>
    </string-array>
</resources>

为了在ArrayAdapter中使用这些字符串资源,可以使用如下的方式来代替原来的ArrayAdapter构造器:

String[] countries = getResources().getStringArray(R.array.countries_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, countries);


相关文章
【错误记录】Tinker 热修复示例运行报错 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
【错误记录】Tinker 热修复示例运行报错 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
307 0
【错误记录】Tinker 热修复示例运行报错 ( Execution failed for task ‘:app:tinkerProcessD‘ . tinkerId is not set!!! )
|
6月前
|
弹性计算 缓存 Serverless
Serverless 应用引擎操作报错合集之阿里函数计算中我打开sd时遇到错误,信息为"Function instance exited unexpectedly(code 1, message:operation not permitted) with start command ' '."如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
302 6
|
JavaScript
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
Vue 打包后打开为空白页面 并且控制台报错‘Failed to load resource: net::ERR_FILE_NOT_FOUND’
|
6月前
|
异构计算 Docker Python
ModelScope问题之加载模型时报错size mismatch如何解决
ModelScope镜像是指用于在ModelScope平台上创建和管理的容器镜像,这些镜像包含用于模型训练和推理的环境和依赖;本合集将说明如何使用ModelScope镜像以及管理镜像的技巧和注意事项。
141 0
|
SQL Java 数据库连接
错误:FUNCTION simple_notebook.count does not exist.解决方法
错误:FUNCTION simple_notebook.count does not exist.解决方法
144 0
Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
423 0
Appium问题解决方案(9)- Original error: Failed to launch Appium Settings app: Condition unmet after 5090 ms
|
数据可视化 Python
【pypi开源项目文档】终极秘诀应对rst解析错误:The description failed to render in the default format of reStructuredText
前面雷学委给大家缕一缕了如何开发一个python库并导入运行, 基本完成了一个初步的库,还支持了命令行工具的发布。
472 0
【pypi开源项目文档】终极秘诀应对rst解析错误:The description failed to render in the default format of reStructuredText
|
Java
会员卡模板修改接口(alipay.marketing.card.template.modify)JAVA版本demo
说明:    该接口主要用于创建之后的会员卡的模板修改,官方接口文档请点击【会员卡模板修改】    创建模板接口请参考我的这个帖子:[url]https://openclub.alipay.com/read.
669 0
|
SQL 关系型数据库 MySQL
组复制官方翻译九、Group Replication Technical Details
https://dev.mysql.com/doc/refman/8.0/en/group-replication-technical-details.html 这一章主要描述MGR的更多细节 18.
1767 0
下一篇
无影云桌面