欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android自定义滚轴选择器

程序员文章站 2022-07-06 14:35:09
本文实例为大家分享了android自定义滚轴选择器的具体代码,供大家参考,具体内容如下 效果图: 点击选择餐具弹出底部弹窗 private dial...

本文实例为大家分享了android自定义滚轴选择器的具体代码,供大家参考,具体内容如下

效果图:

android自定义滚轴选择器

点击选择餐具弹出底部弹窗

 private dialog dialog;
 private view inflate;
 pickvalueview pickstring;
 
 
 
case r.id.rl_cj_num://餐具数量选择
    //选择对话框
    dialog = new dialog(this, r.style.actionsheetdialogstyle);
    view contentview = layoutinflater.from(this).inflate(r.layout.dialog_cj_num, null);
    //获取组件
    tv_cancel = contentview.findviewbyid(r.id.tv_cancel);
    pickstring = contentview.findviewbyid(r.id.pickstring);
    //获取dialog的监听
    tv_cancel.setonclicklistener(this);
    pickstring.setonselectedchangelistener(this);
    string[] valuestr = new string[]{"无需餐具", "1人", "2人", "3人", "4人", "5人",
      "6人", "7人", "8人"};
    pickstring.setvaluedata(valuestr, valuestr[1]);
    dialog.setcontentview(contentview);
    viewgroup.layoutparams layoutparams = contentview.getlayoutparams();
    layoutparams.width = getresources().getdisplaymetrics().widthpixels;
    contentview.setlayoutparams(layoutparams);
    dialog.getwindow().setgravity(gravity.bottom);//弹窗位置
    dialog.getwindow().setwindowanimations(r.style.actionsheetdialogstyle);//弹窗样式
    dialog.show();//显示弹窗
    break;

styles.xml中的样式:

 <!-- dialog弹窗效果-->
 <style name="actionsheetdialogstyle" parent="@android:style/theme.dialog">
  <!-- 背景透明 -->
  <item name="android:windowbackground">@android:color/transparent</item>
  <item name="android:windowcontentoverlay">@null</item>
  <!-- 浮于activity之上 -->
  <item name="android:windowisfloating">true</item>
  <!-- 边框 -->
  <item name="android:windowframe">@null</item>
  <!-- dialog以外的区域模糊效果 -->
  <item name="android:backgrounddimenabled">true</item>
  <!-- 无标题 -->
  <item name="android:windownotitle">true</item>
  <!-- 半透明 -->
  <item name="android:windowistranslucent">true</item>
  <!-- dialog进入及退出动画 -->
  <item name="android:windowanimationstyle">@style/actionsheetdialoganimation</item>
 </style>
 <!-- actionsheet进出动画 -->
 <style name="actionsheetdialoganimation" parent="@android:style/animation.dialog">
  <item name="android:windowenteranimation">@anim/actionsheet_dialog_in</item>
  <item name="android:windowexitanimation">@anim/actionsheet_dialog_out</item>
 </style>
 
 <style name="splashtheme" parent="apptheme">
  <!-- 这里的trans自己写一个#00000000即可-->
  <item name="android:windowbackground">@drawable/layer_splash</item>
  <item name="windownotitle">true</item>
  <item name="android:windowfullscreen">true</item>
 </style>

弹入弹出动画效果:

anim下新建:弹入资源文件actionsheet_dialog_in和弹出资源文件actionsheet_dialog_out

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="200"
 android:fromydelta="100%"
 android:toydelta="0" />
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="200"
 android:fromydelta="0"
 android:toydelta="100%" />

自定义弹窗布局dialog_cj_num:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#f9f9f9"
 android:orientation="vertical">
 
 <textview
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="餐具选择"
  android:textcolor="#333333"
  android:textsize="16sp" />
 
 <view
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#e8e8e8" />
 
 <com.shanjing.mymeishi.view.pickvalueview
  android:id="@+id/pickstring"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />
 
 <view
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#e8e8e8" />
 
 <textview
  android:id="@+id/tv_cancel"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center"
  android:text="取消"
  android:textcolor="#333333"
  android:textsize="16sp" />
</linearlayout>

自定义视图pickvalueview和引用类mynumberpicker:

package com.shanjing.mymeishi.view;
 
import android.content.context;
import android.graphics.color;
import android.util.attributeset;
import android.view.gravity;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.numberpicker;
import android.widget.textview;
 
/**
 * 选择日期视图
 */
 
