【问题标题】:Show certain value in NumberPicker在 NumberPicker 中显示特定值
【发布时间】:2016-10-27 07:51:43
【问题描述】:

用户打开 NumberPicker 时是否可以显示某些值?例如,我有最小值 = 10,最大值 = 50;

final NumberPicker picker = new NumberPicker(MonitorActivity.this);
        picker.setMinValue(10);
        picker.setMaxValue(50);

打开 NumberPicker 时,我想显示 value = 30 而不是标准 10。 我该怎么做?

更新:

upTv = (TextView) findViewById(R.id.upper_tv);
 upTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show(180, 200, upTv);

            }
        });
public void show(int int1, int int2, final TextView tv)
    {

        String button1String = "OK";
        String button2String = "cancell";
        ad = new AlertDialog.Builder(MonitorActivity.this);
        ad.setTitle("Title");  



        final NumberPicker picker = new NumberPicker(MonitorActivity.this);
        picker.setMinValue(int1);
        picker.setMaxValue(int2);
        picker.setWrapSelectorWheel(false);
        final FrameLayout parent = new FrameLayout(MonitorActivity.this);
        parent.addView(picker, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER));
        ad.setView(parent);

        ad.setPositiveButton(button1String, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {


                int pickedValue = picker.getValue();
                tv.setText(String.valueOf(picker.getValue()));
                picker.setValue(pickedValue);
            }
        });
        ad.setNegativeButton(button2String, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {

            }
        });
        ad.setCancelable(true);

        ad.show();
    }

【问题讨论】:

  • 选择器在对话框内?请发布完整代码。
  • @NigamPatro 添加了
  • 这里的问题是,您在显示时没有设置值。需要在 setMaxValue(); 之后将默认值传递给 show() 和 setValue();

标签: android dialog numberpicker


【解决方案1】:
NumberPicker picker = new NumberPicker(MonitorActivity.this);
picker.setMinValue(10);
picker.setMaxValue(50);

您只需执行此操作即可默认显示 30。

picker.setValue(30);

【讨论】:

  • 它不起作用..我想总是动态地改变它。我的意思是用户第一次打开 NumberPicker 并显示最小值。下次用户打开 numberPicker 时,会显示用户上次转过的值等。我改变了我的代码..
  • @MrStuff88 请更改您的问题。根据您提出的问题,我据此给出了答案。
  • 我觉得是一样的
猜你喜欢
  • 2019-01-05
  • 2017-09-19
  • 2012-12-15
  • 2017-03-02
  • 1970-01-01
  • 2015-10-31
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多