本文是笔者翻译了android帮助文档里面的HelloView并且进行了一些分析理解完成的,也是在学习的过程中,把遇到的一些不容易理解的东西,研究明白了,给记下来,个人认为的难点重点吧。废话不说了,进正题,第一篇顺手找的(一天一天的写的,就按照首字母来发吧),是关于Auto Complete的一篇,顾名思义,就是可以自动完成我们输入了一半的词组或者单词。
使用AutoCompleteTextView创建一个文本输入小部件,提供按照建议自动完成的功能。建议内容来自一组与ArrayAdapter相关的字符串。(帮助文档上面的套话)
1、首先创建一个名为HelloAuto的项目。
2、在res/drawable(res/layout)目录下创建一个名为list_item.xml的xml文件。内容如下
<?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:textColor="#000"
android:textSize="16sp" >
</TextView>
刚开始,笔者对这个xml的建立有点迷糊,分不清这个是干嘛用的,其实它就是用来填充那个在我们输入了一半词组的字母时,出现的那个里面含有供我们选择的内容的对话框的。下图红框中的部分
再值得一说的是帮助文档的实例中,说的是放到res/drawable中,但是貌似根本就无法当layou文件用,这里我放到了layout,就没问题了。
3、在res/layout/activity_main.xml
中插入如下内容
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
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:textColor="#0A0A0A"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
4、在HelloAuto中的onCreat()中插入如下代码
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = newArrayAdapter<String>(this,
R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}
使用findViewById找到我们在xml文件中定义的autocomplete
然后为这个autocomplete添加适配器(adapter)
ArrayAdapter的构造函数有三个参数第一个是Context 第二个是一个int值,它索引的是自动完成的下拉列表里面的每个项目(textview)的xml文件,第三个参数是这个下拉列表里面的值所构成的数组。
5、为COUNTRIES数组赋值。
staticfinal String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
"Algeria", "AmericanSamoa", "Andorra", "Angola", "Anguilla",
"Antarctica", "Antigua andBarbuda", "Argentina", "Armenia",
"Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
"Bermuda", "Bhutan", "Bolivia", "Bosnia andHerzegovina",
"Botswana", "BouvetIsland", "Brazil",
"BritishIndian Ocean Territory", "British Virgin Islands",
"Brunei", "Bulgaria", "BurkinaFaso", "Burundi", "Cote
"Cambodia", "Cameroon", "Canada", "CapeVerde", "Cayman
"CentralAfrican Republic", "Chad", "Chile", "China",
"ChristmasIsland", "Cocos(Keeling) Islands", "Colombia",
"Comoros", "Congo", "CookIslands", "CostaRica", "Croatia",
"Cuba", "Cyprus", "CzechRepublic",
"DemocraticRepublic of the Congo", "Denmark", "Djibouti",
"Dominica", "DominicanRepublic", "EastTimor", "Ecuador",
"ElSalvador", "EquatorialGuinea", "Eritrea", "Estonia",
"Ethiopia", "FaeroeIslands", "FalklandIslands", "Fiji",
"Finland", "FormerYugoslav Republic of Macedonia", "France",
"FrenchGuiana", "FrenchPolynesia", "French Southern
"Gabon", "Georgia", "Germany", "Ghana", "Gibraltar",
"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
"Guinea", "Guinea-Bissau", "Guyana", "Haiti",
"HeardIsland and McDonald Islands", "Honduras", "Hong Kong",
"Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq",
"Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
"Liechtenstein", "Lithuania", "Luxembourg", "Macau",
"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
"MarshallIslands", "Martinique", "Mauritania", "Mauritius",
"Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco",
"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
"Nauru", "Nepal", "Netherlands", "NetherlandsAntilles",
"NewCaledonia", "New Zealand", "Nicaragua", "Niger",
"Niue", "NorfolkIsland", "NorthKorea", "NorthernMarianas",
"Norway", "Oman", "Pakistan", "Palau", "Panama",
"Papua NewGuinea", "Paraguay", "Peru", "Philippines",
"PitcairnIslands", "Poland", "Portugal", "PuertoRico",
"Reunion", "Romania", "Russia", "Rwanda", "Sqo Tomeand
"SaintHelena", "SaintKitts and Nevis", "Saint Lucia",
"SaintPierre and Miquelon", "Saint Vincent and the
"Samoa", "SanMarino", "SaudiArabia", "Senegal",
"SierraLeone", "Singapore", "Slovakia", "Slovenia",
"SolomonIslands", "Somalia", "SouthAfrica",
"South Georgiaand the South Sandwich Islands", "South Korea",
"Spain", "SriLanka", "Sudan", "Suriname","Svalbardand Jan en", "Swaziland", "Sweden","Syria", "Taiwan", "Tajikistan",
"TheBahamas", "TheGambia", "Togo", "Tokelau", "Tonga",
"Trinidad andTobago", "Tunisia", "Turkey", "Turkmenistan",
"Turks andCaicos Islands", "Tuvalu", "Virgin Islands",
"Ukraine", "United ArabEmirates", "UnitedKingdom",
"UnitedStates", "UnitedStates Minor Outlying Islands",
"Uzbekistan", "Vanuatu", "VaticanCity", "Venezuela",
"Wallis andFutuna", "WesternSahara", "Yemen", "Yugoslavia",
"Zambia", "Zimbabwe" };