Android drop-down list box, text box, menu, and android list box
1. drop-down list box (Spinner)
Project Layout
<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" tools:context=".MainActivity" > <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>
Add code:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Spinner spinner = (Spinner) findViewById (R. id. spinner); ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_spinner_item); // The R file adapter in the system sdk. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); adapter. add ("java"); adapter. add (". net "); adapter. add ("php"); spinner. setAdapter (adapter );}}
<AutoCompleteTextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" <! -- CompletionThreshold: Specifies at least a few characters before the automatic prompt function is displayed --> android: completionThreshold = "1" android: id = "@ + id/name"/>
The code is relatively simple:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); String [] names = {"Lao Zhang", "Lao Fang", "Lao Bi", "Li Ming", "Li", "Chen Jiang", "abc ", "acc"}; AutoCompleteTextView nameText = (AutoCompleteTextView) this. findViewById (R. id. name); ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, names); nameText. setAdapter (adapter );}}
Run the project:
3. Input Multiple times-Automatically complete the text box (MultiAutoCompleteTextView)
In addition to the AutoCompleteTextView control, we can also use the MultiAutoCompleteTextView control to complete the continuous input function. That is to say, after a string is input, enter a comma (,) next to the string. There can be any number of spaces before and after the comma, and then enter another string, the automatic prompt list is displayed.
When using MultiAutoCompleteTextView, You need to specify the MultiAutoCompleteTextView. CommaTokenizer class object instance for its setTokenizer method. This object indicates that the comma is used as the separator of multiple input strings.
<MultiAutoCompleteTextView android: layout_width = "fill_parent" android: layout_height = "wrap_content"
<! -If you specify at least a few characters for completionThreshold, the system will automatically prompt you. --> android: completionThreshold = "1" android: id = "@ + id/name"/>
The Code is as follows:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); String [] names1 = {"Lao Zhang", "Lao Fang", "Lao Bi", "Li Ming", "Li", "Chen Jiang", "abc ", "acc"}; MultiAutoCompleteTextView nameText1 = (MultiAutoCompleteTextView) this. findViewById (R. id. name1); ArrayAdapter <String> adapter1 = new ArrayAdapter <String> (this, android. r. layout. simple_dropdown_item_1line, names1); nameText1.setAdapter (adapter1); nameText1.setTokenizer (new MultiAutoCompleteTextView. commaTokenizer ());}}
Running effect:
4. Menu)
Rename the main. xml file in the res/menu folder to menu. xml and add the following code:
<Menu xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: id = "@ + id/item1" android: title = "I am menu 1"> </item> <item android: id = "@ + id/item2" android: title = "I am menu 2"> </item> <item android: id = "@ + id/item3" android: title = "I am menu 3"> </item> </menu>
The logic code is as follows:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Override public boolean onCreateOptionsMenu (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 boolean onOptionsItemSelected (MenuItem item) {// Add an entry Click Event int id = item. getItemId (); switch (id) {case R. id. item1: Toast. makeText (this, "Entry 1 is clicked", 0 ). show (); break; case R. id. item2: Toast. makeText (this, "entry 2 is clicked", 0 ). show (); break; case R. id. item3: Toast. makeText (this, "entry 3 is clicked", 0 ). show (); break; default: break;} return super. onOptionsItemSelected (item );}}
The project running effect is as follows: