使用NumberPick实现滚动选择

使用numberpick可以很简单的实现滚动选择,但是条数只限3条

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="@dimen/selfinspect_title_height"
        android:background="@drawable/ipreg_selfinspect_bg"
        android:orientation="horizontal"
        android:gravity="center_vertical">
        <LinearLayout 
            android:layout_width="@dimen/selfinspect_unit_width"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp">
        <TextView 
            android:id="@+id/tv_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="取消"
            android:textColor="@color/white"
            android:textSize="@dimen/selfinspect_title_font"
            />
        </LinearLayout>
        <ImageView 
            android:layout_width="0.5dp"
            android:layout_height="fill_parent"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:background="@color/white"/>
        <LinearLayout 
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:gravity="center">
        <TextView 
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="体重"
            android:textColor="@color/white"
            android:textSize="@dimen/selfinspect_title_font"
            />
        </LinearLayout> 
                <ImageView 
            android:layout_width="0.5dp"
            android:layout_height="fill_parent"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:background="@color/white"/>
        <LinearLayout 
            android:layout_width="@dimen/selfinspect_unit_width"
            android:layout_height="wrap_content"
            android:paddingRight="20dp"
            android:gravity="right">
        <TextView 
            android:id="@+id/tv_sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="确定"
            android:textColor="@color/white"
            android:textSize="@dimen/selfinspect_title_font"
            />
        </LinearLayout>
    </LinearLayout>
     <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"  
    android:orientation="horizontal"
    android:background="@color/white">

    <NumberPicker
      android:id="@+id/np_high"
      android:layout_marginLeft="@dimen/selfinspect_item_interval"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="wrap_content"
      />

    <TextView
        android:id="@+id/tv_dot"
      android:layout_width="@dimen/selfinspect_item_interval"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:textColor="@color/selfinspect_font"
      android:textSize="@dimen/selfinspect_unit_font"
      android:gravity="center"
      android:textStyle="bold"
      android:text="." />

    <NumberPicker
      android:id="@+id/np_low"
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_unit"
      android:layout_width="@dimen/selfinspect_unit_width"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:textColor="@color/selfinspect_font"
      android:textSize="@dimen/selfinspect_unit_font"
      android:gravity="center"
      android:textStyle="bold"
      android:text="kg" />
  </LinearLayout>

</LinearLayout>

java

  1. 对话框style显示选择界面
package com._186soft.app.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;
import android.widget.TextView;

import com.preg.app.R;


public class SelfInspectDialogUtil{
private static ViewHolder holder=new ViewHolder();
public static String result=null,high=null,low=null;
public static void inspectPickDialog(Activity activity,
int minValue,String unit,final TextView tv_item){
    LayoutInflater mInflater=LayoutInflater.from(activity);
     View myView=mInflater.inflate(R.layout.selfinspect_number_picker, null);
     holder.tv_unit=(TextView) myView.findViewById(R.id.tv_unit);
     holder.tv_dot=(TextView) myView.findViewById(R.id.tv_dot);
     holder.np_high = (NumberPicker) myView.findViewById(R.id.np_high);
     holder.np_low = (NumberPicker) myView.findViewById(R.id.np_low);
     holder.tv_cancel=(TextView) myView.findViewById(R.id.tv_cancel);
     holder.tv_sure=(TextView) myView.findViewById(R.id.tv_sure);
     holder.tv_title=(TextView) myView.findViewById(R.id.tv_title);
     holder.tv_title.setText(title);
     holder.tv_unit.setText(unit);
     if(title.equals("胎动")){
         holder.tv_dot.setVisibility(View.GONE);
         holder.np_low.setVisibility(View.GONE);
     }
     //创建对话框实例
     final Dialog dialog = new AlertDialog.Builder(activity).create();
     //设置选择器监听事件
     holder.np_high.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, 
        int oldVal, int newVal) {
            // TODO Auto-generated method stub
            high=String.valueOf(newVal);
        }
    });
     //设置选择器的最大值,最小值,默认值
     holder.np_high.setMaxValue(maxValue);
     holder.np_high.setMinValue(minValue);
     holder.np_high.setValue(defValue); 
     high=String.valueOf(defValue);
     low=String.valueOf(0);
     holder.np_low.setMaxValue(9);
     holder.np_low.setMinValue(0);
     holder.np_low.setValue(0);
     holder.np_low.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, 
        int oldVal, int newVal) {
            // TODO Auto-generated method stub
            low=String.valueOf(newVal);;
        }
    });
    dialog.show();
    //对话框的大小
  dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
  ViewGroup.LayoutParams.WRAP_CONTENT);
  //设置对话框布局
       dialog.getWindow().setContentView(myView);
       //对话框位置
       dialog.getWindow().setGravity(Gravity.BOTTOM);

       holder.tv_sure.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(title.equals("胎动")){
                result=high;
            }else{
            result=high+"."+low;
            }
            tv_item.setText(result);
            dialog.dismiss();
        }
    });
       holder.tv_cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });
}
static class ViewHolder {
    TextView tv_cancel,tv_sure,tv_unit,tv_title,tv_dot;
    NumberPicker np_high,np_low;
    }
}

2.在activity中调用
SelfInspectDialogUtil.inspectPickDialog(HealthDataActivity.this,
"身高", 160, 220, 100,"cm",tv_height);

3. 如何更改NumberPicker分割线颜色

private void setDividerColor(NumberPicker picker, int color) {

java.lang.reflect.Field[] pickerFields = 
NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
    if (pf.getName().equals("mSelectionDivider")) {
        pf.setAccessible(true);
        try {
            ColorDrawable colorDrawable = new ColorDrawable(color);
            pf.set(picker, colorDrawable);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        break;
    }
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值