如何实现此自定义数字选择器?

时间:2016-12-25 06:53:00

标签: android datepicker numberpicker

我试图制作这样的东西

This is what i wanna achieve
你如何实现第二个值和第三个值选择器?
我需要更改为datepicker吗?

*编辑

第二个值我试过这个方法:

private String[] getDatesFromCalender() {
    Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("GMT+7"));
    Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("GMT+7"));

    List<String> dates = new ArrayList<String>();
    DateFormat dateFormat = new SimpleDateFormat("EE, dd MMM", new Locale("en", "Singapore"));
    dates.add(dateFormat.format(c1.getTime()));

    for (int i = 0; i < 60; i++) {
        c1.add(Calendar.DATE, 1);
        dates.add(dateFormat.format(c1.getTime()));
    }
    c2.add(Calendar.DATE, -60);

    for (int i = 0; i < 60; i++) {
        c2.add(Calendar.DATE, 1);
        dates.add(dateFormat.format(c2.getTime()));
    }
    return dates.toArray(new String[dates.size() - 1]);
}

然后我把它放在onCreate

 date = getDatesFromCalender();

并设置numberpicker

 private void setUpDatePicker() {
    npBookingDate.setDisplayedValues(date);
    npBookingDate.setMaxValue(date.length - 1);
    npBookingDate.setMinValue(0);
    npBookingDate.setFormatter(new NumberPicker.Formatter() {
        @Override
        public String format(int value) {
            return date[value];
        }
    });
}

结果是我想要的,但它在今天之前添加另一个日期检查图:
Here is what i tried
如何在今天之前禁用日期?

1 个答案:

答案 0 :(得分:0)

好的,为了在同一个对话框中显示两个数字选择器,我将发布一个代码片段,如下所示

LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
//
final NumberPicker aNumberPicker = new NumberPicker(mContext);
aNumberPicker.setMaxValue(50);
aNumberPicker.setMinValue(1);
//
final NumberPicker aNumberPickerA = new NumberPicker(mContext);
aNumberPickerA.setMaxValue(11);
aNumberPickerA.setMinValue(1);
aNumberPickerA.setDisplayedValues(new String[] { "Tea Cup", "Glass","Plate","Small Plate","Cutlets","Medium","Piece","Katori","Balls","Serving","egg"});
//
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.gravity = Gravity.CENTER;
//
LinearLayout.LayoutParams numPicerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
numPicerParams.weight = 1;
//
LinearLayout.LayoutParams qPicerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
qPicerParams.weight = 1;
//
LL.setLayoutParams(params);
LL.addView(aNumberPicker,numPicerParams);
LL.addView(aNumberPickerA,qPicerParams);

因此,对于三个数字选择器,您可以在线性布局中添加一个数字选择器视图,并将此视图附加到对话框片段,您可以实现功能