public class pickvalueview extends linearlayout implements numberpicker.onvaluechangelistener {
 private context mcontext;
 /**
  * 组件 标题、单位、滚轮
  */
 private textview mtitleleft, mtitlemiddle, mtitleright;
 private textview munitleft, munitmiddle, munitright;
 private mynumberpicker mnpleft, mnpmiddle, mnpright;
 /**
  * 数据个数 1列 or 2列 or 3列
  */
 private int mviewcount = 1;
 /**
  * 一组数据长度
  */
 private final int data_size = 3;
 
 /**
  * 需要设置的值与默认值
  */
 private object[] mleftvalues;
 private object[] mmiddlevalues;
 private object[] mrightvalues;
 private object mdefaultleftvalue;
 private object mdefaultmiddlevalue;
 private object mdefaultrightvalue;
 /**
  * 当前正在显示的值
  */
 private object[] mshowingleft = new object[data_size];
 private object[] mshowingmiddle = new object[data_size];
 private object[] mshowingright = new object[data_size];
 
 /**
  * 步长
  */
 private int mleftstep = 5;
 private int mmiddlestep = 1;
 private int mrightstep = 1;
 /**
  * 回调接口对象
  */
 private onselectedchangelistener mselectedchangelistener;
 
 public pickvalueview(context context) {
  super(context);
  this.mcontext = context;
  generateview();
 }
 
 public pickvalueview(context context, attributeset attrs) {
  super(context, attrs);
  this.mcontext = context;
  generateview();
 }
 
