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

Android自定义wheelview随机选号效果

程序员文章站 2024-03-31 11:16:28
先看下利用wheelview实现滚动随机选择号码效果: 直接上代码 首页就是dialog显示不在描述 主要看dialog代码 package c...

先看下利用wheelview实现滚动随机选择号码效果:

Android自定义wheelview随机选号效果

直接上代码

首页就是dialog显示不在描述
主要看dialog代码

package com.yskj.jh.wheeldemo;

import android.app.dialog;
import android.content.context;
import android.os.bundle;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;
import android.widget.textview;
import android.widget.toast;

import com.yskj.jh.wheeldemo.wheel.adapters.abstractwheeltextadapter;
import com.yskj.jh.wheeldemo.wheel.views.onwheelchangedlistener;
import com.yskj.jh.wheeldemo.wheel.views.onwheelscrolllistener;
import com.yskj.jh.wheeldemo.wheel.views.wheelview;

import java.util.arraylist;
import java.util.list;


/**
 * created by administrator on 2016/4/7.
 */


public class snatchdialog extends dialog implements view.onclicklistener, onwheelchangedlistener {

 private context context;

 private textview tvnumberl, tvnumberc, tvnumberr;

 //数字控件
 private wheelview wvleft;
 private wheelview wvcenter;
 private wheelview wvright;

 //数字集合
 private list<string> list = new arraylist<string>();

 //选中的数字信息
 private string strleft;
 private string strcenter;
 private string strright = "1";

 private textview btnsure;//确定按钮
 private imageview btncancle;//取消按钮
 private textview btnrandom;//随机

 //回调函数
 private onsnatchclistener onsnatchclistener;

 private numberadapter adapter;

 //显示文字的字体大小
 private int maxsize = 26;
 private int minsize = 18;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.dialog_snatch);
 initview();
 }

 public snatchdialog(context context){
 super(context, r.style.sharedialog);
 this.context = context;
 }
 private void initview() {

 tvnumberl = (textview) findviewbyid(r.id.tv_number1);
 tvnumberc = (textview) findviewbyid(r.id.tv_number2);
 tvnumberr = (textview) findviewbyid(r.id.tv_number3);

 for (int i = 0; i < 10; i++) {
  list.add(i + "");
 }

 wvleft = (wheelview) findviewbyid(r.id.wv_snatch_left);
 wvcenter = (wheelview) findviewbyid(r.id.wv_snatch_center);
 wvright = (wheelview) findviewbyid(r.id.wv_snatch_right);
 btnsure = (textview) findviewbyid(r.id.tv_sure);
 btncancle = (imageview) findviewbyid(r.id.img_cancel);
 btnrandom = (textview) findviewbyid(r.id.tv_random);

 btnsure.setonclicklistener(this);
 btncancle.setonclicklistener(this);
 btnrandom.setonclicklistener(this);

 wvleft.addchanginglistener(this);
 wvcenter.addchanginglistener(this);
 wvright.addchanginglistener(this);

 wvleft.setcyclic(true);
 wvright.setcyclic(true);
 wvcenter.setcyclic(true);

 wvleft.setvisibleitems(3);

 wvcenter.setvisibleitems(3);

 wvright.setvisibleitems(3);

 wvleft.addscrollinglistener(new onwheelscrolllistener() {
  @override
  public void onscrollingstarted(wheelview wheel) {

  }

  @override
  public void onscrollingfinished(wheelview wheel) {
  string currenttext = (string) adapter.getitemtext(wheel.getcurrentitem());
  strleft = (string) adapter.getitemobject(wheel.getcurrentitem());
  settextviewsize(strleft, adapter);
  tvnumberl.settext(strleft);
  }
 });
 wvcenter.addscrollinglistener(new onwheelscrolllistener() {
  @override
  public void onscrollingstarted(wheelview wheel) {

  }

  @override
  public void onscrollingfinished(wheelview wheel) {
  string currenttext = (string) adapter.getitemtext(wheel.getcurrentitem());
  strcenter = (string) adapter.getitemobject(wheel.getcurrentitem());
  settextviewsize(strcenter, adapter);
  tvnumberc.settext(strcenter);
  }
 });
 wvright.addscrollinglistener(new onwheelscrolllistener() {
  @override
  public void onscrollingstarted(wheelview wheel) {

  }

  @override
  public void onscrollingfinished(wheelview wheel) {
  string currenttext = (string) adapter.getitemtext(wheel.getcurrentitem());
  strright = (string) adapter.getitemobject(wheel.getcurrentitem());
  settextviewsize(strright, adapter);
  tvnumberr.settext(strright);
  }
 });
 /**
  * 设置适配器
  */
 adapter = new numberadapter(context, list, 0, maxsize, minsize);
 wvleft.setviewadapter(adapter);
 wvleft.setcurrentitem(0);

 wvcenter.setviewadapter(adapter);
 wvcenter.setcurrentitem(0);
 wvright.setviewadapter(adapter);
 wvright.setcurrentitem(1);

 }

 @override
 public void onchanged(wheelview wheel, int oldvalue, int newvalue) {

 }


 public interface onsnatchclistener {
 void onclick(string strleft, string strcenter, string strright);
 }

 @override
 public void onclick(view v) {

 if (v == btnsure) {
  if (onsnatchclistener != null) {
  onsnatchclistener.onclick(strleft, strcenter, strright);
  }
  if (strleft == null) {
  strleft = "0";
  }
  if (strcenter == null) {
  strcenter = "0";
  }
  if (strright == null) {
  strright = "0";
  }
  if ((strleft + strcenter + strright).equals("000")) {
  toast.maketext(context, "不能为0", toast.length_short).show();
  } else {
  if (integer.parseint(strleft + strcenter + strright) > 0 && integer.parseint(strleft + strcenter + strright) <= 999) {
  }
  }
 }
 if (v == btncancle) {
  dismiss();
 }
 if (v == btnrandom) {
  int a = (int) (math.random() * 5000 + 1);
  int b = (int) (math.random() * 5000 + 1);
  int c = (int) (math.random() * 5000 + 1);
  wvleft.scroll(a, 500);
  wvright.scroll(b, 500);
  wvcenter.scroll(c, 500);

 }
 }


 //适配器
 public class numberadapter extends abstractwheeltextadapter {
 list<string> list;

 protected numberadapter(context context, list<string> list, int currentitem, int maxsize, int minsize) {
  super(context, r.layout.item_birth_year, no_resource, currentitem, maxsize, minsize);
  this.list = list;
  setitemtextresource(r.id.tempvalue);
 }

 @override
 public view getitem(int index, view cachedview, viewgroup parent) {
  view view = super.getitem(index, cachedview, parent);
  return view;
 }

 @override
 protected charsequence getitemtext(int index) {
  if (list != null && list.size() > 0) {
  return list.get(index);
  }
  return "";
 }

 @override
 protected object getitemobject(int index) {
  if (list != null && list.size() > 0) {
  return list.get(index);
  }
  return null;
 }

 @override
 public int getitemscount() {
  if (list != null) {
  return list.size();
  }
  return 0;
 }
 }

 public void settextviewsize(string curriteitemtext, numberadapter adapter) {
 arraylist<view> arraylist = adapter.gettestviews();
 int size = arraylist.size();
 string currenttext;
 for (int i = 0; i < size; i++) {
  textview textview = (textview) arraylist.get(i);
  currenttext = textview.gettext().tostring();
  if (curriteitemtext.equals(currenttext)) {
  textview.settextsize(maxsize);
  } else {
  textview.settextsize(minsize);
  }
 }
 }

 public void setonsnatchclistener(onsnatchclistener onsnatchclistener) {
 this.onsnatchclistener = onsnatchclistener;
 }
}

