NumPicker的使用

NumberPicker是一个选择数字的控件,结合Dialog使用,效果如下:

NumPicker的使用_第1张图片NumPicker的使用_第2张图片

但是其中的数字显示却是一个不小的问题。

NumberPick这个控件发现只有当你滑动之后才会选择值,这样会出现什么问题呢?就是当你第一次选了一个不是第一个值的值,后边想选第一个了,当你点了一下NumberPick这个控件,发现默认是第一个,你就没滑动,你以为会选上第一个值,其实并不会,你需要再滑动一下才会选到第一个。

还有当你有了确定和取消按钮后,当你按下“确定”后,下次再选择要先显示上次选择的结果,当你按下“取消”后,虽然你可能改变了NumberPicker的值,但是你并不能将这些值真的改变,因为你取消了,鄙人这里用了静态变量来存每次选择的值,确定之后显示在TextView里,以此值来作为最终选择的值,并以这里的值来确定按了“取消”按钮之前选择的值。

设置的详细过程如下:

//在类中声明静态变量
static int deadline_day,deadline_hour,deadline_min,max_choose; 
private void showTime(){
//        showToast(deadline_day+" "+deadline_hour+" "+deadline_min);
        NumberPicker numberPicker1,numberPicker2,numberPicker3;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.deadline, null);

        numberPicker1 = view.findViewById(R.id.number_picker1);
        numberPicker1.setMinValue(0);
        numberPicker1.setMaxValue(365);
        numberPicker1.setValue(deadline_day);
        numberPicker1.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//不可编辑

        numberPicker2 = view.findViewById(R.id.number_picker2);
        numberPicker2.setMinValue(0);
        numberPicker2.setMaxValue(23);
        numberPicker2.setValue(deadline_hour);
        numberPicker2.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//不可编辑

        numberPicker3 = view.findViewById(R.id.number_picker3);
        numberPicker3.setMinValue(0);
        numberPicker3.setMaxValue(59);
        numberPicker3.setValue(deadline_min);
        numberPicker3.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//不可编辑

        dialog.setView(view);// 将自定义的布局文件设置给dialog
        numberPicker1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                deadline_day = newVal;
//                showToast("D"+Integer.toString(deadline_day));
            }
        });
        numberPicker2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                deadline_hour = newVal;
//                showToast("H"+Integer.toString(deadline_hour));
            }
        });
        numberPicker3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                deadline_min = newVal;
//                showToast("M"+Integer.toString(deadline_min));
            }
        });

//        showToast(Integer.toString(num));
        view.findViewById(R.id.chose_btn).setOnClickListener(new View.OnClickListener() {//设置监听
            @Override
            public void onClick(View v) {
                String show_time = deadline_day+"天"+deadline_hour+"小时"+deadline_min+"分钟";
                time_btn.setText(show_time);
//                showToast(deadline_day+" "+deadline_hour+" "+deadline_min);
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String show_time2 = time_btn.getText().toString();
                String str1="",str2="",str3="";
                int i = 0;
                while(show_time2.charAt(i)!='天')
                {
                    if(show_time2.charAt(i)=='天')break;
                    str1+=show_time2.charAt(i);
                    i++;
                }i++;
                while(show_time2.charAt(i)!='小')
                {
                    if(show_time2.charAt(i)=='小')break;
                    str2+=show_time2.charAt(i);
                    i++;
                }i++;i++;
                while(show_time2.charAt(i)!='分')
                {
                    if(show_time2.charAt(i)=='分')break;
                    str3+=show_time2.charAt(i);
                    i++;

                }
                deadline_day = Integer.parseInt(str1);
                deadline_hour = Integer.parseInt(str2);
                deadline_min = Integer.parseInt(str3);
               // showToast(show_time2+" "+str1+str2+str3);
//                String []str3 = show_time2.split("天|小时|分钟");
//                deadline_day = Integer.getInteger(str3[0]);
               // showToast(deadline_day+" ");
//                deadline_hour = Integer.getInteger(str3[1]);
//                deadline_min = Integer.getInteger(str3[2]);
                dialog.dismiss();
            }
        });
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }

    private void showMax(){
        final NumberPicker numberPicker;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.choose_max, null);
        numberPicker = view.findViewById(R.id.number_picker);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(list.size());
        numberPicker.setValue(max_choose);
        numberPicker.setWrapSelectorWheel(false);//不循环滚动
        numberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);//不可编辑
        dialog.setView(view);// 将自定义的布局文件设置给dialog
        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
              max_choose = newVal;
            }
        });
//        showToast(Integer.toString(num));
        view.findViewById(R.id.chose_btn).setOnClickListener(new View.OnClickListener() {//设置监听
            @Override
            public void onClick(View v) {
                max_tv.setText(Integer.toString(max_choose));
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String max_str = max_tv.getText().toString();
                max_choose = Integer.parseInt(max_str);
                dialog.dismiss();
            }
        });
        dialog.setView(view, 0, 0, 0, 0);
        dialog.show();
    }



//Oncreate方法中对max_tv(设置最大值的TextView),time_btn(设置时间的TextView)设置监听
max_tv = findViewById(R.id.max_tv);
        max_tv.setText("0");
        max_tv.setOnClickListener(new View.OnClickListener() {//最大可选点击事件
            @Override
            public void onClick(View v) {
                showMax();
            }
        });

time_btn = findViewById(R.id.time_btn);
        time_btn.setText(deadline_day+"天"+deadline_hour+"小时"+deadline_min+"分钟");
        time_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTime();
            }
        });

 

XML文件:deadline



    
        
        
        
        
        
        
    


    

        

        

    


 

XML文件:choose_max




    

    

        

        

    


 

至于max_tv和time_btn所在的XML文件就自己写吧,两个简单的TextView。

你可能感兴趣的:(Android)