Android High Imitation iOS wheel selection control _android

Source: Internet
Author: User
Tags string format

Recently, according to the needs of the project, sorted out a relatively comprehensive wheelview use control, borrowed before see a word, is standing on the shoulders of giants, made a number of small adjustments.
Here, put the effect chart first.

The commonly used time choice format, the individual choice, as well as the city linkage, here basically can satisfy.

Here the single selection, and date time selection is presented to the Util class, the code is as follows:

public class Util {/** * Time selection callback/public interface Timerpickercallback {void Ontimeselect (String date); /** * Popup Time selection * * @param context * @param type Timerpickerview-defined in the Select time types * @param format time formatted * @param CallBack Time Selection callback/public static void Alerttimerpicker (context, Timepickerview.type Type, final String format,
  Final Timerpickercallback callBack) {Timepickerview pvtime = new Timepickerview (context, type);
  Control time range/Calendar calendar = Calendar.getinstance ();
  Pvtime.setrange (Calendar.get (calendar.year)-Calendar.get (calendar.year));
  Pvtime.settime (New Date ());
  Pvtime.setcyclic (FALSE);
  Pvtime.setcancelable (TRUE); Callback Pvtime.setontimeselectlistener after Time selection (new Timepickerview.ontimeselectlistener () {@Override public void OnTime
    Select (date date) {//Tvtime.settext (GetTime (date));
    SimpleDateFormat SDF = new SimpleDateFormat (format);
   Callback.ontimeselect (Sdf.format (date));
  }
  }); Pvtime.Settextsize (16);
 Pop-up time selector pvtime.show ();
 /** * Bottom Wheel Click event Callback/public interface Onwheelviewclick {void OnClick (view view, int postion); /** * Eject Bottom wheel selection * * @param context * @param list * @param click/public static void Alertbottomwheelopti On (context, arraylist<?> list, final Onwheelviewclick click) {final Popupwindow Popupwindow = new POPUPW

  Indow ();
  View view = Layoutinflater.from (context). Inflate (r.layout.layout_bottom_wheel_option, NULL);
  TextView tv_confirm = (TextView) View.findviewbyid (r.id.btnsubmit);
  Final Wheelview wv_option = (wheelview) View.findviewbyid (r.id.wv_option);
  Wv_option.setadapter (new Arraywheeladapter (list));
  Wv_option.setcyclic (FALSE);
  Wv_option.settextsize (16); Tv_confirm.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {Popupwindo
    W.dismiss ();
   Click.onclick (view, Wv_option.getcurrentitem ());

  }
  }); View.findviewbyid (R.id.btncancel).Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {//TODO:2016/8/11 00
   11 Cancellation of Popupwindow.dismiss ();
  }
  }); View.setontouchlistener (New View.ontouchlistener () {@Override public boolean ontouch (view view, Motionevent Motione
    VENT) {int top = View.findviewbyid (R.id.ll_container). GetTop ();
     if (motionevent.getaction () = = motionevent.action_up) {int y = (int) motionevent.gety ();
     if (Y < top) {Popupwindow.dismiss ();
   } return true;
  }
  });
  Popupwindow.setcontentview (view);
  Popupwindow.setoutsidetouchable (TRUE);
  Popupwindow.setfocusable (TRUE);
  Popupwindow.setbackgrounddrawable (New bitmapdrawable ());
  Popupwindow.setwidth (ViewGroup.LayoutParams.MATCH_PARENT);
  Popupwindow.setheight (ViewGroup.LayoutParams.MATCH_PARENT); Popupwindow.showatlocation ((ViewGroup) (activity), Findviewbyid (Android).
 r.id.content)). Getchildat (0), Gravity.center, 0, 0);

 }
}

• Single Selection

This is the String type that simulates the incoming ArrayList form:

 Single Select for
  (int i = 0; I <= i++) {
   Mlist.add ("Analog data" + i);
  }

  Tv_single_option.setonclicklistener (New View.onclicklistener () {
   @Override public
   void OnClick (View v) {
    util.alertbottomwheeloption (Mainactivity.this, Mlist, New Util.onwheelviewclick () {
     @Override
     public void OnClick (view view, int postion) {
      toast.maketext (mainactivity.this, Mlist.get (postion), Toast.length_short) . Show ();}}
    );
  

Add: Our actual project usage may be passing in an entity object, so we go to Wheelview to find a way to set the display content:

/**
  * reflects the Getpickerviewtext () method based on the incoming object to obtain the value that needs to be displayed
  * @param item
  * @return/
 private String Getcontenttext (Object Item) {
  String contenttext = item.tostring ();
  try {
   class<?> clz = Item.getclass ();
   Method M = Clz.getmethod (getpickerviewtext);
   ContentText = M.invoke (item, new Object[0]). toString ();
  catch (Nosuchmethodexception e) {
  } catch (InvocationTargetException e) {
  } catch (Illegalaccessexception e) { c14/>} catch (Exception e) {
  } return
  contenttext;
 }

Based on the above code, you can see that if you are an entity object, you are using the return value of a method named Getpickerviewtext (Static constant = "Getpickerviewtext") defined internally by the object as the display content,

So when you create an object, be careful to add a Getpickerviewtext () method inside the object, the code is as follows:

public class Typebean {

 private int id;
 private String name;

 public Typebean (int ID, String name) {
  this.id = ID;
  this.name = name;

 public int getId () {return
  ID;
 }

 public void setId (int id) {
  this.id = ID;
 }

 Public String GetName () {return
  name;
 }

 public void SetName (String name) {
  this.name = name;
 }

 The string that is used to display on the Pickerview, Pickerview is displayed by means of a reflection fetch Getpickerviewtext method. Public
 String Getpickerviewtext () {
  //Here you can also determine that the text is long truncated and then provide display return
  name;
 }


• Date Selection
Here is the incoming selection date type, and the callback time format can directly get the desired result,

 @Override public void OnClick (View v) {String format = ' ";
  Timepickerview.type Type = null;
    Switch (V.getid ()) {case r.id.btn_ymdhm:type = TimePickerView.Type.ALL;
    Format = "Yyyy-mm-dd hh:mm";
   Break
    Case r.id.btn_ymdh:type = TimePickerView.Type.YEAR_MONTH_DAY_HOUR;
    Format = "Yyyy-mm-dd HH";
   Break
    Case r.id.btn_ymd:type = TimePickerView.Type.YEAR_MONTH_DAY;
    format = "YYYY-MM-DD";
   Break
    Case r.id.btn_mdhm:type = TimePickerView.Type.MONTH_DAY_HOUR_MIN;
    Format = "Mm-dd hh:mm";
   Break
    Case r.id.btn_hm:type = TimePickerView.Type.HOURS_MINS;
    format = "hh:mm";
   Break
    Case r.id.btn_ym:type = TimePickerView.Type.YEAR_MONTH;
    format = "YYYY-MM";
  Break Util.alerttimerpicker (this, type, format, new Util.timerpickercallback () {@Override public void Ontimeselect (St
   Ring Date} {Toast.maketext (testactivity.this, date, Toast.length_short). Show ();

 }
  });

 }

  Conditional selection  

Private arraylist<provincebean> Options1items = new arraylist<provincebean> ();
Private arraylist<arraylist<string>> Options2items = new arraylist<arraylist<string>> (); Private arraylist<arraylist<arraylist<string>>> Options3items = new arraylist<arraylist<

Arraylist<string>>> ();

Optionspickerview pvoptions;
  private void Showoptions () {//Option selector pvoptions = new Optionspickerview (this);

  Initializes three list data Datamodel.initdata (Options1items, Options2items, Options3items);
  Three-level linkage effect Pvoptions.setpicker (Options1items, Options2items, Options3items, true);
  Set the selected level three unit//Pwoptions.setlabels ("Province", "City", "District");
  Pvoptions.settitle ("select City");
  Pvoptions.setcyclic (False, False, false);
  Set the default selected level three item//monitor OK Select button Pvoptions.setselectoptions (1, 1, 1);
  Pvoptions.settextsize (18); Pvoptions.setonoptionsselectlistener (New Optionspickerview.onoptionsselectlistener () {@Override public void OnOpti Onsselect (int options1, int option2, int options3) {//Return is three level selected location String tx = Options1items.get (OPTIONS1). Getpickerviewtext ()
    + Options2items.get (options1). Get (Option2) + options3items.get (options1). Get (Option2).
    Tvoptions.settext (TX);
   Vmasker.setvisibility (View.gone);
  }
  });
    Click the popup option selector Tvoptions.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View v) {
   Pvoptions.show ();
 }
  });

 }

Basic use of these, there is no technical content, just as a common tool to organize, but also hope to bring convenience to everyone.

Click to download: source

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.