 public pickvalueview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  this.mcontext = context;
  generateview();
 }
 
 /**
  * 生成视图
  */
 private void generateview() {
  //标题
  linearlayout titlelayout = new linearlayout(mcontext);
  layoutparams titleparams = new layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content);
  titleparams.setmargins(0, 0, 0, dip2px(12));
  titlelayout.setlayoutparams(new layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content));
  titlelayout.setorientation(horizontal);
  mtitleleft = new textview(mcontext);
  mtitlemiddle = new textview(mcontext);
  mtitleright = new textview(mcontext);
 
  layoutparams params = new layoutparams(0, viewgroup.layoutparams.wrap_content, 1);
  textview[] titles = new textview[]{mtitleleft, mtitlemiddle, mtitleright};
  for (int i = 0; i < titles.length; i++) {
   titles[i].setlayoutparams(params);
   titles[i].setgravity(gravity.center);
   titles[i].settextcolor(color.parsecolor("#000000"));
   titles[i].settextsize(12);
  }
  titlelayout.addview(mtitleleft);
  titlelayout.addview(mtitlemiddle);
  titlelayout.addview(mtitleright);
  //内容
  linearlayout contentlayout = new linearlayout(mcontext);
  contentlayout.setlayoutparams(new layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content));
  contentlayout.setorientation(horizontal);
  contentlayout.setgravity(gravity.center);
  mnpleft = new mynumberpicker(mcontext);
  mnpmiddle = new mynumberpicker(mcontext);
  mnpright = new mynumberpicker(mcontext);
  munitleft = new textview(mcontext);
  munitmiddle = new textview(mcontext);
  munitright = new textview(mcontext);
 
  mynumberpicker[] nps = new mynumberpicker[]{mnpleft, mnpmiddle, mnpright};
  for (int i = 0; i < nps.length; i++) {
   nps[i].setlayoutparams(params);
   nps[i].setdescendantfocusability(focus_block_descendants);
   nps[i].setonvaluechangedlistener(this);
  }
 
  contentlayout.addview(mnpleft);
  contentlayout.addview(munitleft);
  contentlayout.addview(mnpmiddle);
  contentlayout.addview(munitmiddle);
  contentlayout.addview(mnpright);
  contentlayout.addview(munitright);
 
  this.setlayoutparams(new layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content));
  this.setorientation(vertical);
  this.addview(titlelayout);
  this.addview(contentlayout);
 }
 
 /**
  * 初始化数据和值
  */
 private void initviewandpicker() {
  if (mviewcount == 1) {
   this.mnpmiddle.setvisibility(gone);
   this.mnpright.setvisibility(gone);
   this.munitmiddle.setvisibility(gone);
   this.munitright.setvisibility(gone);
  } else if (mviewcount == 2) {
   this.mnpright.setvisibility(gone);
   this.munitright.setvisibility(gone);
  }
 
  //初始化数组值
  if (mleftvalues != null && mleftvalues.length != 0) {
   if (mleftvalues.length < data_size) {
    for (int i = 0; i < mleftvalues.length; i++) {
     mshowingleft[i] = mleftvalues[i];
    }
    for (int i = mleftvalues.length; i < data_size; i++) {
     mshowingleft[i] = -9999;
    }
   } else {
    for (int i = 0; i < data_size; i++) {
     mshowingleft[i] = mleftvalues[i];
    }
   }
   mnpleft.setminvalue(0);
   mnpleft.setmaxvalue(data_size - 1);
   if (mdefaultleftvalue != null)
    updateleftview(mdefaultleftvalue);
   else
    updateleftview(mshowingleft[0]);
  }
  /**
   * 中间控件
   */
  if (mviewcount == 2 || mviewcount == 3) {
   if (mmiddlevalues != null && mmiddlevalues.length != 0) {
    if (mmiddlevalues.length < data_size) {
     for (int i = 0; i < mmiddlevalues.length; i++) {
      mshowingmiddle[i] = mmiddlevalues[i];
     }
     for (int i = mmiddlevalues.length; i < data_size; i++) {
      mshowingmiddle[i] = -9999;
     }
    } else {
     for (int i = 0; i < data_size; i++) {
      mshowingmiddle[i] = mmiddlevalues[i];
     }
    }
    mnpmiddle.setminvalue(0);
    mnpmiddle.setmaxvalue(data_size - 1);
    if (mdefaultmiddlevalue != null)
     updatemiddleview(mdefaultmiddlevalue);
    else
     updatemiddleview(mshowingmiddle[0]);
   }
  }
 
  /**
   * 右侧控件
   */
  if (mviewcount == 3) {
   if (mrightvalues != null && mrightvalues.length != 0) {
    if (mrightvalues.length < data_size) {
     for (int i = 0; i < mrightvalues.length; i++) {
      mshowingright[i] = mrightvalues[i];
     }
     for (int i = mrightvalues.length; i < data_size; i++) {
      mshowingright[i] = -9999;
     }
    } else {
     for (int i = 0; i < data_size; i++) {
      mshowingright[i] = mrightvalues[i];
     }
    }
    mnpright.setminvalue(0);
    mnpright.setmaxvalue(data_size - 1);
    if (mdefaultrightvalue != null)
     updaterightview(mdefaultrightvalue);
    else
     updaterightview(mshowingright[0]);
   }
  }
 
 
 }
 
 private void updateleftview(object value) {
  updatevalue(value, 0);
 }
 
 private void updatemiddleview(object value) {
  updatevalue(value, 1);
 }
 
 private void updaterightview(object value) {
  updatevalue(value, 2);
 }
 
 /**
  * 更新滚轮视图
  *
  * @param value
  * @param index
  */
 private void updatevalue(object value, int index) {
  string showstr[] = new string[data_size];
  mynumberpicker picker;
  object[] showingvalue;
  object[] values;
  int step;
  if (index == 0) {
   picker = mnpleft;
   showingvalue = mshowingleft;
   values = mleftvalues;
   step = mleftstep;
  } else if (index == 1) {
   picker = mnpmiddle;
   showingvalue = mshowingmiddle;
   values = mmiddlevalues;
   step = mmiddlestep;
  } else {
   picker = mnpright;
   showingvalue = mshowingright;
   values = mrightvalues;
   step = mrightstep;
  }
 
  if (values instanceof integer[]) {
   for (int i = 0; i < data_size; i++) {
    showingvalue[i] = (int) value - step * (data_size / 2 - i);
    int offset = (int) values[values.length - 1] - (int) values[0] + step;
    if ((int) showingvalue[i] < (int) values[0]) {
     showingvalue[i] = (int) showingvalue[i] + offset;
    }
    if ((int) showingvalue[i] > (int) values[values.length - 1]) {
     showingvalue[i] = (int) showingvalue[i] - offset;
    }
    showstr[i] = "" + showingvalue[i];
   }
  } else {
   int strindex = 0;
   for (int i = 0; i < values.length; i++) {
    if (values[i].equals(value)) {
     strindex = i;
     break;
    }
   }
   for (int i = 0; i < data_size; i++) {
    int temp = strindex - (data_size / 2 - i);
    if (temp < 0) {
     temp += values.length;
    }
    if (temp >= values.length) {
     temp -= values.length;
    }
    showingvalue[i] = values[temp];
    showstr[i] = (string) values[temp];
   }
  }
  picker.setdisplayedvalues(showstr);
  picker.setvalue(data_size / 2);
  picker.postinvalidate();
 }
 
 
 @override
 public void onvaluechange(numberpicker picker, int oldval, int newval) {
  if (picker == mnpleft) {
   updateleftview(mshowingleft[newval]);
  } else if (picker == mnpmiddle) {
   updatemiddleview(mshowingmiddle[newval]);
  } else if (picker == mnpright) {
   updaterightview(mshowingright[newval]);
  }
  if (mselectedchangelistener != null) {
   mselectedchangelistener.onselected(this, mshowingleft[data_size / 2], mshowingmiddle[data_size / 2], mshowingright[data_size / 2]);
  }
 }
 
 /**
  * 设置数据--单列数据
  *
  * @param leftvalues
  * @param mdefaultleftvalue
  */
 public void setvaluedata(object[] leftvalues, object mdefaultleftvalue) {
  this.mviewcount = 1;
  this.mleftvalues = leftvalues;
  this.mdefaultleftvalue = mdefaultleftvalue;
 
  initviewandpicker();
 }
 
 /**
  * 设置数据--两列数据
  *
  * @param leftvalues
  * @param mdefaultleftvalue
  * @param middlevalues
  * @param defaultmiddlevalue
  */
 public void setvaluedata(object[] leftvalues, object mdefaultleftvalue, object[] middlevalues, object defaultmiddlevalue) {
  this.mviewcount = 2;
  this.mleftvalues = leftvalues;
  this.mdefaultleftvalue = mdefaultleftvalue;
 
  this.mmiddlevalues = middlevalues;
  this.mdefaultmiddlevalue = defaultmiddlevalue;
 
  initviewandpicker();
 }
 
 /**
  * 设置数据--三列数据
  *
  * @param leftvalues
  * @param mdefaultleftvalue
  * @param middlevalues
  * @param defaultmiddlevalue
  * @param rightvalues
  * @param defaultrightvalue
  */
 public void setvaluedata(object[] leftvalues, object mdefaultleftvalue, object[] middlevalues, object defaultmiddlevalue, object[] rightvalues, object defaultrightvalue) {
  this.mviewcount = 3;
  this.mleftvalues = leftvalues;
  this.mdefaultleftvalue = mdefaultleftvalue;
 
  this.mmiddlevalues = middlevalues;
  this.mdefaultmiddlevalue = defaultmiddlevalue;
 
  this.mrightvalues = rightvalues;
  this.mdefaultrightvalue = defaultrightvalue;
 
  initviewandpicker();
 }
 
 /**
  * 设置左边数据步长
  *
  * @param step
  */
 public void setleftstep(int step) {
  this.mleftstep = step;
  initviewandpicker();
 }
 
 /**
  * 设置中间数据步长
  *
  * @param step
  */
 public void setmiddlestep(int step) {
  this.mmiddlestep = step;
  initviewandpicker();
 }
 
 /**
  * 设置右边数据步长
  *
  * @param step
  */
 public void setrightstep(int step) {
  this.mrightstep = step;
  initviewandpicker();
 }
 
 /**
  * 设置标题
  *
  * @param left
  * @param middle
  * @param right
  */
 public void settitle(string left, string middle, string right) {
  if (left != null) {
   mtitleleft.setvisibility(visible);
   mtitleleft.settext(left);
  } else {
   mtitleleft.setvisibility(gone);
  }
  if (middle != null) {
   mtitlemiddle.setvisibility(visible);
   mtitlemiddle.settext(middle);
  } else {
   mtitlemiddle.setvisibility(gone);
  }
  if (right != null) {
   mtitleright.setvisibility(visible);
   mtitleright.settext(right);
  } else {
   mtitleright.setvisibility(gone);
  }
  this.postinvalidate();
 }
 
 public void setunitleft(string unitleft) {
  setunit(unitleft, 0);
 }
 
 public void setmunitmiddle(string unitmiddle) {
  setunit(unitmiddle, 1);
 }
 
 public void setunitright(string unitright) {
  setunit(unitright, 2);
 }
 
 private void setunit(string unit, int index) {
  textview tvunit;
  if (index == 0) {
   tvunit = munitleft;
  } else if (index == 1) {
   tvunit = munitmiddle;
  } else {
   tvunit = munitright;
  }
  if (unit != null) {
   tvunit.settext(unit);
  } else {
   tvunit.settext(" ");
  }
  initviewandpicker();
 }
 
 /**
  * 设置回调
  *
  * @param listener
  */
 public void setonselectedchangelistener(onselectedchangelistener listener) {
  this.mselectedchangelistener = listener;
 }
 
 /**
  * dp转px
  *
  * @param dp
  * @return
  */
 private int dip2px(int dp) {
  float scale = mcontext.getresources().getdisplaymetrics().density;
  return (int) (scale * dp + 0.5f);
 }
 
 /**
  * 回调接口
  */
 public interface onselectedchangelistener {
  void onselected(pickvalueview view, object leftvalue, object middlevalue, object rightvalue);
 }
}
package com.shanjing.mymeishi.view;
 
