Я создаю новый пользовательский виджет для NumberPicker, и я хочу, чтобы заполнить элементы массивом в array.xml для этого действия. Я создаю новое определение в array.xml:Android parse array attr в пользовательском виджете для пользовательского NumberPicker
<integer-array name="Items">
<item>1393</item>
<item>1394</item>
<item>1395</item>
<item>1396</item>
<item>1397</item>
</integer-array>
применение атр является:
<declare-styleable name="CNumberPicker">
<attr name="maxValue" format="reference" />
<attr name="minValue" format="string" />
<attr name="value" format="string" />
<attr name="displayedValues" format="string" />
<attr name="focusable" format="boolean" />
<attr name="focusableInTouchMode" format="boolean" />
</declare-styleable>
определить xustom виджет в макет XML:
<com.sample.widget.CNumberPicker
android:id="@+id/np_choose_year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.20"
android:gravity="center_horizontal"
app:maxValue="@array/Items"
app:minValue="0">
</com.sample.widget.CNumberPicker>
сейчас пользовательский виджет класс я должен разобрать массив с setMaxValue
для NumberPicker как:
public class CNumberPicker extends net.simonvt.numberpicker.NumberPicker implements OnCheckedChangeListener {
private String maxValue;
private String minValue;
private String value;
public CNumberPicker(Context context) {
super(context);
//LayoutInflater.from(context).inflate(R.layout.dialog_schedule_date_time, this);
}
public CNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CNumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
private void init(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CNumberPicker);
final int id = a.getResourceId(R.styleable.CNumberPicker_maxValue, 0);
/*final int[] values;
if (id != 0) {
values = getResources().getIntArray(id);
}
setMaxValue(Collections.max(Arrays.asList(values)));*/
setMaxValue(Integer.parseInt(a.getString(R.styleable.CNumberPicker_maxValue)));
setMinValue(Integer.parseInt(a.getString(R.styleable.CNumberPicker_minValue)));
setValue(Integer.parseInt(a.getString(R.styleable.CNumberPicker_value)));
a.recycle();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setValue(19);
}
}
setMaxValue
только принять Int и не мог разобрать массив, как решить эту проблему?
массива имеет несколько значений, так почему вы назначьте его максимальное значение? –