Answers:
巴德尔
您需要设置android:key
项目,然后在您的代码中可以执行...
假设您在XML中使用以下内容:
<Preference android:title="About" android:key="myKey"></Preference>
然后,您可以在代码中执行以下操作:
Preference myPref = (Preference) findPreference("myKey");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
//open browser or intent here
return true;
}
});
findPreference
不推荐使用。
PreferenceActivity.findPreference
不推荐使用。但是,文档说“现在应该在新的PreferenceFragment类中找到他的功能。如果您在旧模式下使用PreferenceActivity,则此处的文档适用于此处不推荐使用的API。”,并且PreferenceFragment.findPreference
不推荐使用(从API开始) 23级,在撰写本文时为最新水平)。因此,findPreference
它本身不被弃用;不建议直接使用PreferenceActivity
。Google希望我们转向,PreferenceFragment
而不是放弃findPreference
。
onPreferenceClick
,返回true
是否处理了单击false
。
启动一个网站:
<PreferenceScreen android:title="website">
<intent
android:action="android.intent.action.VIEW"
android:data="http://www.example.com"
/>
</PreferenceScreen>
发起特定活动:
<PreferenceScreen android:title="something">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.example.foo"
android:targetClass="com.example.foo.SomeActivity"
/>
</PreferenceScreen>
您还可以使用“ android:mimetype”设置模仿类型。
intent-filter
在清单中指定任何内容吗SomeActivity
,因为得到了ActivityNotFoundException
:(
您需要使用onPreferenceTreeClick事件。
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
/** TODO: Refactor and get rid of the if's using subclasses */
if (mGsmUmtsOptions != null &&
mGsmUmtsOptions.preferenceTreeClick(preference) == true) {
return true;
} else if (mCdmaOptions != null &&
mCdmaOptions.preferenceTreeClick(preference) == true) {
if (Boolean.parseBoolean(
SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
mClickedPreference = preference;
// In ECM mode launch ECM app dialog
startActivityForResult(
new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
REQUEST_CODE_EXIT_ECM);
}
return true;
} else if (preference == mButtonPreferredNetworkMode) {
//displays the value taken from the Settings.System
int settingsNetworkMode = android.provider.Settings.Secure.getInt(mPhone.getContext().
getContentResolver(), android.provider.Settings.Secure.PREFERRED_NETWORK_MODE,
preferredNetworkMode);
mButtonPreferredNetworkMode.setValue(Integer.toString(settingsNetworkMode));
return true;
} else if (preference == mButtonDataRoam) {
if (DBG) log("onPreferenceTreeClick: preference == mButtonDataRoam.");
//normally called on the toggle click
if (mButtonDataRoam.isChecked()) {
// First confirm with a warning dialog about charges
mOkClicked = false;
new AlertDialog.Builder(this).setMessage(
getResources().getString(R.string.roaming_warning))
.setTitle(android.R.string.dialog_alert_title)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setPositiveButton(android.R.string.yes, this)
.setNegativeButton(android.R.string.no, this)
.show()
.setOnDismissListener(this);
} else {
mPhone.setDataRoamingEnabled(false);
}
return true;
} else if (preference == mButtonDataEnabled) {
if (DBG) log("onPreferenceTreeClick: preference == mButtonDataEnabled.");
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
cm.setMobileDataEnabled(mButtonDataEnabled.isChecked());
return true;
} else if (preference == mLteDataServicePref) {
String tmpl = android.provider.Settings.Secure.getString(getContentResolver(),
android.provider.Settings.Secure.SETUP_PREPAID_DATA_SERVICE_URL);
if (!TextUtils.isEmpty(tmpl)) {
TelephonyManager tm = (TelephonyManager) getSystemService(
Context.TELEPHONY_SERVICE);
String imsi = tm.getSubscriberId();
if (imsi == null) {
imsi = "";
}
final String url = TextUtils.isEmpty(tmpl) ? null
: TextUtils.expandTemplate(tmpl, imsi).toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} else {
android.util.Log.e(LOG_TAG, "Missing SETUP_PREPAID_DATA_SERVICE_URL");
}
return true;
} else {
// if the button is anything but the simple toggle preference,
// we'll need to disable all preferences to reject all click
// events until the sub-activity's UI comes up.
preferenceScreen.setEnabled(false);
// Let the intents be launched by the Preference manager
return false;
}
}
2018年更新的
今天,该findPreference
方法已被描述。因此,要实现此目的,只需覆盖onPreferenceTreeClick
您的首选项片段中的方法。例如:
public class MySettingsFragment extends PreferenceFragment {
@Override
public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen,
Preference preference)
{
String key = preference.getKey();
if(key.equals("someKey")){
// do your work
return true;
}
return false;
}
}
此外,如果您需要处理特定首选项元素(例如ListPreference
)内的click ,则应setOnPreferenceChangeListener
在的onCreate
方法内部注册MySettingsFragment
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// register listener
final Preference prefList = findPreference("key");
prefList.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object value) {
System.out.println("Selected: " + value);
return true;
}
});
}
onPreferenceClick
和之间有什么区别onPreferenceTreeClick
?
@jason gilbert的回答的后续行动
我在targetSdkVersion 25上,他的答案无效,我不得不将Intent标记包装为Preference标记。例:
<PreferenceScreen android:title="something">
<Preference title="Title">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.example.foo"
android:targetClass="com.example.foo.SomeActivity"
/>
</Preference>
</PreferenceScreen>
onPostCreate
无法正常工作,因为它为时过早(Nullpointer-Exception)。感谢您的帮助