[Transfer]android learning----Basic UI Programming (VII)

Source: Internet
Author: User
Tags home screen

    • Auto-complete input box Autocompletetextview
    • Multi-content Auto-complete input box

Autocompletetextview Auto-Complete input box

Smart Input Box Autocompletetextview

1. Introduction

An editable text view displays auto-complete suggestions when the user types. The suggestion list is displayed in a drop-down menu from which the user can select an item to complete the input. The recommendation list is the data obtained from a data adapter .

2. Important Methods

Clearlistselection (): Clears the selected list item

      Dismissdropdown (): If there is a close drop-down menu

      Getadapter (): Get Adapter

Sample code

① New Project
② Modify the Main.xml layout, add a TextView, a autocompletetextview, a button

<?xml version= "1.0" encoding= "Utf-8"?>
<absolutelayout
Android:id= "@+id/widget0"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Xmlns:android= "Http://schemas.android.com/apk/res/android" >

<textview
Android:id= "@+id/textview_inputshow"
Android:layout_width= "228px"
android:layout_height= "47PX"
android:text= "Please enter"
Android:textsize= "25px"
android:layout_x= "42px"
Android:layout_y= "37PX"
> </TextView>
<Autocompletetextview
Android:id= "@+id/autocompletetextview_input"
Android:layout_width= "275px"
android:layout_height= "Wrap_content"
Android:text= ""
Android:textsize= "18SP"
android:layout_x= "23PX"
Android:layout_y= "98px"
> </AutoCompleteTextView>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_x= "127dip"
android:text= "Empty"
Android:id= "@+id/button_clean"
android:layout_y= "150dip" >
</Button>
</AbsoluteLayout>

③ Modify Mainactivity.java, add auto-complete function

Package ZYF. Ex_ctrl_13me;
Packages used for import
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.AutoCompleteTextView;
Import Android.widget.Button;
Import Android.widget.TextView;

public class Ex_ctrl_13me extends Activity {
Called when the activity is first created.
Defining the class object to use
Private string[]normalstring=new string[] {
"Android", "Android Blog", "Android Market", "Android SDK",
"Android AVD", "BlackBerry", "BlackBerry JDE", "Symbian",
"Symbian Carbide", "Java 2ME", "Java FX", "Java 2EE",
"Java 2SE", "Mobile", "Motorola", "Nokia", "Sun",
"Nokia Symbian", "Nokia Forum", "WindowsMobile", "Broncho",
"Windows XP", "Google", "Google Android", "Google Chrome",
"IBM", "MicroSoft", "Java", "C + +", "C", "C #", "J #", "VB"};
@SuppressWarnings ("unused")
Private TextView Show;
Private Autocompletetextview Autotextview;
Private Button clean;
Private arrayadapter<string> Arrayadapter;

     @Override
    public void OnCreate (Bundle Savedinstancestate) {
        super.oncreate (savedInstanceState);
        //Loading the Home screen layout main.xml
         setcontentview (R.layout.main);
        //Get UI element objects from XML
         show = (TextView) Findviewbyid (r.id.textview_inputshow);
        autotextview = (autocompletetextview) findViewById ( R.id.autocompletetextview_input);
        clean = (Button) Findviewbyid (R.id.button_clean);
       

//Implement an Adapter object to add automatically loaded content to the auto-complete input box
Arrayadapter = new arrayadapter<String> (this,Android. R.layout.simple_dropdown_item_1line , normalstring);
//Add content adapter to auto-complete input box
Autotextview.setadapter (arrayadapter);

Add a Click event handler listener to the empty button

Clean.setonclicklistener (New Button.onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Empty
Autotextview.settext ("");
}
});
}
}

④ Results



Multiautocompletetextview.

Smart Input Box Multiautocompletetextview

1. Introduction

Inherit from Autocompletetextview, extending the length of the Autocompletetextview .

Autocompletetextview can implement simple AutoComplete, and the difference between multicompletetextview is that you can set a separator tag.
The Multicompletetextview is delimited by the public void Settokenizer (Multiautocompletetextview.tokenizer t) function, with a separator tag, Can be implemented in the case of delimiters, the input sentence will be automatically completed multiple times.

Delimited tags : Multiautocompletetextview.tokenizer are used to distinguish between different substrings.

2. Important Methods

