Android SeekBar首选项

发布于 2024-10-18 07:26:32 字数 4142 浏览 5 评论 0 原文

我目前正在尝试使用 http: 实现 SeekBarPreference 类: //android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html 使用相对布局的教程。第一个问题是 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();
}

}

I'm currently trying to implement SeekBarPreference class using http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html tutorial with RelativeLayout. First problem is TextView preferenceText isn't showing at all.
Second problem is the bar can't be slided like those in regular preferences (like media volume bar)?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴意少年 2024-10-25 07:26:32

这里滑动的问题是由于 notifyChange() 调用造成的。这会以某种方式干扰滑动。我也在开发类似的小部件,因为我不喜欢对话框方法。

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

我编写了一个 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

The problem with sliding here is due to notifyChange() call. That is interfering with the sliding somehow. I am also working on similar widget because I do not like the Dialog approach.

As promised here is the solution that I added to my open source project:

I have written a SeekBarPreference class that is embeded widget inside the preferences activity instead of popup dialog. You can download the jar file from:

http://aniqroid.sileria.com/

The class documentation and usage is here: http://aniqroid.sileria.com/doc/api/com/sileria/android/view/SeekBarPreference.html

Also do not forget to checkout the handy companion class to auto show a summary when you change the SeekBar: http://aniqroid.sileria.com/doc/api/com/sileria/android/event/PrefsSeekBarListener.html

淡墨 2024-10-25 07:26:32

滑块在教程中通过将 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 );
}

Slider works in tutorial by changing onProgressChanged to

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 );
}
恍梦境° 2024-10-25 07:26:32

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

Regarding the inability to move the slider, this is because its width is set to 80 (see params2) which is too narrow to allow sliding. After setting a reasonable width for it, e.g. 400, and the associated text view and a few mods to get it to compile with my SDK version it worked for me. One advantage this version has over many others is that the layout is done in the code instead of XML which allows an app to easily instantiate multiple sliders.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文