[Android basic learning 4] -- Basic control checkbox, spinner, autocompletetextview, datepicker, timepicker

Source: Internet
Author: User

Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

1. Multiple options (checkbox)

Multiple-choice is similar to single-choice. You need to monitor each control.

 

Ii. drop-down list (spinner)

Instance analysis: this instance is mainly used to select the blood type from the drop-down list.

 

[Focus]

1. How to associate the optional content with the drop-down list.

In this example, arrayadapter <string> is used.

2. understanding and application of adapter

API: an adapter object acts as a bridge between an adapterview and the underlying data for that view. the adapter provides access to the data items. the adapter is also responsible for making a view for each item in the data set.

An adapter object acts as a bridge between adapterview and view underlying data, provides access to data items, and is responsible for rendering each data to the corresponding view, it can be understood as the core controller in MVC.

 

The key source code of the instance is as follows:

Private Static final string [] m_countries = {"O", "A", "B", "AB", "other"}; private arrayadapter <string> adapter; m_spinner = (spinner) findviewbyid (R. id. spinner1); // connect the available content to arrayadapter adapter = new arrayadapter <string> (this, android. r. layout. simple_spinner_item, m_countries); // you can specify the style adapter in the drop-down list. setdropdownviewresource (Android. r. layout. simple_dropdown_item_1line); // Add the adapter to m_spinner . Setadapter (adapter); // Add a spinner time listener m_spinner.setonitemselectedlistener (new Spinner. onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {m_textview.settext ("your blood type is:" + m_countries [arg2]); // you can set arg0.setvisibility (view. visible) ;}@ overridepublic void onnothingselected (adapterview <?> Arg0 ){}});

Instance effect:

[Expansion point]

1. adapterview

The spinner is an indirect subclass of adapterview,

API: An adapterview is a view whose children are determined by an adapter.

As a view, its children is determined by an adapter.

Note that the specific adapter subclass interfaces include

Android. widget. listadatper, spinneradapter, and wrapperlistadapter

Abstract implementation classes include

Android. widget. baseadapter, cursoradapter, resourcecursoradapter

Common Implementation classes

Android. widget. simpleadapter,

Android. widget. arrayadapter

Android. widget. simplecursoradapter

Headerviewlistadapter

 

2. arrayadatper

The arrayadapter model can be any custom object.

/***

* Constructor

* @ Param context the current context.

* @ Param textviewresourceid the resource ID for a layout file containing a textview to use when instantiating views.

* @ Param objects the objects to represent in the listview.

*/

Public arrayadapter (context, int textviewresourceid, list <t> objects ){

Init (context, textviewresourceid, 0, objects );

}

For example:

String [] gamecategory = new string [] {fivechess, numberchess, linkchess,

Russiabox };

Gametype [] gamecategory2 = {New gametype (fivechess, "wuziqi "),

New gametype (numberchess, ""), new gametype (linkchess, ""),

New gametype (russiabox, "")};

 

One is a string array and the other is a gametype array.

Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, gamecategory );

Arrayadapter <gametype> adapter = new arrayadapter <gametype> (this, Android. R. layout. simple_spinner_item, gamecategory2 );

 

Different adapters can be bound to the spinner.

 

Private spinner gametypesp;

 

Gametypesp. setadapter (adapter );

 

You can obtain the corresponding value based on the corresponding object type.

 

Object gametypeselectitem = This. gametypesp. getselecteditem ();

If (gametypeselectitem instanceof gametype ){

If (gametypeselectitem! = NULL ){

This. seletedgametype = (gametype) gametypeselectitem). getgamekey ();

} Else {

Log. D ("gamecategory onclick", "gametypeselectitem is null ");

}

}

 

If it is a string object, it is directly as follows:

Object gametypeselectitem = This. gametypesp. getselecteditem (). tostring ();

The expected value must be returned for the tostring () method of gametype.

 

3. Automatic Display controls (autocompletetextview and multiautocompletetextview)

This control allows users to dynamically query input content when entering data, similar to the dynamic query results of Google and Baidu.

 

Key source code of the instance:

Private Static final string [] autostring = new string [] {"A2", "abf", "Abe", "ABCDE", "abc2", "abcd3 ", "abcde2", "abc2", "abcd2", "abcde2"};/association keyword arrayadapter <string> adapter = new arrayadapter <string> (this, android. r. layout. simple_dropdown_item_1line, autostring); autocompletetextview m_autocompletetextview = (autocompletetextview) findviewbyid (R. id. autocompletetextview01); // Add the adapter to autocompletetextview m_autocompletetextview.setadapter (adapter ); /////////////// multiautocompletetextview mm_autocompletetextview = (multiautocompletetextview) findviewbyid (R. id. multiautocompletetextview01); // Add the adapter to autocompletetextview mm_autocompletetextview.setadapter (adapter); mm_autocompletetextview.settokenizer (New multiautocompletetextview. commatokenizer ());

:

 

For details about the differences between the two controls autocompletetextview and multiautocompletetextview, refer:

Android control: Exploring autocompletetextview and multiautocompletetextview

4. Date and Time (datepicker and timepicker)

The use of these two controls is relatively simple, in Main. after being defined in the XML layout file. util. the calendar class obtains the system time, passes the date to datepicker through the init method, and sets the ondatechangedlistener to listen for the date change.

You can set the setontimechangedlistener listener for the same time changes.

 

Key source code:

 

Calendar c = calendar. getinstance (); // get the datepicker object m_datepicker = (datepicker) findviewbyid (R. id. datepicker01); // initialize the calendar to the current system time and set its event listening m_datepicker.init (C. get (calendar. year), C. get (calendar. month), C. get (calendar. day_of_month), new datepicker. ondatechangedlistener () {@ overridepublic void ondatechanged (datepicker view, int year, int monthofyear, int dayofmonth) {// when the date is changed, process it here // C. set (year, monthofyear, dayofmonth );}});

Go to p79

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.