number picker preferences

This commit is contained in:
Sergey Solovyev 2011-09-19 15:27:39 +04:00
parent 60251243f0
commit 98407531ec
3 changed files with 85 additions and 46 deletions

View File

@ -0,0 +1,62 @@
package org.solovyev.android.view.prefs;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/19/11
* Time: 3:17 PM
*/
public class AbstractDialogPreference extends DialogPreference {
@NotNull
protected static final String androidns = "http://schemas.android.com/apk/res/android";
@NotNull
protected TextView splashText, valueText;
@NotNull
protected final Context context;
protected String dialogMessage, suffix;
public AbstractDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
dialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
suffix = attrs.getAttributeValue(androidns, "text");
}
@Override
protected LinearLayout onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
splashText = new TextView(context);
if (dialogMessage != null)
splashText.setText(dialogMessage);
layout.addView(splashText);
valueText = new TextView(context);
valueText.setGravity(Gravity.CENTER_HORIZONTAL);
valueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(valueText, params);
return layout;
}
}

View File

@ -16,33 +16,38 @@ import org.solovyev.android.view.widgets.NumberPicker;
* Date: 9/19/11 * Date: 9/19/11
* Time: 12:27 PM * Time: 12:27 PM
*/ */
public class IntervalPreference extends DialogPreference implements NumberPicker.OnChangedListener { public class IntervalPreference extends AbstractDialogPreference implements NumberPicker.OnChangedListener {
@NotNull @NotNull
private final Context context; private final NumberPicker leftBorder;
@NotNull
private final NumberPicker rightBorder;
public IntervalPreference(@NotNull Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public IntervalPreference(@NotNull Context context, AttributeSet attrs) { public IntervalPreference(@NotNull Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.context = context; this.leftBorder = new NumberPicker(context);
this.rightBorder = new NumberPicker(context);
} }
@Override @Override
public void onChanged(NumberPicker picker, int oldVal, int newVal) { public void onChanged(NumberPicker picker, int oldVal, int newVal) {
//To change body of implemented methods use File | Settings | File Templates. /* if (shouldPersist())
persistString(value);
callChangeListener(new Integer(value));*/
} }
@Override @Override
protected View onCreateDialogView() { protected LinearLayout onCreateDialogView() {
LinearLayout.LayoutParams params; final LinearLayout result = super.onCreateDialogView();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
return layout; final LinearLayout horizontal = new LinearLayout(context);
horizontal.setOrientation(LinearLayout.HORIZONTAL);
horizontal.addView(leftBorder);
horizontal.addView(rightBorder);
return result;
} }
} }

View File

@ -23,53 +23,24 @@ import org.jetbrains.annotations.NotNull;
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
*/ */
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener { public class SeekBarPreference extends AbstractDialogPreference implements SeekBar.OnSeekBarChangeListener {
private static final String androidns = "http://schemas.android.com/apk/res/android";
@NotNull @NotNull
private SeekBar seekBar; private SeekBar seekBar;
@NotNull
private TextView splashText, valueText;
@NotNull
private final Context context;
private String dialogMessage, suffix;
private int defaultValue, max, value = 0; private int defaultValue, max, value = 0;
public SeekBarPreference(Context context, AttributeSet attrs) { public SeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.context = context;
dialogMessage = attrs.getAttributeValue(androidns, "dialogMessage");
suffix = attrs.getAttributeValue(androidns, "text");
defaultValue = attrs.getAttributeIntValue(androidns, "defaultValue", 0); defaultValue = attrs.getAttributeIntValue(androidns, "defaultValue", 0);
max = attrs.getAttributeIntValue(androidns, "max", 100); max = attrs.getAttributeIntValue(androidns, "max", 100);
} }
@Override @Override
protected View onCreateDialogView() { protected LinearLayout onCreateDialogView() {
LinearLayout.LayoutParams params; final LinearLayout layout = onCreateDialogView();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6, 6, 6, 6);
splashText = new TextView(context);
if (dialogMessage != null)
splashText.setText(dialogMessage);
layout.addView(splashText);
valueText = new TextView(context);
valueText.setGravity(Gravity.CENTER_HORIZONTAL);
valueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(valueText, params);
seekBar = new SeekBar(context); seekBar = new SeekBar(context);
seekBar.setOnSeekBarChangeListener(this); seekBar.setOnSeekBarChangeListener(this);
@ -80,6 +51,7 @@ public class SeekBarPreference extends DialogPreference implements SeekBar.OnSee
seekBar.setMax(max); seekBar.setMax(max);
seekBar.setProgress(value); seekBar.setProgress(value);
return layout; return layout;
} }