2013-07-07 17 views
8

Sto provando a creare una finestra di dialogo Numero nella mia schermata delle preferenze. Ho già fatto una seguendo questo: https://stackoverflow.com/a/5533295/2442638Crea preferito nella finestra di dialogo NumeroPiù

Tuttavia, per la mia seconda finestra di dialogo, voglio solo un filatore, così ho adattato il codice come segue:

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

} 

ci sono gli errori e gli schiocchi di dialogo su correttamente, ma il layout di esso sembra essere "incasinato" :-). La linea blu si estende su tutta la finestra di dialogo invece della sola larghezza dei numeri. enter image description here

La domanda è: come impostare il layout correttamente? (Sono sicuro che ci sono un sacco di altri errori, come pure!)

si

+0

possono aiutare https://gist.github.com/tomstrummer/959884 –

+0

@RSenApps Grazie, l'ho provato ma non ho potuto farlo funzionare :-( – RiThBo

risposta

5
+0

questa è un'ottima risposta – toobsco42

+2

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

+5

Questa classe sembra fare riferimento a 'com.android.internal' pacchetto. Devo creare un 'original-android.jar' come descritto [qui] (http://bit.ly/1fimVuv) per usarlo o sto complicando eccessivamente questo? –

1

Grazie Questo è più una soluzione che una soluzione, ma spero che aiuta. L'aggiunta di un visualizzatore di testo fittizio ha risolto il problema. Ho esattamente lo stesso problema.

mio XML File:

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

e

android:text="@string/textDummyEmpty" 

è una stringa vuota. Forse è anche abbastanza da usare solo una vista invece di una TextView.

+0

Grazie per la risposta. Alla fine ho usato un metodo diverso per crearlo - era un codice open source. Non sono in grado di provare ciò che hai suggerito al momento - ma proverò a farlo presto :-) – RiThBo

+1

@RiThBo per favore condividi come hai risolto il problema in modo che anche altri utenti possano farlo. – Richard

+1

@Richard Ho usato il CyanogenMod. È su GitHub da qualche parte, ho cercato il file esatto ma non riesco a trovarlo più. Scusa – RiThBo

1

cambio una LinearLayout in onCreateDialogView piuttosto che NumberPicker come di seguito:

@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

suo e è un esempio di semplice, ma NumberPickerPreference lavorando, risparmiando numero intero compreso tra 1 e 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); 
    } 
} 
Problemi correlati