Android号码选择器

时间:2014-10-07 03:19:16

标签: android numberpicker

我发现我的号码选择器存在一些问题。

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog, null);
final NumberPicker np = (NumberPicker) view.findViewById(R.id.numberPicker1);
builder.setView(view)
        // Add action buttons
        .setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // sign in the user ...
                seekBar.setProgress(np.getValue());
                dialog.dismiss();
            }
        })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });
builder.setTitle("Pick value");
np.setMinValue(0);
np.setMaxValue(max - min);
int value_now;
switch (notification.dataType) {
    case 0:
        value_now = Math.round(UnitConverter.getTemperatureValue(notification.value));
        break;
    case 1:
        value_now = (int) notification.value;
        break;
    case 2:
        value_now = Math.round(UnitConverter.getPressureValue(notification.value));
        break;
    case 3:
        value_now = Math.round(UnitConverter.getRainfallValue(notification.value));
        break;
    case 4:
    default:
        value_now = Math.round(UnitConverter.getWindSpeedValue(notification.value));
        break;
}
np.setValue(value_now - min);
np.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int index) {
        return Integer.toString(index + min);
    }
});
np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
np.setWrapSelectorWheel(false);
AlertDialog dialog = builder.create();
dialog.show();

问题是,当有时value_now大于100时,将不显示当前值。但是,如果我滚动数字选择器,它将再次显示。 enter image description here

1 个答案:

答案 0 :(得分:0)

您是否在XML中设置最大值?

尝试在Min

之后设置它

np.setMinValue(0);

np.setMaxValue(6000);

OR

这使您的代码更长,但似乎是最好的方法: Take a look HERE