2012-09-07 64 views
43

有沒有方法根據XML佈局設置NumberPicker的最小值,最大值和默認值?Android NumberPicker:設置最小值,最大值,來自XML的默認值

我從活動代碼中做它:

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

XML顯然是比較合適的,因爲它定義屬性,而不是行爲。

有沒有辦法使用XML佈局來設置它們?

+2

您可能需要自定義NumberPicker,然後到處使用它MyNumberPicker ...並在MyNumberPicker的構造函數中獲取所有屬性集和設置值... – MKJParekh

+0

@MKJParekh你知道如何設置setMaxValue setMinValue for Ti我選擇器? http://stackoverflow.com/questions/20188983/how-to-set-the-setmaxvalue-setminvalue-for-time-picker-dailog-fragment –

回答

50

我有同樣的問題,這是我如何解決它(根據MKJParekh的評論):

  1. 我創建了自己NumberPicker級

    package com.exaple.project; 
    
    import android.annotation.TargetApi; 
    import android.content.Context; 
    import android.os.Build; 
    import android.util.AttributeSet; 
    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, AttributeSet attrs) { 
         super(context, attrs); 
         processAttributeSet(attrs); 
        } 
    
        public MyNumberPicker(Context context, AttributeSet attrs, int defStyle) { 
         super(context, attrs, defStyle); 
         processAttributeSet(attrs); 
        } 
        private void processAttributeSet(AttributeSet attrs) { 
         //This method reads the parameters given in the xml file and sets the properties according to it 
         this.setMinValue(attrs.getAttributeIntValue(null, "min", 0)); 
         this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0)); 
        } 
    } 
    
  2. 現在,您可以在您的xml佈局文件中使用此NumberPicker

    <com.exaple.project.myNumberPicker 
        android:id="@+id/numberPicker1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:orientation="vertical" 
        max="100" 
        min="1" /> 
    

感謝MKJParekh對他有用的評論

+4

很好,謝謝分享。在Eclipse中使用圖形佈局編輯器時請注意:如果在此處更改任何內容,像min和max這樣的自定義屬性將被刪除,您必須再次手動添加它們。 – pgruetter

+2

請注意,如[docs](https://developer.android.com/training/custom-views/create-view)中所述,可以在'res/values/attrs.xml'中定義自定義XML屬性。 HTML#customattr)。這也使IDE /佈局編輯器能夠識別自定義屬性。該文檔還說'AttributeSet'不應該直接使用(如這裏),而應該檢索一個'TypedArray'。 – user905686

0

這裏是下面的Android Docs
(從而支持主題&的Android Studio設計預覽)的更新版本

值/ 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() 
     } 
    } 

} 

......或NumberPickerWithXml.java(未經測試):

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(); 
     } 
    } 

} 

使用你的佈局:

<?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> 
相關問題