2013-07-07 32 views
8

我想在我的首選項屏幕中創建NumberPicker對話框。我已經作出了一個下面這一點:https://stackoverflow.com/a/5533295/2442638優先創建NumberPicker對話框

但是,對於我的第二次對話,我只想要一個微調,所以我已經適應了代碼如下:

import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.res.TypedArray; 
import android.preference.DialogPreference; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.NumberPicker; 

public class SnoozeTPP extends DialogPreference { 

    private int Minute = 0; 
    private NumberPicker np= null; 

    public static int getMinute(String time) { 
     String[] pieces = time.split(":"); 

     return (Integer.parseInt(pieces[1])); 
    } 

    public SnoozeTPP(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     setPositiveButtonText("Set"); 
     setNegativeButtonText("Cancel"); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     np = new NumberPicker(getContext()); 

     return (np); 
    } 

    @Override 
    protected void onBindDialogView(View v) { 
     super.onBindDialogView(v); 

     np.setMaxValue(60); 
     np.setValue(Minute); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) {                
     super.onDialogClosed(positiveResult); 

     if (positiveResult) { 

      Minute = np.getValue(); 

      String time = 0 + ":" + String.valueOf(Minute); 

      if (callChangeListener(time)) { 
       persistString(time); 
      } 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return (a.getString(index)); 
    } 

    @Override 
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 
     String time = null; 

     if (restoreValue) { 
      if (defaultValue == null) { 
       time = getPersistedString("08:00"); 
      } else { 
       time = getPersistedString(defaultValue.toString()); 
      } 
     } else { 
      time = defaultValue.toString(); 
     } 

     Minute = getMinute(time); 
    } 

} 

有沒有錯誤,對話框彈出正確的,但它的佈局似乎「搞砸了」:-)。藍線橫跨整個對話而不是數字的寬度。 enter image description here

問題是 - 如何正確設置佈局? (我相信有很多其他錯誤的!)

謝謝

+0

可以幫助你https://gist.github.com/tomstrummer/959884 –

+0

@RSenApps謝謝,我確實嘗試過,但是我無法完成它的工作:-( – RiThBo

回答

5
+0

這是一個很好的答案 – toobsco42

+2

Styleable在這裏:https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/res/values/attrs.xml – Gh61

+5

這個類似乎引用'com.android.internal'包。我是否需要按照[這裏](http://bit.ly/1fimVuv)的描述來構建'original-android.jar'來使用它,還是我會過度複雜化? –

1

這比解決一個解決辦法,但我希望它能幫助。添加一個虛擬textView解決了這個問題。我遇到了完全相同的問題。

我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textDummyEmpty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/textDummyEmpty" /> 

    <NumberPicker 
     android:id="@+id/numberPicker1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 

</LinearLayout> 

android:text="@string/textDummyEmpty" 

是一個空字符串。也許它也足以使用視圖而不是textView。

+0

感謝您的回答。最後,我用了一種不同的方法來創建它 - 它是開源代碼。我目前無法嘗試你的建議 - 但我會盡快做到這一點:-) – RiThBo

+1

@RiThBo請分享你如何解決問題,以便其他用戶也可以做到這一點。 – Richard

+1

@Richard我使用了CyanogenMod。它在GitHub的某個地方,我尋找確切的文件,但我找不到它了。對不起 – RiThBo

1

回報的LinearLayout在onCreateDialogView而不是NumberPicker如下:

@Override 
protected View onCreateDialogView() { 
    numberPicker = new NumberPicker(getContext()); 
    numberPicker.setMinValue(1); 
    numberPicker.setMaxValue(12); 
    numberPicker.setWrapSelectorWheel(false); 
    numberPicker.setValue(lastValue); 

    LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams 
      (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    pickerParams.gravity = Gravity.CENTER; 
    numberPicker.setLayoutParams(pickerParams); 

    LinearLayout layout = new LinearLayout(getContext()); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams 
      (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    layout.setLayoutParams(params); 

    layout.addView(numberPicker); 
    return layout; 

    //return numberPicker; 
} 
3

她e是簡單的一個例子,但工作NumberPickerPreference,節省了整數值介於1和100:

app screenshot

NumberPickerPreference.java

public class NumberPickerPreference extends DialogPreference { 
    private NumberPicker mPicker; 
    private Integer mNumber = 0; 

    public NumberPickerPreference(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setPositiveButtonText(android.R.string.ok); 
     setNegativeButtonText(android.R.string.cancel); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     mPicker = new NumberPicker(getContext()); 
     mPicker.setMinValue(1); 
     mPicker.setMaxValue(100); 
     mPicker.setValue(mNumber); 
     return mPicker; 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     if (positiveResult) { 
      // needed when user edits the text field and clicks OK 
      mPicker.clearFocus(); 
      setValue(mPicker.getValue()); 
     } 
    } 

    @Override 
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 
     setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue); 
    } 

    public void setValue(int value) { 
     if (shouldPersist()) { 
      persistInt(value); 
     } 

     if (value != mNumber) { 
      mNumber = value; 
      notifyChanged(); 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return a.getInt(index, 0); 
    } 
}