Tutaj jest zaktualizowana wersja, która podąża za Android Docs
(a więc obsługuje Skórki & Android Studio projektant podgląd)
wartości/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NumberPickerWithXml">
<attr name="maxValue" format="integer" />
<attr name="minValue" format="integer" />
<attr name="defaultValue" format="integer" />
</declare-styleable>
</resources>
NumberPickerWithXml.kt:
package com.example.library.ui
import android.content.Context
import android.util.AttributeSet
import android.widget.NumberPicker
import com.example.library.ui.R
class NumberPickerWithXml : NumberPicker {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
processXmlAttributes(attrs)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
processXmlAttributes(attrs, defStyleAttr)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
processXmlAttributes(attrs, defStyleAttr, defStyleRes)
}
private fun processXmlAttributes(attrs: AttributeSet, defStyleAttr: Int = 0, defStyleRes: Int = 0) {
val attributes = context.theme.obtainStyledAttributes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes)
try {
this.minValue = attributes.getInt(R.styleable.NumberPickerWithXml_minValue, 0)
this.maxValue = attributes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0)
this.value = attributes.getInt(R.styleable.NumberPickerWithXml_defaultValue, 0)
} finally {
attributes.recycle()
}
}
}
... lub NumberPickerWithXml.java (niesprawdzone):
package com.example.library.ui
import android.content.Context
import android.util.AttributeSet
import android.widget.NumberPicker
import com.example.library.ui.R
public class NumberPickerWithXml extends NumberPicker {
public NumberPickerWithXml(Context context) {
super(context);
}
public NumberPickerWithXml(Context context, AttributeSet: attrs) {
super(context, attrs);
processXmlAttributes(attrs, 0, 0);
}
public NumberPickerWithXml(Context context, AttributeSet: attrs, int: defStyleAttr) {
super(context, attrs, defStyleAttr);
processXmlAttributes(attrs, defStyleAttr, 0);
}
public NumberPickerWithXml(Context context, AttributeSet: attrs, int: defStyleAttr, int: defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
processXmlAttributes(attrs, defStyleAttr, defStyleRes);
}
private void processXmlAttributes(AttributeSet: attrs, int: defStyleAttr, int: defStyleRes) {
TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes)
try {
this.minValue = attributes.getInt(R.styleable.NumberPickerWithXml_minValue, 0);
this.maxValue = attributes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0);
this.value = attributes.getInt(R.styleable.NumberPickerWithXml_defaultValue, 0);
} finally {
attributes.recycle();
}
}
}
Wykorzystanie w układzie:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<com.example.library.ui.NumberPickerWithXml
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:defaultValue="30"
custom:maxValue="120"
custom:minValue="0" />
</LinearLayout>
może trzeba dostosować NumberPicker a następnie używać go wszędzie MyNumberPicker ... i w Konstruktorze MyNumberPicker uzyskać wszystkie zestaw atrybutów i ustawić wartości ... – MKJParekh
@MKJParekh Czy wiesz, jak ustawić setMaxValue setMinValue dla Ti mój selektor? http://stackoverflow.com/questions/20188983/how-to-set-the-setmaxvalue-setminvalue-for-time-picker-dailog-fragment –