How to get the selected number from NumberPicker? [closed]

核能气质少年 提交于 2019-12-23 09:56:54

问题


I have two number pickers, I want to get the value that user chose from thos number pickers. then convert them to String.

Any idea?


回答1:


You can get current picked number by calling getValue(), eg. if you have myPicker, you can do this:

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

If you want to get the value when it's selected by user, you need to implement 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;
    }
}

Remember to set your listener, eg:

myPicker.setOnValueChangedListener(new MyListener());


来源:https://stackoverflow.com/questions/20181836/how-to-get-the-selected-number-from-numberpicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!