Java 代码中的 SwitchPreference 和 CheckBoxPreference
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18414267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SwitchPreference and CheckBoxPreference in code
提问by Chiman
I am now creating a preference page for my app
我现在正在为我的应用程序创建一个首选项页面
After API-14, switchpreference is available. and i would like to use it to replace checkboxpreference on API14+ devices
在 API-14 之后,switchpreference 可用。我想用它来替换 API14+ 设备上的 checkboxpreference
It is easy to use res/xml
and res/xml-14
to get the correct xml resource
易于使用res/xml
并res/xml-14
获取正确的xml资源
However, in the coding part, it is not so convenient to switching the preference according to the API.
但是在编码部分,根据API切换偏好就不是那么方便了。
public class SettingActivity extends PreferenceActivity {
private CheckBoxPreference enable;
private SwitchPreference enablev14;
@Override
protected void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.setting);
if (Build.VERSION.SDK_INT < 14)
enable = (CheckBoxPreference) findPreference(key_enable);
else
enablev14 = (SwitchPreference) findPreference(key_enable);
}
...
}
Now my way is to use if-clause to check the Build.VERSION
and get the corresponding object to process it.
But it is quite inconvenient and hard to manage the code.
Do anyone has a smarter way to do it?
现在我的方法是使用if-clause来检查Build.VERSION
并获取相应的对象来处理它。但是代码管理起来非常不方便和困难。有没有人有更聪明的方法来做到这一点?
回答by Renan Ferrari
Maybe you could set an android:key
attribute to both of your SwitchPreference and CheckBoxPreference xml, just like this:
也许你可以android:key
为你的 SwitchPreference 和 CheckBoxPreference xml设置一个属性,就像这样:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_sync"
android:title="@string/pref_sync"
android:defaultValue="true" />
</PreferenceScreen>
and
和
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:key="pref_sync"
android:title="@string/pref_sync"
android:defaultValue="true" />
</PreferenceScreen>
And then you can check if this key returns true or false on your code, something like this:
然后你可以检查这个键是否在你的代码上返回 true 或 false,如下所示:
public class SettingActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.setting);
PreferenceManager preferenceManager = getPreferenceManager();
if (preferenceManager.getSharedPreferences().getBoolean("pref_sync", true)){
// Your switch is on
} else {
// Your switch is off
}
...
}
Hope this works for you.
希望这对你有用。
回答by Mark Larter
Depending on what the code would want to do with the preference instance, it might be as simple as casting to Preference
instead of a specific derived type, e.g.:
根据代码想要对首选项实例做什么,它可能就像转换为Preference
而不是特定的派生类型一样简单,例如:
enable = (Preference)findPreference(key_enable);
which would then allow something like:
这将允许类似的事情:
enable.setEnabled(true);
and eliminate the need to check API level in the code.
并且无需在代码中检查 API 级别。
回答by AleXqwq
In your Java code use TwoStatePreference
, which is a parent class for both CheckBoxPreference
and SwitchPreference
. It has all the methods you are likely to need in your use case.
在Java代码中使用TwoStatePreference
,这是两个父类CheckBoxPreference
和SwitchPreference
。它具有您在用例中可能需要的所有方法。
This is what the code sample you provided would look like:
这是您提供的代码示例的样子:
public class SettingActivity extends PreferenceActivity {
private TwoStatePreference enable;
@Override
protected void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.setting);
enable = (TwoStatePreference) findPreference(key_enable);
}
...
}