下面列出了android.preference.Preference#getTitle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
private CharSequence getTitleOfOtherActionAssociatedWith(Long extendedKeyCode) {
/*
* Collect all KeyComboPreferences. It's somewhat inefficient to iterate through all
* preferences every time, but it's only done during configuration when the user presses a
* key. Lazily-initializing a static list would assume that there's no way a preference
* will be added after the initialization. That assumption was not true during testing,
* which may have been specific to the testing environment but may also indicate that
* problematic situations can arise.
*/
PreferenceManager preferenceManager = getPreferenceManager();
SharedPreferences prefs = SharedPreferencesUtils.getSharedPreferences(getContext());
Map<String, ?> prefMap = prefs.getAll();
String myKey = getKey();
for (String key : prefMap.keySet()) {
if (!myKey.equals(key)) {
Preference preference = preferenceManager.findPreference(key);
if (preference instanceof KeyComboPreference
&& KeyAssignmentUtils.getKeyCodesForPreference(getContext(), key)
.contains(extendedKeyCode)) {
return preference.getTitle();
}
}
}
return null;
}
public static Matcher<Preference> withTitleText(final Matcher<String> titleMatcher) {
return new TypeSafeMatcher<Preference>() {
@Override
public void describeTo(Description description) {
description.appendText(" a preference with title matching: ");
titleMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(Preference pref) {
if (pref.getTitle() == null) {
return false;
}
String title = pref.getTitle().toString();
return titleMatcher.matches(title);
}
};
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
String title = (String) preference.getTitle();
if (title.equals("颜色风格")) {
showThemeDialog();
return true;
} else if (title.equals("捐赠")) {
donate();
return true;
}
return false;
}
private void showOverrideKeyComboDialog(final String key) {
final Preference currentActionPreference = getPreferenceManager().findPreference(key);
if (currentActionPreference == null) {
return;
}
final Preference newActionPreference = getPreferenceManager().findPreference(getKey());
if (newActionPreference == null) {
return;
}
CharSequence currentAction = currentActionPreference.getTitle();
CharSequence newAction = newActionPreference.getTitle();
setKeyEventSource(KEY_EVENT_SOURCE_ACTIVITY);
showOverrideKeyComboDialog(
currentAction,
newAction,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which != DialogInterface.BUTTON_POSITIVE) {
setKeyEventSource(getKeyEventSourceForCurrentKeyComboModel());
return;
}
saveKeyCode();
keyComboManager.getKeyComboModel().clearKeyComboCode(key);
notifyListener(key, keyComboManager.getKeyComboModel().getKeyComboCodeForKey(key));
Dialog mainDialog = getDialog();
if (mainDialog != null) {
mainDialog.dismiss();
}
}
});
}
private void markPro(Preference pref, String sku) {
if (sku == null || !IAB.isPurchased(sku, this)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean dark = prefs.getBoolean("dark_theme", false);
SpannableStringBuilder ssb = new SpannableStringBuilder(" " + pref.getTitle());
ssb.setSpan(new ImageSpan(this, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
pref.setTitle(ssb);
}
}
public static Matcher<Preference> withTitle(final int resourceId) {
return new TypeSafeMatcher<Preference>() {
private String resourceName = null;
private String expectedText = null;
@Override
public void describeTo(Description description) {
description.appendText(" with title string from resource id: ");
description.appendValue(resourceId);
if (null != resourceName) {
description.appendText("[");
description.appendText(resourceName);
description.appendText("]");
}
if (null != expectedText) {
description.appendText(" value: ");
description.appendText(expectedText);
}
}
@Override
public boolean matchesSafely(Preference preference) {
if (null == expectedText) {
try {
expectedText = preference.getContext().getResources().getString(resourceId);
resourceName = preference.getContext().getResources().getResourceEntryName(resourceId);
} catch (Resources.NotFoundException ignored) {
/* view could be from a context unaware of the resource id. */
}
}
if (null != expectedText && preference.getTitle() != null) {
return expectedText.equals(preference.getTitle().toString());
} else {
return false;
}
}
};
}