因此,我尝试用XML定义一个NumberPicker。我相信我已经正确完成了所有设置,但是在运行应用程序时,选择器设置为0,并且无法更改。
如果有任何不同,它在片段内。
代码如下:
private void setupNumberPicker(View view){
NumberPicker picker = view.findViewById(R.id.vocabNumberPicker);
int wordCount = ((Globals) getContext().getApplicationContext()).getDictSize();
String[] values = new String[wordCount - Constants.MINIMUM_WORDS_REQUIRED_FOR_TEST + 1];
int k = 0;
System.out.println("VALUES REQUIRED: " + values.length);
for(int i = Constants.MINIMUM_WORDS_REQUIRED_FOR_TEST; i < wordCount + 1; i++){
System.out.println("DEBUG - Loop" + k);
System.out.println("DEBUG - i = " + i);
values[k] = Integer.toString(i);
k++;
System.out.println("Successful\n");
}
picker.setMaxValue(wordCount);
picker.setMinValue(Constants.MINIMUM_WORDS_REQUIRED_FOR_TEST);
picker.setWrapSelectorWheel(false);
picker.setDisplayedValues(values);
picker.setValue(Constants.MINIMUM_WORDS_REQUIRED_FOR_TEST);
}
输出:
I/System.out: VALUES REQUIRED: 4
I/System.out: DEBUG - Loop0
DEBUG - i = 2
Successful
I/System.out: DEBUG - Loop1
I/System.out: DEBUG - i = 3
I/System.out: Successful
I/System.out: DEBUG - Loop2
DEBUG - i = 4
Successful
DEBUG - Loop3
DEBUG - i = 5
Successful
XML中的NumberPicker(以防万一):
<NumberPicker
android:id="@+id/vocabNumberPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vocabHowManyQuestionsTextView"/>
setupNumberPicker从onCreateView调用