gpt4 book ai didi

Android SeekBarPreference

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:40 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

我编写了一个 SeekBarPreference 类,该类将小部件嵌入到首选项 Activity 中,而不是弹出对话框。您可以从以下位置下载 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

关于Android SeekBarPreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5050272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com