import android.content.context;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rect;
import android.graphics.drawable.colordrawable;
import android.graphics.drawable.drawable;
import android.util.attributeset;
import android.util.sparsearray;
import android.util.typedvalue;
import android.view.view;
import android.view.viewgroup;
import android.widget.edittext;
import android.widget.numberpicker;
 
import java.lang.reflect.field;
 
public class mynumberpicker extends numberpicker {
 private context mcontext;
 numberpicker picker;
 
 public mynumberpicker(context context) {
  super(context);
  picker = this;
  mcontext = context;
 }
 
 public mynumberpicker(context context, attributeset attrs) {
  super(context, attrs);
  picker = this;
  mcontext = context;
 }
 
 public mynumberpicker(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  picker = this;
  mcontext = context;
 }
 
 @override
 public void addview(view child) {
  super.addview(child);
  updateview(child);
 }
 
 @override
 public void addview(view child, int index, viewgroup.layoutparams params) {
  super.addview(child, index, params);
  updateview(child);
 }
 
 @override
 public void addview(view child, viewgroup.layoutparams params) {
  super.addview(child, params);
  updateview(child);
 }
 
 private void updateview(view view) {
  if (view instanceof edittext) {
   ((edittext) view).settextsize(typedvalue.complex_unit_sp, 20);
   ((edittext) view).settextcolor(color.parsecolor("#000000"));//进入时滚轴显示颜色
  }
 }
 
