Android 0 Basics Section 58th: Value selector numberpicker

Source: Internet
Author: User
Tags event listener

Original: Android 0 Basics 58th: Value selector numberpicker

The previous issue learned the date selector DatePicker and time picker timepicker, is not feeling very simple, this issue continues to learn the value selector Numberpicker.

I. Overview of Numberpicker

Numberpicker is a component used to select a set of predefined numbers that can be entered either by the keyboard or by scrolling to select a value.

The common methods of Numberpicker are as follows:

    • Setminvalue (int minval): Sets the minimum supported value for this component.

    • Setmaxvalue (int maxval): Sets the maximum value supported by this component.

    • SetValue (int value): Sets the current value of the component.

    • Getmaxvalue (): Gets the maximum value set by the component.

    • Getminvalue (): Gets the minimum value set by the component.

    • GetValue (): Gets the value displayed by the current component.

    • SetValue (int value): Sets the value displayed by the current component.

With Numberpicker there are altogether 2 listeners and one formatter format processor,

    • Numberpicker.onvaluechangelistener: Used to listen for changes to the current value.

    • Numberpicker.onscrolllistener: The scroll state used to listen for the control. The following three states are mainly included:

      • Scroll_state_touch_scroll: The user presses down and then slides.

      • Scroll_state_fling: Equivalent to the subsequent slide operation of the Scroll_state_touch_scroll.

      • Scroll_state_idle:numberpicker is not scrolling.

    • Numberpicker.formatter: Used to format the display of value in the component, such as 0-23 formatted as 00-23.

Ii. Examples of Numberpicker

Next, a simple example program is used to learn the use of Numberpicker.

Continue to use the Advancedviewsample module of the Widgetsample project to create a numberpicker_layout.xml file in the app/main/res/layout/directory populated with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <NumberpickerAndroid:id= "@+id/numberpicker"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"/></Relativelayout>

Create a new Numberpickeractivity.java file, load the new layout file above, initialize the Numberpicker and get the user's choice, with the following code:

 Packagecom.jinyu.cqkxzsxy.android.advancedviewsample;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.NumberPicker;ImportAndroid.widget.Toast;/*** @ Creator Xin 鱻 * @ description Android 0 Basic primer to Master Series tutorials, welcome to the public number Shareexpert*/ Public classNumberpickeractivityextendsappcompatactivity {PrivateNumberpicker Mnumberpicker =NULL;//Value Selectors@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.numberpicker_layout); //Get Numberpicker ComponentsMnumberpicker =(Numberpicker) Findviewbyid (R.id.numberpicker); //Setting the Numberpicker propertyMnumberpicker.setminvalue (1); Mnumberpicker.setmaxvalue (20); Mnumberpicker.setvalue (5); //Monitor numeric change eventsMnumberpicker.setonvaluechangedlistener (NewNumberpicker.onvaluechangelistener () {@Override Public voidOnvaluechange (Numberpicker picker,intOldval,intnewval) {Toast.maketext (numberpickeractivity). This, "Selected:" +newval, Toast.length_short). Show ();    }        }); }}

The above program code is the code that Numberpicker binds the event listener, and the listener is triggered when the user chooses the time through this component.

Running the program, you can see the interface effect shown.

In addition to the Android system-defined DatePicker, Timepicker, and Numberpicker, which are often not met in real-world development, some picker components, such as City selectors, gender selectors, picture selectors, color selectors, are often customized.

Can generally borrow third-party components to quickly complete, of course, can also be customized implementation, here do not introduce too much, you can study on their own, welcome to explore together.

Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: text box TextView

Android 0 Basics Section 18th: Input box EditText

Android 0 Basics Get started section 19th: Buttons button

Android 0 Basics 20th: check box checkbox and radio button radiobutton

Android 0 Basics Section 21st: Switch Components ToggleButton and switch

Android 0 Basics Section 22nd: Image View ImageView

Android 0 Basics Section 23rd: Image button ImageButton and zoom button Zoombutton

Android 0 Basics Section 24th: Customize view simple to use to create your own controls

Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts

Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly

Android 0 Basics section 27th: Using padding and margin correctly

Android 0 Basics Section 28th: Easy to master relativelayout relative layout

Android 0 Basics 29th: Use tablelayout table layouts

Android 0 Basics 30th: Two minutes master Framelayout frame layout

Android 0 Basics Section 31st: absolutelayout absolute Layout with less

Android 0 Basics Section 32nd: New GridLayout Grid layout

Android 0 Basics section 33rd: Android Event Handling overview

Android 0 Basics Section 34th: Listening-based event handling in Android

Android 0 Basics Section 35th: Callback-based event handling in Android

Android 0 Basics Section 36th: Handling of Android system events

Android 0 Basics 37th: First Knowledge ListView

Android 0 Basics 38th: First Knowledge Adapter

Android 0 Basics section 39th: listactivity and custom list items

Android 0 Basics Section 40th: Customizing Arrayadapter

Android 0 Basics Section 41st: Using Simpleadapter

Android 0 Basics Section 42nd: Customizing Baseadapter

Android 0 Basics section 43rd: ListView Optimization and List End-to-end use

Android 0 Basics Section 44th: ListView Data Dynamic Update

Android 0 Basic Getting Started section 45th: Grid view GridView

Android 0 Basics section 46th: List Options Box spinner

Android 0 Basic Getting Started section 47th: AutoComplete text Box Autocompletetextview

Android 0 Basics Section 48th: Collapsible list Expandablelistview

Android 0 Basics section 49th: Adapterviewflipper picture Carousel

Android 0 Basics Section 50th: StackView card Stacking

Android 0 Basics Section 51st: progress bar ProgressBar

Android 0 Basics section 52nd: Customizing ProgressBar Cool progress bar

Android 0 Basics 53rd: Drag bar Seekbar and star rating bar Ratingbar

Android 0 Basics section 54th: View switch Components Viewswitcher

Android 0 Basics Section 55th: Imageswitcher and Textswitcher

Android 0 Basics Section 56th: Flip View Viewflipper

Android 0 Basics Section 57th: DatePicker and Timepicker selectors

Android 0 Basics Section 58th: Value selector numberpicker

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.