6

我目前正在尝试使用http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html教程和 RelativeLayout 来实现 SeekBarPreference 类。第一个问题是 TextView 偏好文本根本没有显示。第二个问题是栏不能像常规偏好中的那样滑动(如媒体音量栏)?

public class SeekBarPreference extends Preference implements
    OnSeekBarChangeListener {

public static int maximum    = 100;
public static int interval   = 5;
private float oldValue = 25;
private TextView Indicator;

public SeekBarPreference(Context context) {
    super(context);
}

public SeekBarPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected View onCreateView(ViewGroup parent) {

    float scale = getContext().getResources().getDisplayMetrics().density;

    RelativeLayout layout = new RelativeLayout(getContext());

    RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    RelativeLayout.LayoutParams sbarParams = new RelativeLayout.LayoutParams(
            Math.round(scale * 160),
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    RelativeLayout.LayoutParams indParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    TextView preferenceText = new TextView(getContext());
    preferenceText.setId(0);
    preferenceText.setText(getTitle());
    preferenceText.setTextSize(18);
    preferenceText.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);


    SeekBar sbar = new SeekBar(getContext());
    sbar.setId(1);
    sbar.setMax(maximum);
    sbar.setProgress((int)this.oldValue);
    sbar.setOnSeekBarChangeListener(this);

    this.Indicator = new TextView(getContext());
    this.Indicator.setTextSize(12);
    this.Indicator.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);
    this.Indicator.setText("" + sbar.getProgress());

    textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    sbarParams.addRule(RelativeLayout.RIGHT_OF, preferenceText.getId());
    indParams.setMargins(Math.round(20*scale), 0, 0, 0);
    indParams.addRule(RelativeLayout.RIGHT_OF, sbar.getId());

    preferenceText.setLayoutParams(textParams);
    sbar.setLayoutParams(sbarParams);
    this.Indicator.setLayoutParams(indParams);
    layout.addView(preferenceText);
    layout.addView(this.Indicator);
    layout.addView(sbar);
    layout.setId(android.R.id.widget_frame);

    return layout;
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    progress = Math.round(((float) progress)/interval) * interval;

    if(!callChangeListener(progress)) {
        seekBar.setProgress((int) this.oldValue);
        return;
    }

    seekBar.setProgress(progress);
    this.oldValue = progress;
    this.Indicator.setText("" + progress);
    updatePreference(progress);

    notifyChanged();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
protected Object onGetDefaultValue(TypedArray ta, int index) {
    int dValue = ta.getInt(index, 25);

    return validateValue(dValue);
}

@Override
protected void onSetInitialValue(boolean restoreValue,
        Object defaultValue) {
    int temp = restoreValue ? getPersistedInt(25) : (Integer)defaultValue;
    if(!restoreValue)
        persistInt(temp);

    this.oldValue = temp;
}

private int validateValue(int value) {
    if(value > maximum) 
        value = maximum;
    else if (value < 0)
        value = 0;
    else if (value % interval != 0)
        value = Math.round(((float) value)/interval) * interval;

    return value;
}

private void updatePreference(int newValue) {
    SharedPreferences.Editor editor = getEditor();
    editor.putInt(getKey(), newValue);
    editor.commit();
}

}

4

3 回答 3

2

这里滑动的问题是由于notifyChange()调用。那会以某种方式干扰滑动。我也在研究类似的小部件,因为我不喜欢 Dialog 方法。

正如这里所承诺的,这是我添加到我的开源项目中的解决方案:

我编写了一个 SeekBarPreference 类,它嵌入在首选项活动中,而不是弹出对话框中。您可以从以下位置下载 jar 文件:

http://aniqroid.sileria.com/

类文档和用法在这里:http ://aniqroid.sileria.com/doc/api/com/sileria/android/view/SeekBarPreference.html

当您更改 SeekBar 时,不要忘记检查方便的配套类以自动显示摘要:http: //aniqroid.sileria.com/doc/api/com/sileria/android/event/PrefsSeekBarListener.html

于 2011-12-23T19:24:18.347 回答
0

滑块通过将 onProgressChanged 更改为在教程中工作

public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser )
{
    progress = Math.round( ( (float) progress ) / interval ) * interval;
    persistInt(progress);
    callChangeListener( progress );
    seekBar.setProgress( progress );
    this.monitorBox.setText( progress + "" );
    updatePreference( progress );
}
于 2013-08-29T04:15:47.863 回答
0

关于无法移动滑块,这是因为它的宽度设置为 80(请参阅 params2),这太窄而无法滑动。在为它设置了一个合理的宽度后,例如 400,以及相关的文本视图和一些模块,让它与我的 SDK 版本一起编译,它对我有用。此版本相对于其他许多版本的一个优势是布局是在代码中完成的,而不是 XML,这允许应用程序轻松实例化多个滑块。

于 2015-10-05T16:57:41.497 回答