Android tutorial (15th) -- Use of the automatic prompt box

Source: Internet
Author: User

In this chapter, we will talk about the use of AutoCompleteTextView in the automatic prompt box.

I believe that when we use Baidu, Google, and other search engines, we enter a word in the input box, and related prompts will appear below the input box.

In fact, this function will bring us a lot of convenience. Let's see how it is implemented.

Step 1: Create a project Ep. AutoCompleteTextView.

Step 2: design the view.


[Java]
<RelativeLayout xmlns: 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"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
 
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
 
<AutoCompleteTextView
Android: id = "@ + id/autoCompleteTextView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "16dp"
Android: EMS = "10">
 
<RequestFocus/>
</AutoCompleteTextView>
 
</RelativeLayout>

<RelativeLayout xmlns: 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"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>

<AutoCompleteTextView
Android: id = "@ + id/autoCompleteTextView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "16dp"
Android: EMS = "10">

<RequestFocus/>
</AutoCompleteTextView>

</RelativeLayout>
Step 3: Write the core file.


[Java]
Package com. example. ep. autocompletetextview;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;
 
Public class MainActivity extends Activity {
 
Private AutoCompleteTextView atct;
Private static final String [] autoStrs = new String [] {"J", "Jo", "Jov", "Jove", "Joven "};
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Atct = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1 );
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. R. layout. simple_dropdown_item_1line, autoStrs );
Atct. setAdapter (adapter );
}
 
}

Package com. example. ep. autocompletetextview;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. widget. ArrayAdapter;
Import android. widget. AutoCompleteTextView;

Public class MainActivity extends Activity {

Private AutoCompleteTextView atct;
Private static final String [] autoStrs = new String [] {"J", "Jo", "Jov", "Jove", "Joven "};

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Atct = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1 );
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. R. layout. simple_dropdown_item_1line, autoStrs );
Atct. setAdapter (adapter );
}

}

 

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.