2013-07-07 15 views
8

Próbuję utworzyć okno dialogowe NumberPicker na moim ekranie preferencji. Mam już jedną następujący sposób: https://stackoverflow.com/a/5533295/2442638Utwórz okno dialogowe NumberPicker w preferencjach

Jednak dla mojego drugiego okna, chcę tylko jednego pokrętła, więc muszę dostosować kod w następujący sposób:

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

} 

Brak błędy i wyskakuje okno dialogowe poprawnie, ale jego układ wydaje się być "pomieszany" :-). Niebieska linia rozciąga się w całym oknie, a nie tylko na szerokość liczb. enter image description here

Pytanie brzmi - jak poprawnie ustawić układ? (Jestem pewien, że istnieje wiele innych błędów, jak również!)

Dziękuję

+0

może pomóc https://gist.github.com/tomstrummer/959884 –

+0

@RSenApps Dziękuję, spróbowałem, ale nie mogłem go uruchomić :-( – RiThBo

Odpowiedz

5
+0

to jest świetna odpowiedź – toobsco42

+2

Styleable jest tutaj: https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/res/values/attrs.xml – Gh61

+5

Ta klasa wydaje się odwoływać do 'com.android.internal' pakiet. Czy muszę zbudować 'original-android.jar' zgodnie z opisem [tutaj] (http://bit.ly/1fimVuv), aby go użyć, czy też zbytnio to komplikuję? –

1

To więcej niż obejście rozwiązania, ale mam nadzieję, że to pomaga. Dodanie manekina tekstowego rozwiązało problem. Mam dokładnie ten sam problem.

Moje xml Plik:

<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> 

i

android:text="@string/textDummyEmpty" 

jest pusty. Może to również wystarczy, aby użyć tylko widoku zamiast textView.

+0

Dziękuję za odpowiedź. W końcu użyłem innej metody, aby ją stworzyć - był to kod open source. Nie jestem w stanie wypróbować tego, co zasugerowałeś w tej chwili - ale postaram się to zrobić wkrótce :-) – RiThBo

+1

@RiThBo podziel się tym, jak rozwiązałeś problem, aby inni użytkownicy mogli to zrobić. – Richard

+1

@Richard Użyłem CyanogenMod jeden. Jest gdzieś na GitHub, szukałem dokładnego pliku, ale nie mogę go już znaleźć. Przepraszam – RiThBo

1

powrotnym LinearLayout w onCreateDialogView zamiast NumberPicker jak poniżej:

@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

Her e jest przykładem prosty, ale działa NumberPickerPreference, oszczędzając wartość całkowitą pomiędzy 1 i 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); 
    } 
}