Android drop-down list box, text box, menu

Source: Internet
Author: User
Tags ming

1. Drop-down list box (Spinner)

Project layout

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >    <SpinnerAndroid:id= "@+id/spinner"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"          /></Relativelayout>

Add the appropriate code:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Spinner Spinner=(Spinner) Findviewbyid (R.id.spinner); Arrayadapter<String> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_spinner_item); //r file inside the system SDKAdapter.setdropdownviewresource (Android.        R.layout.simple_spinner_dropdown_item); Adapter.add ("Java"); Adapter.add (". Net"); Adapter.add ("PHP");            Spinner.setadapter (adapter); }}

2. Input content Auto-complete text box (Autocompletetextview)

Autocompletetextview and EditText components are similar, you can enter text. However, the Autocompletetextview component can be bound to a string array or List object, and when the user enters two or more characters, the system will list all strings in the string array that begin with the input character under the Autocompletetextview component. This is very similar to the www.google.com search box, and when you enter a string to find, the Google search box lists the most popular list of search strings that begin with the string.

<android:layout_width= "Fill_parent" android:layout_height= "wrap_content" <!–-completionthreshold Specifies that you enter at least a few characters before the auto-prompt function-->android:completionthreshold= "1" android:id= "  />

The code is relatively simple:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); String[] Names= {"Lao Zhang", "Lao Fang", "Lao Bi", "Li Ming", "Lily", "Tanoto", "ABC", "ACC" }; Autocompletetextview Nametext= (Autocompletetextview) This. Findviewbyid (R.id.name); Arrayadapter<String> adapter =NewArrayadapter<string> ( This, Android.        R.layout.simple_dropdown_item_1line, names);    Nametext.setadapter (adapter); }}

To run the project:

3, multiple input-content auto-complete text box (Multiautocompletetextview)

In addition to the Autocompletetextview control, we can also use the Multiautocompletetextview control to complete the function of continuous input. That is, after entering a string, enter a comma (,) after the string, you can have any number of spaces before and after the comma, and then enter a string, and still display the list of automatic prompts.
When using Multiautocompletetextview, You need to specify an instance of the Multiautocompletetextview.commatokenizer class object for its Settokenizer method, which represents a delimiter that uses commas as input for multiple strings.

<android:layout_width= "Fill_parent" android:layout_height= "
<!–completionthreshold specifies that the auto-prompt function will not appear until at least a few characters are entered-->android:completionthreshold= "1" Android:id= "@+id/name"/>

The code is as follows:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); String[] Names1= {"Lao Zhang", "Lao Fang", "Lao Bi", "Li Ming", "Lily", "Tanoto", "ABC", "ACC" }; Multiautocompletetextview NameText1= (Multiautocompletetextview) This. Findviewbyid (R.ID.NAME1); Arrayadapter<String> Adapter1 =NewArrayadapter<string> ( This, Android.        R.layout.simple_dropdown_item_1line, names1);        Nametext1.setadapter (Adapter1); Nametext1.settokenizer (NewMultiautocompletetextview.commatokenizer ()); }}

The effect of running:

4. Menus (menu)

Rename the Main.xml file under the Res/menu folder to Menu.xml, and add the following code:

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android" >    <ItemAndroid:id= "@+id/item1"Android:title= "I am menu 1">    </Item>    <ItemAndroid:id= "@+id/item2"Android:title= "I am menu 2">    </Item>    <ItemAndroid:id= "@+id/item3"Android:title= "I am menu 3">    </Item></Menu>

The code for the logical section is as follows:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Add Entry Click event        intID =Item.getitemid (); Switch(ID) { CaseR.id.item1:toast.maketext ( This, "Entry 1 was clicked", 0). Show ();  Break;  CaseR.id.item2:toast.maketext ( This, "Entry 2 was clicked", 0). Show ();  Break;  CaseR.id.item3:toast.maketext ( This, "Entry 3 was clicked", 0). Show ();  Break; default:             Break; }        return Super. onoptionsitemselected (item); }}

The effect of running the project is as follows:

Android drop-down list box, text box, menu

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.