使用NumberPick实现滚动选择
2016-09-20 12:25
204 查看
使用numberpick可以很简单的实现滚动选择,但是条数只限3条
2.在activity中调用
3. 如何更改NumberPicker分割线颜色
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" 4000 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
对话框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; } } }
相关文章推荐
- 日期滚动选择(使用DatePicker实现)
- 使用JAVASCRIPT实现动态增加、删除选择项
- 交换4.3.1使用外置路由器实现VLAN间路由选择
- 使用DOS命令实现选择菜单
- 使用if…else,if…else实现多分支选择结构
- setInterval的使用练习——实现网页标题title的滚动效果。
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 使用jQuery简单实现产品展示的图片左右滚动功能
- 使用jQuery实现简单上下无缝滚动
- 使用JavaScrpt实现网页字体的自由选择
- 有效的使用和设计COM智能指针——条款3:按照功能和实现原理选择合适的智能指针
- 通过VS中的数据源选择对话框实现数据库连接配置(用户使用)
- 使用dwr实现下拉框级联和js实现动态选择option的selected状态方法总结
- 使用dwr实现下拉框级联和js实现动态选择option的selected状态方法总结
- 使用ScrollView实现滚动效果 出现 ScrollView can host only one direct child (ScrollView只能包裹一个直接子元素)
- 使用JAVASCRIPT实现动态增加、删除选择项
- 使用JavaScript实现新闻滚动效果
- 通过Visual Studio 2005 中的数据源选择对话框实现数据库连接配置(用户使用)
- C language 使用if…else if…else语句实现多分支选择结构*(语法结构同C#)
- 使用jquery实现的漂亮的滚动图片