Enoughtofilter (): Filter When text length exceeds threshold

   performvalidation (): Instead of validating the entire text, this subclass method validates each individual text tag

   Settokenizer (Multiautocompletetextview.tokenizer t): When the user is entering , thetokenizer setting will be used to determine the text in the relevant range.

Sample code

① New Project

② Modify the Main.xml layout, add a TextView, a multiautocompletetextview, a button

<?xml version= "1.0" encoding= "Utf-8"?>
<absolutelayout
Android:id= "@+id/widget0"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Xmlns:android= "http://schemas.android.com/apk/res/android">
<textview
Android:id= "@+id/textview_inputshow"
Android:layout_width= "228px"
android:layout_height= "47PX"
android:text= "Please enter"
Android:textsize= "25px"
android:layout_x= "42px"
android:layout_y= "37px"/>
<Multiautocompletetextview
Android:layout_width= "275px"
android:layout_height= "Wrap_content"
Android:text= ""
Android:id= "@+id/multiautocompletetextview"
android:layout_x= "23PX"
android:layout_y= "98px"/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_x= "127dip"
android:text= "Empty"
Android:id= "@+id/button_clean"
android:layout_y= "180dip"/>
</AbsoluteLayout>

③multiautocompletetextview is able to automatically complete user input, can add multiple inputs, note the settings tokenizer

Set Tokenizer to determine the relevant range of user input text
Myautocompletetextview. Settokenizer (new Multiautocompletetextview.commatokenizer ());

④ Modify the Mainactivity.java, realize the dynamic automatic completion of user input related content

Package ZYF. Ex_ctrl_13_b;
Packages used for import
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.Button;
Import Android.widget.MultiAutoCompleteTextView;

public class Ex_ctrl_13_b extends Activity {
Called when the activity is first created.
Defining the class object to use
Private string[] normalstring = new string[] {
"Android", "Android Blog", "Android Market", "Android SDK",
"Android AVD", "BlackBerry", "BlackBerry JDE", "Symbian",
"Symbian Carbide", "Java 2ME", "Java FX", "Java 2EE",
"Java 2SE", "Mobile", "Motorola", "Nokia", "Sun",
"Nokia Symbian", "Nokia Forum", "WindowsMobile", "Broncho",
"Windows XP", "Google", "Google Android", "Google Chrome",
"IBM", "MicroSoft", "Java", "C + +", "C", "C #", "J #", "VB"};
Private Button clean;
Private MultiautocompletetextviewMyautocompletetextview;
Private arrayadapter<string>Adapter;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Loading the home screen layout main.xml
Setcontentview (R.layout.main);
Get UI Element object from XML in Findviewbyid ()
Myautocompletetextview = (Multiautocompletetextview) Findviewbyid (R.id.multiautocompletetextview);
Clean = (Button) Findviewbyid (R.id.button_clean);


//new the Arrayadapter object and passes an array of normalstring strings
Implements an adapter object to add automatically loaded content to the AutoComplete input box
Adapter= new arrayadapter<string>( This,Android. R.layout.simple_dropdown_item_1line, normalstring);
//Add Arrayadapter to the Autocompletetextview object
Add a content adapter to the auto-complete input box
Myautocompletetextview.Setadapter(Adapter);
//Set Tokenizer to determine the relevant range of user input text,Tokenizer as an instance of a comma delimited tag (commatokenizer)
Myautocompletetextview.Settokenizer(NewMultiautocompletetextview.Commatokenizer ());
Add a Click event handler listener to the empty button
Clean.setonclicklistener (New Button.onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Empty
Myautocompletetextview.settext ("");
}
});
}
}

⑤ Results



Key points

1. Multiautocompletetextview. Settokenizer

Prototype

Multiautocompletetextview. Settokenizer (multiautocompletetextview.tokenizer t)

sets the separator mark.

Parameters

Multiautocompletetextview.tokenizer : A delimited tag used to distinguish different substrings.

2. Multiautocompletetextview.commatokenizer

A structure
Public Static Class Multiautocompletetextview.commatokenizer

   extends Object

    Implements Multiautocompletetextview.tokenizer

Ii. Overview
Multiautocompletetextview.commatokenizer is a delimited markup component that can be used in a number of lists that contain items that are broken by commas and one or several spaces .

[Transfer]android learning----Basic UI Programming (VII)

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.