Android basic entry control (5)

Source: Internet
Author: User

I. Spinner (drop-down list ):

Encoding implementation: first define the Spinner component in the layout file, connect the optional content through ArrayApadter and the drop-down list, and finally obtain the user selection option, you need to design the event listening setOnItemSelectedListener and implement onItemSelected to obtain the content selected by the user.

The ArrayAdapter adapter is used to connect the data source and view, that is, the data in the array can be displayed in the spinnner.

Instance:


     
      
  
 


Code Section:

Public class MainActivity extends Activity {public static final String [] m_Countries = {"O", "A", "B", "AB", "other"}; private TextView textView; private Spinner spinner; private ArrayAdapter
 
  
Adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById (R. id. textView1); spinner = (Spinner) findViewById (R. id. spinner1); // connect the available content to the ArrayAdapter adapter. adapter = new ArrayAdapter
  
   
(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_spinner_dropdown_item); // Add the adapter to the spinner. setAdapter (adapter); spinner. setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView
   Arg0, View arg1, int arg2, long arg3) {displayToast ("You selected" + m_Countries [arg2]); textView. setText ("your blood type is:" + m_Countries [arg2]); // set to display the currently selected item arg0.setVisibility (View. VISIBLE) ;}@ Overridepublic void onNothingSelected (AdapterView
   Arg0) {// TODO Auto-generated method stub});} public void displayToast (String text) {Toast. makeText (getApplicationContext (), text, Toast. LENGTH_SHORT ). show () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
  
 


Running interface:

After the selected blood type is selected, a message is displayed, and the displayed content in TextView is changed to the selected type.



Ii. AutoCompleteTextView and MultiAutoCompleteTextView (automatic prompt ):

AutoCompleteTextView: supports basic auto-completion functions. It is applicable to various search functions and can set its default display data as needed.

MultiAutoCompleteTextView: multiple values can be selected (in case of multiple inputs), separated by delimiters, and will be automatically matched when values are input again when they are selected, for example, add a contact when a text message is sent.

Layout implementation:

     
          
      
     
 

Code implementation:

Public class MainActivity extends Activity {public static final String [] auto = {"tiger", "bull", "horse", "elephant", "dog", "pig ", "duck", "bee", "cat", "dove", "eagle", "fish", "whale"}; private TextView textView; private ArrayAdapter
 
  
Adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById (R. id. textView1); // correlation keyword adapter = new ArrayAdapter
  
   
(This, android. R. layout. layout, auto); AutoCompleteTextView autoView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1); // Add the adapter to AutoCompleteTextView. AutoView. setAdapter (adapter); MultiAutoCompleteTextView multView = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView1); // Add the adapter to MultiAutoCompleteTextView. MultView. setAdapter (adapter); multView. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer ();} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
  
 


Running image:

AutoCompleteTextView:

MultiAutoCompleteTextView:


You can use the setThreshold () method to set the number of characters entered to start retrieval. The default value is 2 characters.


3. DatePicker and TimePicker (date and time ):

In android, DatePicker is used to implement date and TimePicker is used to implement time.

Implementation process:

Now define DatePicker and TimePicker in the layout file, get the system time through the Calendar class, pass the date to DatePicker through the init () method, and set OnDateChangedListener to listen for date changes, when the time is changed, you need to set the setOnTimeChangedListener () Listener to set the time.

Layout file:

     
      
      
  
 


Code implementation:

Public class MainActivity extends Activity {private TextView textview; private DatePicker datepicker; private TimePicker timepicker; // CalendarCalendar c in java; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); c = Calendar. getInstance (); textview = (TextView) findViewById (R. id. textView1); datepicker = (DatePicker) findVie WById (R. id. datePicker1); // initialize the diary to the current system time and set the monitoring event 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) {// process the changes here when they are mentioned .}}); Timepicker = (TimePicker) findViewById (R. id. timePicker1); // set to display timepicker in 24-hour format. setIs24HourView (true); timepicker. setOnTimeChangedListener (new TimePicker. onTimeChangedListener () {@ Overridepublic void onTimeChanged (TimePicker view, int hourOfDay, int minute) {// processing when the time changes. Add yourself});} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}

Running image:





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.