- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在尝试使用 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/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/
我目前正在尝试使用 http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html 实现
我在类中实现 SeekBarPreference 时遇到问题。 https://developer.android.com/reference/android/support/v7/preferenc
我只想在我的项目中包含一个只有 3 个值的 SeekBarPreference(紧急级别:低-中-高) 这可以在经典的 SeekBar 上轻松完成(请参见下面的示例),但我不知道如何将其包含在首选项中
SeekBarPreference 是怎么回事?让我展开... 如果我在 PreferenceScreen xml 中放置一个 SeekBarPreference,它可以正常工作(我可以在手机和模拟器
如何使用 SeekBarPreference 显示搜索栏值? 最佳答案 使用 JetPack,我可以简单地将以下属性添加到 SeekBarPreference XML 元素。 app:
我正在尝试将 SeekBarPreference 实现到我的偏好 XML 中。我曾尝试使用 MrBIMC seekbar ( https://github.com/MrBIMC/MaterialSee
您好,我想知道如何在扩展 Preference 的类中使用以下方法 findViewById(int id) ,该类定义了一个名为 setProgress(int) 的方法Progress) ,它使用
我在显示的 XML 文件中有一个 SeekBarPreference(并且 slider 有效),但是在我单击 OK 按钮后,该值似乎就消失了。 这是我的 XML 文件...
我正在使用 holoeverywhere 库,当我启动 ActivityPreference 时出现此错误: 06-22 22:54:23.425: E/AndroidRuntime(11853):
看完Material Design的设置部分可用here ,我遇到了以下屏幕截图: 显示的 3 个第一个首选项是搜索栏,但我找不到任何相关信息,即使在支持库中也是如此。 我是不是遗漏了什么,或者 Go
我在互联网上找到了这段代码,我想我会试一试。我把它放在我的偏好中,它完全符合我想要的方式,但我唯一的问题是我无法获得 SeekBar 的值。 这是我根据自己的喜好使用的代码链接: http://and
我正在尝试根据 docs 实现 androidx SeekBarPreference我也许可以在我的 xml 中使用 setMax 属性,但这样做时出现以下错误: 在 xml 中: 错误: root
我已经准备好了a simple test project对于我在 GitHub 上的问题。 在我的项目中有一个自定义内联 SeekBarPreference ,它大部分工作正常(当拖动搜索栏时它的摘要
我的 preferences.xml 中有以下首选项: 问题是这会呈现 Marglins 与 SwitchPreference 的标题非常不同的样式: 是否可以在我的 styles.xml 中添
首先,我是 Xamarin 和 Android 编程的新手。我已经创建了一个 SeekbarPreference,但是它们的某些布局无法正确显示。该值正在从框中掉落(见图)。 我的 styles.xm
我一直在尝试制作一个 Android 设置屏幕,其中包含一些 SeekBarPreferences,以选择从 0 到 10 的三个数字。但是,我的 OnPreferenceChanged 代码没有被调
我正在尝试从我的应用程序设置 (preferences.xml) 的布局中检索 SeekBarPreference。但是,当我尝试将 findPreference("font_size") 转换为 S
我是一名优秀的程序员,十分优秀!