Android Digital Selector-numberpicker

Source: Internet
Author: User

Digital selector Numberpicker is a control introduced after Android3.0, more commonly used, such as mobile phone commonly used alarm clock, you can choose hours and minutes, if you need to compatible with the 3.0 version, GitHub has open source projects, specific https:// Github.com/simonvt/android-numberpicker. I do not use open source projects, simply use the Numberpicker show the effect, start to the point:

Basic Maintenance

Development things first look at the effect of it:

Numberpicker and TextView show the time, linear layout, and look at the layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "android:orient ation= "vertical" tools:context= "com.example.googlenumberpicker.MainActivity" > <linearlayout android:la Yout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "30DP" Android : layout_marginleft= "50DP" android:layout_gravity= "Center_horizontal" > <numberpicker androi        D:id= "@+id/hourpicker" android:layout_width= "40DP" android:layout_height= "Wrap_content"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:layout_gravity= "center_vertical" android:text= "/> <numberpicker android:id=" @+ Id/minuteicker "Android:layout_width= "40DP" android:layout_height= "wrap_content"/> <textview android:layout_width= "Wrap_co Ntent "android:layout_height=" wrap_content "android:layout_gravity=" Center_vertical "Android oid:text= "min"/> </LinearLayout></LinearLayout>
Demo Implementation

The digital selection is able to slide, so you need to define a Onvaluechangelistener event, Onscrolllistener slide event, formatter event:

Formatter event:

Public String Format (int value) {        string tmpstr = string.valueof (value);        if (value <) {            tmpstr = "0" + tmpstr;        }        return tmpstr;    }

Onvaluechangelistener Event:

  public void Onvaluechange (Numberpicker picker, int. oldval, int newval) {        Toast.maketext (this                ,                "original value" + O Ldval + "-New value:"                        + newval, Toast.length_short). Show ();    

Onscrolllistener sliding events, there are three states of sliding events:

Scroll_state_fling: The hand is still sliding after leaving

Scroll_state_idle: Do not slide

Scroll_state_touch_scroll: Sliding in

  public void Onscrollstatechange (Numberpicker view, int. scrollstate) {        switch (scrollstate) {        case Onscrolllistener.scroll_state_fling:            Toast.maketext (This, "Follow the slide (fly, fly, stop at all)", Toast.length_long)                    . Show ();            break;        Case Onscrolllistener.scroll_state_idle:            toast.maketext (This, "Do not Slide", Toast.length_long). Show ();            break;        Case Onscrolllistener.scroll_state_touch_scroll:            toast.maketext (This, "sliding in", Toast.length_long)                    . Show () ;            break;        }    }

Initialization

Hourpicker= (Numberpicker) Findviewbyid (R.id.hourpicker); minutepicker= (Numberpicker) findViewById ( R.id.minuteicker); init ();

In the Init method, set the maximum, minimum, and sliding events for a number:

   private void Init () {    hourpicker.setformatter (this);    Hourpicker.setonvaluechangedlistener (this);    Hourpicker.setonscrolllistener (this);    Hourpicker.setmaxvalue ();    Hourpicker.setminvalue (0);    Hourpicker.setvalue (9);        Minutepicker.setformatter (this);    Minutepicker.setonvaluechangedlistener (this);    Minutepicker.setonscrolllistener (this);    Minutepicker.setmaxvalue ();    Minutepicker.setminvalue (0);    Minutepicker.setvalue ();    }

One more step, the activity needs to inherit the Onvaluechangelistener,onscrolllistener,formatter:

public class Mainactivity extends Activity implements onvaluechangelistener,onscrolllistener,formatter{...}

The last thing to say is that Numberpicker can also display text, redefine a numberpicker, and load it up:

Valuepicker = (numberpicker) Findviewbyid (R.id.valuepicker); string[] City = {"Stand Water Bridge", "Huo Ying", "Huilongguan", "Long Ze", "West Two Flags", "Shang Di"};valuepicker.setdisplayedvalues (city); Valuepicker.setminvalue (0 ); Valuepicker.setmaxvalue (city.length-1); Valuepicker.setvalue (4);

The result of the final display:

Android Digital Selector-numberpicker

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.