2017-02-23 22 views
0

我想用一組顯示值創建NumberPicker。顯示NumberPicker時,值不應該改變,但最小值和最大值根據用戶操作動態更改。NumberPicker上的setMinValue未選擇正確的最小值

下面是代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <NumberPicker 
     android:id="@+id/picker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true"/> 
</RelativeLayout> 

在主要活動

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final String[] values = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 

     NumberPicker picker = (NumberPicker) findViewById(R.id.picker); 
     picker.setDisplayedValues(values); 
     picker.setMaxValue(7); 
     picker.setMinValue(3); 

     picker.setWrapSelectorWheel(false); 
    } 
} 

當我設定的最大而不設置了最小值,我用「0」之間的顯示值結束,並且「7」(包括在內)。如果我將minValue設置爲0,則具有相同的行爲。 但是,如果我將minValue設置爲3,則結束顯示「0」和「4」之間的值。當它應該在「3」和「7」之間時。

我不明白爲什麼。我做錯了什麼或者是否存在組件問題?

回答

2

如果你看看setMinValue或setMaxValue的實施有一個評論,說:

* <strong>Note:</strong> The length of the displayed values array 
* set via {@link #setDisplayedValues(String[])} must be equal to the 
* range of selectable numbers which is equal to 
* {@link #getMaxValue()} - {@link #getMinValue()} + 1. 

因此,要解決這個最好是剛剛設置的值列表中,當你想更新顯示的值。

//do this once in onCreate and save the values list somewhere where you have access 
final List<String> values = new ArrayList<>(); 
for (int i = 0; i < 10; ++i) { 
    values.add(String.valueOf(i)); 
} 
NumberPicker picker = (NumberPicker) findViewById(R.id.picker); 
picker.setWrapSelectorWheel(false); 


//do this every time you want to change the displayed values and set minValue and maxValue accordingly 
//also make sure that you stay within the bounds of value 
int minValue = 3; 
int maxValue = 7; 

final List<String> subValues = values.subList(minValue, maxValue + 1); 
String[] subValuesArray = new String[subValues.size()]; 
subValuesArray = subValues.toArray(subValuesArray); 
picker.setDisplayedValues(subValuesArray); 
picker.setMinValue(0); 
picker.setMaxValue(subValues.size() - 1); 
+0

你能詳細解釋一下嗎?爲什麼它不能解決OP的問題?你不是也建議每當最小/最大值發生變化時顯示值應該改變(也許我誤解了)? – Bmuig

+0

我的歉意,我誤解了你的建議。它按預期工作。對不起! –

1

the documentation可以解釋。

當您設置最小值和最大值時,您可以定義可使用NumberPicker輸入的int值。所以setValue(int)只接受從最小值到最大值的值。

displayValues()是標籤覆蓋。並且將以下一種方式使用 - NumberPicker將採用索引value - min的標籤。

+0

那麼,這是否就意味着我有每個更改最小和最大時間來改變顯示的值的陣列? – Eselfar

+0

不幸的是,你必須 –

0

爲了簡潔到@Bmuig答案,只是一個輔助功能:

private void configurePicker(NumberPicker picker, ArrayList<String> values, int minValue, int maxValue, int currentValue) { 
    picker.setDisplayedValues(values) //to avoid out of bounds 
    picker.setMinValue(minValue); 
    picker.setMaxValue(maxValue); 
    picker.setValue(currentValue); 
    picker.setDisplayedValues((String[]) values.subList(minValue, maxValue + 1).toArray()); 
}