NumberPicker的使用

来源:互联网 发布:js确认对话框 确认执行 编辑:程序博客网 时间:2024/05/28 20:18

NumberPicker是一个滚动的数值选择器,在github上可以下载到开源项目,将项目集合到自己的项目中,并对其进行修改。首先在NumberPicker.java中,

mInputText.setTextColor(getResources().getColor(R.color.mine));

可以修改numberpicker里面的字体的颜色,numberpicker里面线条的颜色可以通过修改图片资源得以修改。在layout里面添加一个xml,将自己想要的布局写在xml中,在自己的xml中引入numberpicker。
在java中写一个show()方法,并给它赋值int i;创建一个对话框,对话框显示的内容是自定义的xml,点击numberpicker中的确定按钮的时候就将numberpicker中的数值传递到activity中,点击取消按钮的时候,就把对话框取消掉。在init()中,start.setMaxValue(100);为数值里面的最大值,start.setMinValue(0);为最小值,newVal就是numberpicker当前的值

private void show(final int i) {AlertDialog.Builder builder = new AlertDialog.Builder(TwoActivity.this);LayoutInflater inflater = getLayoutInflater();View view = inflater.inflate(R.layout.popu_picker_time, null);builder.setView(view);init(view);final AlertDialog dialog = builder.create();dialog.show();btn2 = (Button) dialog.findViewById(R.id.main_light_btn2);btn1 = (Button) dialog.findViewById(R.id.main_light_btn1);btn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});btn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {switch (i) {case 1:activity_main_target_tv1.setText(k + "%");activity_main_target_tv1.setTextColor(Color.parseColor("#e7830d"));target_bt2.setTextColor(Color.parseColor("#e7830d"));break;default:break;}dialog.dismiss();}});} private void init(View view) {NumberPicker start = (NumberPicker) view.findViewById(R.id.numberPicker);start.setMaxValue(100);start.setMinValue(0);start.setFocusable(true);start.setFocusableInTouchMode(true);start.setOnValueChangedListener(new OnValueChangeListener() {@Overridepublic void onValueChange(NumberPicker picker, int oldVal,int newVal, EditText editText) {k = newVal;}});}

当然将xml的内容显示出来的除了用dialog的形式,还能用popupwindow的方式,下面是用popup的方式,原理是一样的:

 private void show(final int i) { View popuView = LayoutInflater.from(this).inflate( R.layout.popu_picker_time, null);   init(popuView); final PopupWindow popu = new PopupWindow(popuView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  popu.setBackgroundDrawable(new BitmapDrawable()); popu.setOutsideTouchable(true); popu.setFocusable(true); int[] location = new int[2]; more_page_row2.getLocationOnScreen(location); popuView.measure(0, 0); popu.showAtLocation(more_page_row2, Gravity.TOP, location[0], location[1]);  btn2 = (Button) popuView.findViewById(R.id.main_light_btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (i) { case 1: activity_main_target_tv1.setText("" + k); break; case 2: activity_main_target_tv2.setText("" + k); break; case 3: activity_main_target_tv3.setText("" + k); break; case 4: activity_main_target_tv4.setText("" + k); break; case 5: activity_main_target_tv5.setText("" + k); break; default: break; } popu.dismiss(); } }); }
0 0
原创粉丝点击