Android widget -- autocompletetextview learning notes

Source: Internet
Author: User

Autocompletetextview:

An editable text view that shows completion suggestions automatically while the user is typing. the list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box.

The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/DPAD center key.

The list of suggestions is obtained from a data adapter and appears only after a given number of characters definedthe threshold.

That is, a textview that can be entered. However, it is bound with some initial data. After a user inputs a part of the characters (the default value is 2 characters, it automatically matches the bound content and displays the matching results in the following menu. You can click the option in a drop-down menu to complete the input.

Important attributes:

Android: completionthreshold = "1", defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu. That is, set the number of characters entered by the user before matching, and a drop-down box appears.

Example 1:

Autocomplete_1.xml

 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/autocomplete_1_instructions"/> </P> <p> <linearlayout <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/autocomplete_{country"/> </P> <p> <autocompletetextview Android: Id = "@ + ID/edit" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> </P> <p> </linearlayout> </P> <p> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/autocomplete_shortfocus"/> </P> <p> </linearlayout>

Autocomplete1.java

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

 

Example 2:

Autocomplete_4.xml

 <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "@ string/autocomplete_4_instructions"/> </P> <p> <linearlayout <br/> Android: orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "@ string/autocomplete_4_name"/> </P> <p> <autocompletetextview Android: id = "@ + ID/edit" <br/> Android: completionthreshold = "1" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> </P> <p> </linearlayout> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/autocomplete_4_message"/> </P> <p> </linearlayout>

Autocomplete4.java

 Public class autocomplete4 extends activity {<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. autocomplete_4); </P> <p> contentresolver content = getcontentresolver (); <br/> cursor = content. query (contacts. people. content_uri, <br/> lele_projection, null, null, contacts. people. default_sort_order ); <Br/> contactlistadapter adapter = new contactlistadapter (this, cursor); </P> <p> autocompletetextview textview = (autocompletetextview) <br/> findviewbyid (R. id. edit); <br/> textview. setadapter (adapter); <br/>}</P> <p> // XXX compiler bug in javac 1.5.0 _ 07-164, we need to implement filterable <br/> // to make Compilation Work <br/> Public static class contactlistadapter extends cursoradapter impl Ements filterable {<br/> Public contactlistadapter (context, cursor c) {<br/> super (context, c); <br/> mcontent = context. getcontentresolver (); <br/>}</P> <p> @ override <br/> Public View newview (context, cursor, viewgroup parent) {<br/> final layoutinflater Inflater = layoutinflater. from (context); <br/> final textview view = (textview) Inflater. inflate (<br/> android. r. lay Out. simple_dropdown_item_1line, parent, false); <br/> View. settext (cursor. getstring (5); <br/> return view; <br/>}</P> <p> @ override <br/> Public void BindView (view, context context, cursor) {<br/> (textview) view ). settext (cursor. getstring (5); <br/>}</P> <p> @ override <br/> Public String converttostring (cursor) {<br/> return cursor. getstring (5); <br/>}</P> <p> @ override <br/> Public cursor runqueryonbackgroundthread (charsequence constraint) {<br/> If (getfilterqueryprovider ()! = NULL) {<br/> return getfilterqueryprovider (). runquery (constraint); <br/>}</P> <p> stringbuilder buffer = NULL; <br/> string [] ARGs = NULL; <br/> If (constraint! = NULL) {<br/> buffer = new stringbuilder (); <br/> buffer. append ("upper ("); <br/> buffer. append (contacts. contactmethods. name); <br/> buffer. append (") glob? "); <Br/> ARGs = new string [] {constraint. tostring (). touppercase () + "*" };< br/>}</P> <p> return mcontent. query (contacts. people. content_uri, people_projection, <br/> buffer = NULL? Null: buffer. tostring (), argS, <br/> contacts. people. default_sort_order); <br/>}</P> <p> private contentresolver mcontent; <br/>}</P> <p> Private Static final string [] people_projection = new string [] {<br/> contacts. people. _ id, <br/> contacts. people. primary_phone_id, <br/> contacts. people. type, <br/> contacts. people. number, <br/> contacts. people. label, <br/> contacts. people. name, <br/>}; <br/>}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.