- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建自定义 Preference显示在 PreferenceFragment如此处所述:Building a Custom Preference .我的自定义首选项的外观和功能应为 SwitchPreference ,但有一个额外的 TextView
用于错误报告。
我已经实现了所有内容并且 UI 看起来不错,但是当我的 PreferenceFragment 显示时我无法初始化此 Preference!
Preference.onBindView()
的文档指出:
This is a good place to grab references to custom Views in the layout and set properties on them.
所以我做了:
@Override
protected void onBindView(View view) {
super.onBindView(view);
txtError = (TextView) view.findViewById(R.id.error);
}
public void setError(String errorMessage) {
txtError.setText(errorMessage);
notifyChanged();
}
但是,当我在 PreferenceFragment.onResume()
中调用 CustomSwitchPreference.setError(String)
时,我得到了 NPE,因为 txtError
为空。
我试图找到一些解决方法,但看起来 PreferenceFragment 中没有保证在所有底层 Preferences
初始化其 Views
之后调用的生命周期方法(我检查了 Preference.onBindView(View)
和 Preference.onCreateView(ViewGroup)
)。
这种行为没有任何意义 - 当显示 PreferenceFragment
时,应该有一些方法可以初始化底层 Preferences
的 UI。我怎样才能做到这一点?
注意:在 CustomPreferenceFragment.onResume()
中调用 customPreference.setTitle(String)
和 customPreference.setSummary(String()
工作正常. 这只是额外的 TextView
,我无法获取对它的引用...
CustomSwitchPreference.java:
public class CustomSwitchPreference extends SwitchPreference {
private TextView txtError;
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
@Override
protected View onCreateView(ViewGroup parent) {
setLayoutResource(R.layout.custom_switch_preference_layout);
return super.onCreateView(parent);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
txtError = (TextView) view.findViewById(R.id.error);
}
public void setError(String errorMessage) {
txtError.setText(errorMessage);
notifyChanged();
}
}
CustomPreferenceFragment.java:
public class CustomPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(PREFERENCES_FILE_NAME);
addPreferencesFromResource(R.xml.application_settings);
}
@Override
public void onResume() {
super.onResume();
Preference preference = findPreference("CUSTOM_PREF");
if (preference == null ||
!CustomSwitchPreference.class.isAssignableFrom(preference.getClass()))
throw new RuntimeException("couldn't get a valid reference to custom preference");
CustomSwitchPreference customPreference = (CustomSwitchPreference) preference;
customPreference.setError("error");
}
}
custom_switch_preference_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@android:id/widget_frame">
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"/>
<TextView
android:id="@android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"/>
<TextView
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"/>
</LinearLayout>
<FrameLayout
android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
应用程序设置.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.example.settings.CustomSwitchPreference
android:key="CUSTOM_PREF"/>
</PreferenceScreen>
最佳答案
我找不到解决此问题的合适方法 - 这种不一致感觉就像是 AOSP 中的生命周期错误,但我对此不是 100% 确定。
作为解决方法,我定义了一个回调接口(interface),CustomSwitchPreference
在 onBindView
方法中调用它,以便通知包含它的 PreferenceFragment
它已经初始化:
@Override
protected void onBindView(View view) {
super.onBindView(view);
txtError = (TextView) view.findViewById(R.id.error);
initializationListener.onInitialized(CustomSwitchPreference.this);
}
我想在 onResume
中执行的对此 CustomSwitchPreference
的所有操作现在都在 onInitialized
回调中执行。这是一个丑陋的解决方法,需要大量的样板文件,但它似乎有效。
关于Android:PreferenceFragment 与自定义 Preference 生命周期不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33723070/
为了使用androidx.preference,我应该将依赖添加到build.gradle 图片 A 来自 https://developer.android.com/jetpack/androidx
我正在尝试从我的应用程序设置 (preferences.xml) 的布局中检索 SeekBarPreference。但是,当我尝试将 findPreference("font_size") 转换为 S
我的需求 拥有一个充满偏好的主屏幕 (main_screen) 单击此 main_screen 中的一个首选项(pref1)会打开一个 设置子屏幕(sub_screen) 在这个 sub_screen
我正在使用 Eclipse 3.7 (Indigo) 作为 Eclipse 插件。该插件将通过产品文件打包为独立应用程序。安装将是多用户安装,如 this link 的场景 2 中所述。 。这样就会有
Preferences.flush() 和 Preferences.sync() 有什么区别? 来自Javadoc(flush()): Forces any changes in the conten
在我的 AppDelegate.m 中,如果用户还没有进入设置面板,我会尝试从我的 Preference Bundle 中初始化默认值。初始化运行,我看到了符合我偏好的正确数量的对象,但它们都是空的,
我有 CheckBoxPreference 和其他 2 个:一个是 Edit Test Pref。另一个是 ListBox Pref。我如何启用列表框首选项和禁用编辑文本首选项。 CheckBoxPr
我想使用“prefer source”选项安装所有由我维护的依赖项,以及使用“prefer dist”选项安装所有其他非我维护的依赖项。 Composer 可以吗?我在想可能有这样的东西,但我找不到:
查看PHP Composer的install命令的帮助,看到以下两个选项 $ composer help install Options: --prefer-source Fo
我无法解决错误的问题: incompatible types: no unique maximal instance exists for type variable T with upper bou
我的任务是使用 POSIX 线程编写读写实现。我想知道我的实现对于这两种情况是否正确: 已删除可怕的尝试 2 号尝试 读者首选: 变量: int readersActive_; sem_t lock_
我正在尝试使用上面的工具栏为我的应用创建一个设置窗口,以输入“后退”按钮等。但是我总是这个错误。 错误 呈现期间引发的异常:android.support.design.widget.AppBarLa
Coverity 实例详细信息: SA 版本:8.6连接:8.7 尝试将缺陷上传到 coverity 实例时,出现以下错误 正在连接到服务器 xxx.xxx.com:9090[错误] SSL 请求失败
在我当前的应用程序中,我已将偏好设置的导入从“android.preference.Preference”更改为“android.support.v7.preference.Preference”,因
在学习旧教程 (Créez des applications pour Android -> openclassroom) 时,我被 PreferenceActivity 类中的这个已弃用的方法 ad
我想在我的应用程序中使用“API Demos/app/preferences/1.preferences from XML”布局,但我不知道在哪里可以找到源文件。我在 API 演示示例中进行了搜索,但
我必须使用 weblogic.xml 在 Weblogic 12c 服务器 (12.2.1) 上 它是基于 Jersey 1.9 的 REST 应用程序。 (Jersey 1.x JAX-RS RI)
我有一些简单的代码,我正在使用它来学习使用路标库的 OAuth 身份验证。我正在尝试使用 java.util.prefs.Preferences 将请求 token 、 token secret 和
如果没有特殊要求,我们可以使用android提供的框架来创建系统样式的preference screen,在其内部可以包含preferencecategory和preference<con
我需要基于另一个应用构建一个应用。在我的 PreferenceScreen 中,我有 2 个 ListPreference 对象。第二个依赖于第一个并禁用,直到选择第一个(仍然需要弄清楚) 我想将第一
我是一名优秀的程序员,十分优秀!