我想在我的首選項屏幕中創建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);
}
}
有沒有錯誤,對話框彈出正確的,但它的佈局似乎「搞砸了」:-)。藍線橫跨整個對話而不是數字的寬度。
問題是 - 如何正確設置佈局? (我相信有很多其他錯誤的!)
謝謝
可以幫助你https://gist.github.com/tomstrummer/959884 –
@RSenApps謝謝,我確實嘗試過,但是我無法完成它的工作:-( – RiThBo