Android NumberPicker: définit min, max, default à partir de XML

Existe-t-il un moyen de définir les valeurs minimale, maximale et par défaut d’un NumberPicker à partir de la disposition XML?

Je le fais depuis le code d’activité:

np = (NumberPicker) findViewById(R.id.np); np.setMaxValue(120); np.setMinValue(0); np.setValue(30); 

XML est évidemment plus approprié, car il définit la propriété, pas le comportement.

Existe-t-il un moyen de les définir en utilisant la mise en page XML?

J’ai eu le même problème, c’est comme ça que je l’ai résolu (d’après le commentaire de MKJParekh):

  1. J’ai créé ma propre classe NumberPicker

     package com.exaple.project; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.util.AtsortingbuteSet; import android.widget.NumberPicker; @TargetApi(Build.VERSION_CODES.HONEYCOMB)//For backward-compability public class MyNumberPicker extends NumberPicker { public MyNumberPicker(Context context) { super(context); } public MyNumberPicker(Context context, AtsortingbuteSet attrs) { super(context, attrs); processAtsortingbuteSet(attrs); } public MyNumberPicker(Context context, AtsortingbuteSet attrs, int defStyle) { super(context, attrs, defStyle); processAtsortingbuteSet(attrs); } private void processAtsortingbuteSet(AtsortingbuteSet attrs) { //This method reads the parameters given in the xml file and sets the properties according to it this.setMinValue(attrs.getAtsortingbuteIntValue(null, "min", 0)); this.setMaxValue(attrs.getAtsortingbuteIntValue(null, "max", 0)); } } 
  2. Maintenant, vous pouvez utiliser NumberPicker dans votre fichier de mise en page XML

      

Merci à MKJParekh pour son commentaire utile

Voici une version mise à jour qui suit les documents Android
(et prend donc en charge la présentation du concepteur et du studio Android)

values ​​/ attrs.xml:

         

NumberPickerWithXml.kt:

 package com.example.library.ui import android.content.Context import android.util.AtsortingbuteSet import android.widget.NumberPicker import com.example.library.ui.R class NumberPickerWithXml : NumberPicker { constructor(context: Context) : super(context) constructor(context: Context, attrs: AtsortingbuteSet) : super(context, attrs) { processXmlAtsortingbutes(attrs) } constructor(context: Context, attrs: AtsortingbuteSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { processXmlAtsortingbutes(attrs, defStyleAttr) } constructor(context: Context, attrs: AtsortingbuteSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { processXmlAtsortingbutes(attrs, defStyleAttr, defStyleRes) } private fun processXmlAtsortingbutes(attrs: AtsortingbuteSet, defStyleAttr: Int = 0, defStyleRes: Int = 0) { val atsortingbutes = context.theme.obtainStyledAtsortingbutes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes) try { this.minValue = atsortingbutes.getInt(R.styleable.NumberPickerWithXml_minValue, 0) this.maxValue = atsortingbutes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0) this.value = atsortingbutes.getInt(R.styleable.NumberPickerWithXml_defaultValue, 0) } finally { atsortingbutes.recycle() } } } 

… ou NumberPickerWithXml.java (non testé):

 package com.example.library.ui import android.content.Context import android.util.AtsortingbuteSet 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, AtsortingbuteSet: attrs) { super(context, attrs); processXmlAtsortingbutes(attrs, 0, 0); } public NumberPickerWithXml(Context context, AtsortingbuteSet: attrs, int: defStyleAttr) { super(context, attrs, defStyleAttr); processXmlAtsortingbutes(attrs, defStyleAttr, 0); } public NumberPickerWithXml(Context context, AtsortingbuteSet: attrs, int: defStyleAttr, int: defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); processXmlAtsortingbutes(attrs, defStyleAttr, defStyleRes); } private void processXmlAtsortingbutes(AtsortingbuteSet: attrs, int: defStyleAttr, int: defStyleRes) { TypedArray atsortingbutes = context.getTheme().obtainStyledAtsortingbutes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes) try { this.minValue = atsortingbutes.getInt(R.styleable.NumberPickerWithXml_minValue, 0); this.maxValue = atsortingbutes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0); this.value = atsortingbutes.getInt(R.styleable.NumberPickerWithXml_defaultValue, 0); } finally { atsortingbutes.recycle(); } } } 

Utilisation dans votre mise en page: