Java 如何从 NumberPicker 中获取所选号码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20181836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the selected number from NumberPicker?
提问by user2977338
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?
任何的想法?
采纳答案by Melquiades
You can get current picked number by calling getValue(), eg. if you have myPicker, you can do this:
您可以通过调用 getValue() 获取当前选择的号码,例如。如果你有 myPicker,你可以这样做:
String value = "" + myPicker.getValue();
If you want to get the value when it's selected by user, you need to implement NumberPicker.OnValueChangeListener interface:
如果要获取用户选中时的值,则需要实现 NumberPicker.OnValueChangeListener 接口:
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());