有没有办法使用Android NumberPicker小部件来选择字符串而不是整数?
答案 0 :(得分:220)
NumberPicker picker = new NumberPicker(this);
picker.setMinValue(0);
picker.setMaxValue(2);
picker.setDisplayedValues( new String[] { "Belgium", "France", "United Kingdom" } );
答案 1 :(得分:36)
NumberPicker numberPicker = new NumberPicker(this);
String[] arrayString= new String[]{"hakuna","matata","timon","and","pumba"};
numberPicker.setMinValue(0);
numberPicker.setMaxValue(arrayString.length-1);
numberPicker.setFormatter(new NumberPicker.Formatter() {
@Override
public String format(int value) {
// TODO Auto-generated method stub
return arrayString[value];
}
});
希望有所帮助!
答案 2 :(得分:15)
String value = String.valueOf(picker.getValue());
你可能意味着别的东西,但你的意思并不是很清楚。
更新:
您可以通过设置自己的格式化程序来伪造它:
CountryFormatter implements Formatter {
public String toString(int value) {
switch(value) {
case 0:
return "England";
case 1:
return "France";
}
return "Unknown";
}
}
picker.setFormatter(new CountryFormatter());
getValue()仍将返回一个int,因此您可能希望将国家/地区的名称映射到其ID。
增加: NumberPicker的实现有:
public void setRange(int start, int end, String[] displayedValues)
这似乎比你想要的更好,然后上面的格式化程序..虽然它没有在文档中提到所以可能不是公共API的一部分
答案 3 :(得分:6)
试试这个解决方案,可以帮到你
NumberPicker pickers = new NumberPicker(this);
String[] arrayPicker= new String[]{"abc","def","ghi","jkl","mno"};
//set min value zero
pickers.setMinValue(0);
//set max value from length array string reduced 1
pickers.setMaxValue(arrayPicker.length - 1);
//implement array string to number picker
pickers.setDisplayedValues(arrayPicker);
//disable soft keyboard
pickers.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
//set wrap true or false, try it you will know the difference
pickers.setWrapSelectorWheel(false);
//create button to test selected text string
btnPicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//get position (int) from number picker
int pos = pickers.getValue();
//get string from number picker and position
String selecPicker = arrayPicker[pos];
//test toast to get selected text string
Toast.makeText(context, selecPicker , Toast.LENGTH_SHORT).show();
}
});
答案 4 :(得分:0)
这对我有用。
node 'testnode' {
include testmodule::basics
# here I would like to add the user to a group
# something like this (obviously does not work because of duplicate resource)
user { 'testuser':
groups => 'testgroup',
membership => 'minimum',
}
}