布局

<?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="385dp"
 android:layout_marginbottom="5dp"
 android:layout_marginleft="5dp"
 android:layout_marginright="5dp"
 android:background="#00000000"
 android:gravity="bottom"
 android:layout_gravity="bottom"
 android:orientation="vertical">

 <linearlayout
 android:id="@+id/ly_myinfo_changeaddress_child"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@drawable/address_edit_delete_ensure_bg"
 android:orientation="vertical"
 >

 <framelayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <textview
  android:layout_width="match_parent"
  android:layout_height="45dp"
  android:gravity="center"
  android:textsize="16sp"
  android:textcolor="#7a7a7c"
  android:text="选择幸运号" />

  <imageview
  android:id="@+id/img_cancel"
  android:layout_width="16dp"
  android:layout_height="16dp"
  android:layout_gravity="right|center"
  android:layout_marginright="17dp"
  android:clickable="true"
  android:src="@mipmap/cha" />
 </framelayout>

 <view
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:background="#f5f5f5" />
 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="110dp"
  android:gravity="center_vertical"
  android:orientation="horizontal">

  <com.yskj.jh.wheeldemo.wheel.views.wheelview
  android:id="@+id/wv_snatch_left"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_gravity="center_vertical"
  android:layout_weight="1" />

  <com.yskj.jh.wheeldemo.wheel.views.wheelview
  android:id="@+id/wv_snatch_center"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_gravity="center_vertical"
  android:layout_weight="1" />

  <com.yskj.jh.wheeldemo.wheel.views.wheelview
  android:id="@+id/wv_snatch_right"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_gravity="center_vertical"
  android:layout_weight="1" />
 </linearlayout>

 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="vertical">
  <linearlayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_margintop="8dp"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv_number"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="当前选中的号码是: "
   android:textcolor="#faa701"
   android:textsize="11sp"/>
  <textview
   android:id="@+id/tv_number1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textsize="11sp"
   android:textcolor="#faa701"
   android:text="0"/>
  <textview
   android:id="@+id/tv_number2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textsize="11sp"
   android:textcolor="#faa701"
   android:text="0"/>
  <textview
   android:id="@+id/tv_number3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textsize="11sp"
   android:textcolor="#faa701"
   android:text="1"/>
  </linearlayout>
  <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <textview
   android:id="@+id/tv_random"
   android:layout_width="0dp"
   android:layout_height="27dp"
   android:layout_weight="1"
   android:layout_margin="10dp"
   android:background="@drawable/round_corner_blue"
   android:gravity="center"
   android:text="我要随机选号"
   android:clickable="true"
   android:textsize="13sp"
   android:textcolor="#45a3f3" />
  <textview
   android:id="@+id/tv_sure"
   android:layout_width="0dp"
   android:layout_height="27dp"
   android:layout_weight="1"
   android:layout_margin="10dp"
   android:background="@drawable/round_corner_blue_btn"
   android:gravity="center"
   android:clickable="true"
   android:text="确定"
   android:textsize="15sp"
   android:textcolor="#ffffff" />
  </linearlayout>

 </linearlayout>
 </linearlayout>


</linearlayout>

源码下载:http://xiazai.jb51.net/201612/yuanma/androidwheeldemo(jb51.net).rar

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