Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。












想要改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

7年前关闭。



Improve this question




我有两个数字选择器,我想获得用户从数字选择器中选择的值。然后将它们转换为String。

任何想法?

最佳答案

您可以通过调用getValue()获得当前选择的号码,例如。如果您有myPicker,则可以执行以下操作:

String value = "" + myPicker.getValue();

如果要在用户选择值时获取值,则需要实现NumberPicker.OnValueChangeListener接口(interface):
private class MyListener implements NumberPicker.OnValueChangeListener {
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        //get new value and convert it to String
        //if you want to use variable value elsewhere, declare it as a field
        //of your main function
        String value = "" + newVal;
    }
}

记住要设置您的听众,例如:
myPicker.setOnValueChangedListener(new MyListener());

10-08 06:59