我正在尝试在点击我的某个偏好设置时打开网站(而不是因为没有首选项而实际更改了偏好设置)。
问题是永远不会调用onPreferenceClick()
。
这是我的PreferenceActivity
:
public class About extends PreferenceActivity implements
OnPreferenceClickListener {
TextView tv_developer;
TextView tv_version;
String versionName;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.about);
Preference p_developer = (Preference) findPreference("p_developer");
p_developer.setOnPreferenceClickListener(this);
Preference p_licences = (Preference) findPreference("p_licences");
p_licences.setOnPreferenceClickListener(this);
}
@Override
public boolean onPreferenceClick(Preference pref) {
// TODO Auto-generated method stub
Log.i("Anything pressed", "YES");
if (pref.getKey().equals("p_developer")) {
Log.i("p_developer", "YES");
} else {
Log.i("p_developer", "NO");
}
return true;
}
}
这是xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="about"
android:title="About" >
<Preference
style="?android:preferenceInformationStyle"
android:key="p_licences"
android:title="p_licences" />
<PreferenceCategory android:title="Category" >
<Preference
style="?android:preferenceInformationStyle"
android:key="p_developer"
android:title="p_developer" />
</PreferenceCategory>
</PreferenceScreen>
我也发现这也没有做任何事情:
<Preference android:title="@string/prefs_web_page" >
<intent android:action="android.intent.action.VIEW"
android:data="http://www.example.com" />
</Preference>
答案 0 :(得分:3)
将android:enabled="true"
添加到Preference
:
<Preference
style="?android:preferenceInformationStyle"
android:key="p2"
android:enabled="true"
android:title="p2" />
答案 1 :(得分:1)
您正在使用:
PreferenceActivity.addPreferencesFromResource(R.layout.about);
不应该更像:
PreferenceActivity.addPreferencesFromResource(R.xml.yourxmlfilename);
由于您定义的首选项不是布局。 您需要加载.xml文件的名称,而不是.xml中首选项的键。
因此,将您的首选项.xml文件放在“xml”文件夹中,并为其命名为“mypreferences.xml”。然后,加载它:
PreferenceActivity.addPreferencesFromResource(R.xml.mypreferences);
在我的项目中,文件如下所示:(名称“settings.xml”)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Your Settings" >
<Preference
android:key="delete"
android:title="Delete User"
android:summary="Deletes the user." />
<!-- and so on -->
我通过调用:
将它添加到PreferenceActivity中PreferenceActivity.addPreferencesFromResource(R.xml.settings);
设置onClickListener:
Preference deletePref = (Preference) findPreference("delete");
deletePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// do stuff
return true;
}
});
它有效。
此外,您可以尝试为标题和密钥使用其他名称。
答案 2 :(得分:0)
这里有新鲜的答案。
您需要先声明侦听器。然后为每个(!)结束首选项对象附加该侦听器。像这样:
final static int CAMERA_SLEEP_TIME=0,
CAMERA_TIME=1,
RESET=2;
private Preference.OnPreferenceClickListener listener =new Preference.OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
String keyLcl = preference.getKey();
if(keyLcl.equals(cameraSleepTime))
triggerPrefsDialog(CAMERA_SLEEP_TIME);
else
if (keyLcl.equals(cameraTime))
triggerPrefsDialog(CAMERA_TIME);
else
if (keyLcl.equals(reset))
triggerPrefsDialog(RESET);
else
return false;
return true;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Resources resLcl = getResources();
cameraSleepTime=resLcl.getString(R.string.camera_sleep_time);
cameraTime=resLcl.getString(R.string.camera_time);
reset =resLcl.getString(R.string.reset);
PreferenceScreen sceenLcl = getPreferenceScreen();
PreferenceCategory pcLcl=(PreferenceCategory)sceenLcl.getPreference(1);
pcLcl.getPreference(0).setOnPreferenceClickListener(listener);
pcLcl.getPreference(1).setOnPreferenceClickListener(listener);
pcLcl=(PreferenceCategory)sceenLcl.getPreference(2);
pcLcl.getPreference(0).setOnPreferenceClickListener(listener);
l.a(pcLcl.getTitle());
}
相关的preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory
android:title="@string/switch_mode">
<CheckBoxPreference
android:key="@string/enable_switch"
android:defaultValue="true"
android:gravity="center"
android:title="@string/is_switched"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/seconds_category">
<Preference
android:key="@string/camera_time"
android:defaultValue="600000"
android:title="@string/camera_time"
/>
<Preference
android:key="@string/camera_sleep_time"
android:defaultValue="600000"
android:title="@string/camera_sleep_time"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/System">
<Preference
android:key="@string/reset"
android:defaultValue="false"
android:title="@string/reset"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/to_be_added">
</PreferenceCategory>
答案 3 :(得分:0)
对我来说,解决方案是扩展现有的PreferenceCategory
类(在支持库中)并覆盖isEnabled()
。此方法始终返回false
,因此我创建了一个名为ClickablePreferenceCategory
的类,并将其更改为true
。
除此之外,我还必须将android:selectable="true"
添加到我的自定义首选项xml节点。结果如下:
<PreferenceCategory
android:enabled="true"
android:key="@string/key_category_problem_solving"
android:selectable="true"
android:title="@string/problem_solving">
</PreferenceCategory>
ClickablePreferenceCategory
:
public class ClickablePreferenceCategory extends PreferenceCategory {
public ClickablePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public ClickablePreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ClickablePreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ClickablePreferenceCategory(Context context) {
super(context);
}
@Override
public boolean isEnabled() {
return true;
}
}