Use of Android autocompletetextview, spinner, and listview

Source: Internet
Author: User

This article mainly describes the use of the autocompletetextview, spinner, and listview controls.

 

(1) autocompletetextview

Autocompletetextview automatically completes the text box. It inherits from edittext and can input text like edittext. However, it can pop up a smart prompt drop-down list based on the text you enter, in this way, you can select the corresponding options, similar to the drop-down list function shown in the text in the search box.

 

Create a new project and modify 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"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "country"/> <autocompletetextview Android: Id = "@ + ID/edit" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: completionthreshold = "1"/> </linearlayout>

 

JavaCodeAs follows:

Public class autocompletedemo extends activity {final string [] countries = new string [] {"Afghanistan", "Australia", "America", "Canada", "Chile ", "China" };@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); autocompletetextview textview = (autocompletetextview) findviewbyid (R. id. edit); arrayadapter <string> adapter = new arrayadapter <string> (this, android. r. layout. simple_dropdown_item_1line, countries); textview. setadapter (adapter );}}

 

Here, a string array of country names is defined. autocompletetextview searches for matched country names based on user input in this array. This can be defined in RES/values/strings. XML, which is defined in the Code for demonstration convenience. Define arrayadapter <string> In oncreate () to bind each country name to the built-in layout item simple_dropdown_item_1line of Android, and associate the arrayadapter <string> object with the autocompletetextview object.

 

Autocompletetextview has the attribute Android: completionthreshold, which indicates that a drop-down list is displayed after a few characters are entered. The default value is 2. The running effect is as follows:

 

 

(2) Spinner

The drop-down list of the spinner, similar to the dropdownlist in. net.

Create a new project and modify Res/layout/Main. XML:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: padding = "10dip" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margintop = "10dip" Android: text = "Please select a country: "/> <spinner Android: Id =" @ + ID/Spinner "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: drawselectofftop = "true"/> </linearlayout>

 

The Java code is as follows:

Public class spinnerdemo extends activity {final string [] countries = new string [] {"Afghanistan", "Australia", "America", "Canada", "Chile ", "China" };@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); spinner S = (spinner) findviewbyid (R. id. spinner); arrayadapter <string> adapter = new arrayadapter <string> (this, android. r. layout. simple_spinner_item, countries); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); S. setadapter (adapter );}}

 

The code is similar to the previous one. Here, the setdropdownviewresource () of the arrayadapter object is called to set the drop-down list to the built-in simple_spinner_dropdown_item of Android. The running effect is as follows:

 

(3) listview

Listview is the list displayed in vertical scrolling mode. There are two ways to implement listview:

 

Create a new project and modify Res/layout/Main. XML:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <listview Android: id = "@ + ID/listview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: drawselectid Top = "false"/> </linearlayout>

 

The Java code is as follows:

 public class listviewdemo extends activity {final string [] countries = new string [] {" Afghanistan "," Australia "," America "," Canada "," Chile ", "China"}; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview = (listview) findviewbyid (R. id. listview); arrayadapter 
  
    adapter = new arrayadapter 
   
     (this, android. r. layout. simple_list_item_1, countries); listview. setadapter (adapter) ;}
   
  

The code is similar to the above spinner. Another way is to use listactivity. listactivity contains a listview. If your activity is to display a list, then, you can let your activity inherit the listactivity directly, or even generate a full screen list without setting the layout. To customize the layout, you must set the Android: Id = "@ Android: ID/List" of listview so that listactivity can know the corresponding listview.

The main. XML Code is as follows:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <listview Android: id = "@ Android: ID/List" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: drawselectid Top = "false"/> </linearlayout>

 

The Java code is as follows:

Public class listviewdemo2 extends listactivity {final string [] countries = new string [] {"Afghanistan", "Australia", "America", "Canada", "Chile ", "China"}; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); arrayadapter <string> adapter = new arrayadapter <string> (this, android. r. layout. simple_list_item_1, countries); setlistadapter (adapter );}}

 

The running effect is as follows:

 

I hope this article will help you.ArticleAs follows:

The main. Out. xml file is generated during Android program debugging.

Use git and repo to obtain the android source code

Introducing Android

Hello android

Android project structure (project structure)

Android message prompt box and dialog box

Android layout Layout

 

Reference books: Beginning Android 2 and Android official documents

Related Article

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.