 private int mright, mleft, mbottom;
 private int mtopselectiondividertop;
 private int mbottomselectiondividerbottom;
 private int[] mselectorindices;
 private int mscrollstate;
 private sparsearray<string> mselectorindextostringcache;
 private edittext minputtext;
 private paint mselectorwheelpaint;
 private int mselectorelementheight;
 private int mcurrentscrolloffset;
 private boolean mhasselectorwheel;
 private boolean mhidewheeluntilfocused;
 private drawable mselectiondivider;
 
 /**
  * 通过反射获取值
  */
 private void getmyvalue() {
  mleft = super.getleft();
  mright = super.getright();
  mbottom = super.getbottom();
  field[] pickerfields = numberpicker.class.getdeclaredfields();
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mselectorwheelpaint")) {
    try {
     mselectorwheelpaint = (paint) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mselectorelementheight")) {
    try {
     mselectorelementheight = (int) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mcurrentscrolloffset")) {
    try {
     mcurrentscrolloffset = (int) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("minputtext")) {
    try {
     minputtext = (edittext) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mselectorindextostringcache")) {
    try {
     mselectorindextostringcache = (sparsearray<string>) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mselectorindices")) {
    try {
     mselectorindices = (int[]) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mhasselectorwheel")) {
    try {
     mhasselectorwheel = (boolean) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mhidewheeluntilfocused")) {
    try {
     mhidewheeluntilfocused = (boolean) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
//  for (field field : pickerfields) {
//   field.setaccessible(true);
//   if (field.getname().equals("mdecrementvirtualbuttonpressed")) {
//    try {
//     mdecrementvirtualbuttonpressed = (boolean) field.get(picker);
//    } catch (illegalaccessexception e) {
//     e.printstacktrace();
//    }
//    break;
//   }
//  }
//  for (field field : pickerfields) {
//   field.setaccessible(true);
//   if (field.getname().equals("mincrementvirtualbuttonpressed")) {
//    try {
//     mincrementvirtualbuttonpressed = (boolean) field.get(picker);
//    } catch (illegalaccessexception e) {
//     e.printstacktrace();
//    }
//    break;
//   }
//  }
//  for (field field : pickerfields) {
//   field.setaccessible(true);
//   if (field.getname().equals("mvirtualbuttonpresseddrawable")) {
//    try {
//     mvirtualbuttonpresseddrawable = (drawable) field.get(picker);
//    } catch (illegalaccessexception e) {
//     e.printstacktrace();
//    }
//    break;
//   }
//  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mscrollstate")) {
    try {
     mscrollstate = (int) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mtopselectiondividertop")) {
    try {
     mtopselectiondividertop = (int) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mbottomselectiondividerbottom")) {
    try {
     mbottomselectiondividerbottom = (int) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
  for (field field : pickerfields) {
   field.setaccessible(true);
   if (field.getname().equals("mselectiondivider")) {
    try {
     mselectiondivider = (drawable) field.get(picker);
    } catch (illegalaccessexception e) {
     e.printstacktrace();
    }
    break;
   }
  }
 
 }
 
 @override
 protected void ondraw(canvas canvas) {
//  super.ondraw(canvas);
  getmyvalue();
  mselectorwheelpaint.setcolor(color.blue);
 
  if (!mhasselectorwheel) {
   super.ondraw(canvas);
   return;
  }
  final boolean showselectorwheel = mhidewheeluntilfocused ? hasfocus() : true;
  float x = (mright - mleft) / 2;
  float y = mcurrentscrolloffset;
 
//  if (showselectorwheel && mvirtualbuttonpresseddrawable != null
//    && mscrollstate == onscrolllistener.scroll_state_idle) {
//   if (mdecrementvirtualbuttonpressed) {
//    mvirtualbuttonpresseddrawable.setstate(view.pressed_state_set);
//    mvirtualbuttonpresseddrawable.setbounds(0, 0, mright, mtopselectiondividertop);
//    mvirtualbuttonpresseddrawable.draw(canvas);
//   }
//   if (mincrementvirtualbuttonpressed) {
//    mvirtualbuttonpresseddrawable.setstate(pressed_state_set);
//    mvirtualbuttonpresseddrawable.setbounds(0, mbottomselectiondividerbottom, mright,
//      mbottom);
//    mvirtualbuttonpresseddrawable.draw(canvas);
//   }
//  }
 
  int[] selectorindices = mselectorindices;
  for (int i = 0; i < selectorindices.length; i++) {
   int selectorindex = selectorindices[i];
   string scrollselectorvalue = mselectorindextostringcache.get(selectorindex);
   if (i != 1) {
    mselectorwheelpaint.setcolor(color.black);
    mselectorwheelpaint.settextsize(sp2px(16));
   } else {
    mselectorwheelpaint.setcolor(color.parsecolor("#000000"));//滑动时当前滚轴上的内容颜色
    mselectorwheelpaint.settextsize(sp2px(20));
   }
 
   if ((showselectorwheel && i != 1) ||
     (i == 1 && minputtext.getvisibility() != visible)) {
    rect mrect = new rect();
    mselectorwheelpaint.gettextbounds(scrollselectorvalue, 0, scrollselectorvalue.length(), mrect);
    canvas.drawtext(scrollselectorvalue, x, y, mselectorwheelpaint);
   }
   y += mselectorelementheight;
  }
 
  // draw the selection dividers
  if (showselectorwheel && mselectiondivider != null) {
   mselectiondivider = new colordrawable(color.parsecolor("#a0c4c4c4"));//滚轴线的颜色
   // draw the top divider
   int topoftopdivider = mtopselectiondividertop;
   int bottomoftopdivider = topoftopdivider + 2;
   mselectiondivider.setbounds(0, topoftopdivider, mright, bottomoftopdivider);
   mselectiondivider.draw(canvas);
 
   // draw the bottom divider
   int bottomofbottomdivider = mbottomselectiondividerbottom;
   int topofbottomdivider = bottomofbottomdivider - 2;
   mselectiondivider.setbounds(0, topofbottomdivider, mright, bottomofbottomdivider);
   mselectiondivider.draw(canvas);
  }
 }
 
 private int sp2px(int sp) {
  float scale = mcontext.getresources().getdisplaymetrics().scaleddensity;
  return (int) (scale * sp + 0.5f);
 }
}

用法:

先实现pickvalueview.onselectedchangelistener

然后textview显示:

@override
public void onselected(pickvalueview view, object leftvalue, object middlevalue, object rightvalue) {
 string selectedstr = (string) leftvalue;
 tv_cj_num.settext(selectedstr);
}

完成,自定义弹窗里可以修改一些属性。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。