下面列出了android.widget.TextView#getContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@BindingAdapter("date")
public static void setInteger(TextView textView, Date receivedAt) {
if (receivedAt == null || receivedAt.getTime() <= 0) {
textView.setVisibility(View.GONE);
} else {
Context context = textView.getContext();
Calendar specifiedDate = Calendar.getInstance();
specifiedDate.setTime(receivedAt);
Calendar today = Calendar.getInstance();
textView.setVisibility(View.VISIBLE);
long diff = today.getTimeInMillis() - specifiedDate.getTimeInMillis();
if (sameDay(today, specifiedDate) || diff < SIX_HOURS) {
textView.setText(DateUtils.formatDateTime(context, receivedAt.getTime(), DateUtils.FORMAT_SHOW_TIME));
} else if (sameYear(today, specifiedDate) || diff < THREE_MONTH) {
textView.setText(DateUtils.formatDateTime(context, receivedAt.getTime(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL));
} else {
textView.setText(DateUtils.formatDateTime(context, receivedAt.getTime(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_ABBREV_ALL));
}
}
}
@BindingAdapter("android:text")
public static void setText(final TextView textView, final FullEmail.From from) {
if (from instanceof FullEmail.NamedFrom) {
final FullEmail.NamedFrom named = (FullEmail.NamedFrom) from;
textView.setText(named.getName());
} else if (from instanceof FullEmail.DraftFrom) {
final Context context = textView.getContext();
final SpannableString spannable = new SpannableString(context.getString(R.string.draft));
spannable.setSpan(
new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)),
0,
spannable.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
textView.setText(spannable);
}
}
public void style(TextView badgeTextView, ColorStateList colorStateList) {
Context ctx = badgeTextView.getContext();
//set background for badge
if (mBadgeBackground == null) {
UIUtils.setBackground(badgeTextView, new BadgeDrawableBuilder(this).build(ctx));
} else {
UIUtils.setBackground(badgeTextView, mBadgeBackground);
}
//set the badge text color
if (mTextColor != null) {
ColorHolder.applyToOr(mTextColor, badgeTextView, null);
} else if (colorStateList != null) {
badgeTextView.setTextColor(colorStateList);
}
//set the padding
int paddingLeftRight = mPaddingLeftRight.asPixel(ctx);
int paddingTopBottom = mPaddingTopBottom.asPixel(ctx);
badgeTextView.setPadding(paddingLeftRight, paddingTopBottom, paddingLeftRight, paddingTopBottom);
//set the min width
badgeTextView.setMinWidth(mMinWidth.asPixel(ctx));
}
@Override
public void updateLeftSpannable(@NonNull final SpannableStringBuilder spannableStringBuilder,
@NonNull final TextView textView) {
final Context context = textView.getContext();
if (showAmount) {
updateInstallment(spannableStringBuilder, context, textView);
spannableStringBuilder.append(TextUtil.SPACE);
}
if (accountMoneyMetadata.displayInfo != null) {
sliderTitle = accountMoneyMetadata.displayInfo.sliderTitle;
if (TextUtil.isEmpty(sliderTitle)) {
spannableStringBuilder.append(TextUtil.SPACE);
} else {
final AmountLabeledFormatter amountLabeledFormatter =
new AmountLabeledFormatter(spannableStringBuilder, context)
.withTextColor(ContextCompat.getColor(context, R.color.ui_meli_grey));
amountLabeledFormatter.apply(sliderTitle);
}
}
}
public SlideToCancel(TextView slideToCancelView) {
this.slideToCancelView = slideToCancelView;
Context context = slideToCancelView.getContext();
int clr = context.getResources().getColor(R.color.common_foreground_color);
for (Drawable drawable : slideToCancelView.getCompoundDrawables()) {
if (drawable != null) {
drawable.setColorFilter(new PorterDuffColorFilter(clr, PorterDuff.Mode.SRC_IN));
}
}
}
/**
* Scans the text of the provided TextView and turns all occurrences of
* the link types indicated in the mask into clickable links. If matches
* are found the movement method for the TextView is set to
* LinkMovementMethod.
*
* @param text TextView whose text is to be marked-up with links
* @param mask Mask to define which kinds of links will be searched.
*
* @return True if at least one link is found and applied.
*/
public static final boolean addLinks(@NonNull TextView text, @LinkifyMask int mask) {
if (mask == 0) {
return false;
}
final Context context = text.getContext();
final CharSequence t = text.getText();
if (t instanceof Spannable) {
if (addLinks((Spannable) t, mask, context)) {
addLinkMovementMethod(text);
return true;
}
return false;
} else {
SpannableString s = SpannableString.valueOf(t);
if (addLinks(s, mask, context)) {
addLinkMovementMethod(text);
text.setText(s);
return true;
}
return false;
}
}
public StyleBuilderImpl(TextView textView, Html.ImageGetter imageGetter) {
this.textViewWeakReference = new WeakReference<>(textView);
this.imageGetter = imageGetter;
Context context = textView.getContext();
TypedArray a = context.obtainStyledAttributes(null, R.styleable.MarkdownTheme, R.attr.markdownStyle, 0);
final boolean failed = !a.hasValue(0);
if (failed) {
Log.w("Markdown", "Missing markdownStyle in your theme, using hardcoded color.");
h1_text_color = 0xdf000000;
h6_text_color = 0x8a000000;
quota_color = 0x4037474f;
quota_text_color = 0x61000000;
code_text_color = 0xd8000000;
code_background_color = 0x0c37474f;
link_color = 0xdc3e7bc9;
h_under_line_color = 0x1837474f;
} else {
h1_text_color = a.getColor(R.styleable.MarkdownTheme_h1TextColor, 0);
h6_text_color = a.getColor(R.styleable.MarkdownTheme_h6TextColor, 0);
quota_color = a.getColor(R.styleable.MarkdownTheme_quotaColor, 0);
quota_text_color = a.getColor(R.styleable.MarkdownTheme_quotaTextColor, 0);
code_text_color = a.getColor(R.styleable.MarkdownTheme_codeTextColor, 0);
code_background_color = a.getColor(R.styleable.MarkdownTheme_codeBackgroundColor, 0);
link_color = a.getColor(R.styleable.MarkdownTheme_linkColor, 0);
h_under_line_color = a.getColor(R.styleable.MarkdownTheme_underlineColor, 0);
}
a.recycle();
}
public static ValueAnimator voteUpReverseTextColorAnimation(TextView view) {
Context context = view.getContext();
return colorChangeAnimation(view,
context.getResources().getColor(R.color.vote_state_up),
context.getResources().getColor(R.color.vote_state_empty));
}
private static void addDetectedValueHeader(@NonNull ListView listView, @StringRes int labelRes, @NonNull String labelKey, @NonNull String labelValue, @NonNull String value, @NonNull Icon icon) {
final TextView textView = (TextView) LayoutInflater.from(listView.getContext()).inflate(R.layout.edx_selectable_list_item, listView, false);
{
final SpannableString labelValueSpan = new SpannableString(labelValue);
labelValueSpan.setSpan(new ForegroundColorSpan(listView.getResources().getColor(R.color.edx_brand_gray_base)), 0, labelValueSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ResourceUtil.getFormattedString(listView.getContext().getResources(), labelRes, labelKey, labelValueSpan));
}
Context context = textView.getContext();
TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
new IconDrawable(context, icon)
.sizeRes(context, R.dimen.edx_base)
.colorRes(context, R.color.edx_brand_gray_back)
, null, null, null);
listView.addHeaderView(textView, new FormOption(labelValue, value), true);
}
/**
* Sets the state, text and icon of the new item creation button on discussion screens
*
* @param isTopicClosed Boolean if the topic is closed or not
* @param textView The TextView whose text has to be updated
* @param positiveTextResId The text resource to be applied when topic IS NOT closed
* @param negativeTextResId The text resource to be applied when topic IS closed
* @param creationLayout The layout which should be enabled/disabled and applied listener to
* @param listener The listener to apply to creationLayout
*/
public static void setStateOnTopicClosed(boolean isTopicClosed, TextView textView,
@StringRes int positiveTextResId,
@StringRes int negativeTextResId,
ViewGroup creationLayout,
View.OnClickListener listener) {
Context context = textView.getContext();
if (isTopicClosed) {
textView.setText(negativeTextResId);
TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
new IconDrawable(context, FontAwesomeIcons.fa_lock)
.sizeRes(context, R.dimen.small_icon_size)
.colorRes(context, R.color.white),
null, null, null
);
creationLayout.setOnClickListener(null);
} else {
textView.setText(positiveTextResId);
TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(textView,
new IconDrawable(context, FontAwesomeIcons.fa_plus_circle)
.sizeRes(context, R.dimen.small_icon_size)
.colorRes(context, R.color.white),
null, null, null
);
creationLayout.setOnClickListener(listener);
}
creationLayout.setEnabled(!isTopicClosed);
}
private void highlightText(TextView textView) {
Context context = textView.getContext();
CharSequence text = textView.getText();
TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true);
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(
new BackgroundColorSpan(value.data), 0, text.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
}
@Override
public void updateLeftSpannable(@NonNull final SpannableStringBuilder spannableStringBuilder,
@NonNull final TextView textView) {
final Context context = textView.getContext();
final SpannableFormatter amountLabeledFormatter = new SpannableFormatter(spannableStringBuilder, context);
amountLabeledFormatter.withTextColor(ContextCompat.getColor(context, R.color.ui_meli_blue))
.withStyle(PxFont.SEMI_BOLD);
if (message != null && TextUtil.isNotEmpty(message.getMessage())) {
description = message.getMessage();
} else {
description = context.getString(R.string.px_payment_method_disable_title);
}
amountLabeledFormatter.apply(description);
}
/**
* Creates a new instance of {@code AutofitHelper} that wraps a {@link TextView} and enables
* automatically sizing the text to fit.
*/
public static AutofitHelper create(TextView view, AttributeSet attrs, int defStyle) {
AutofitHelper helper = new AutofitHelper(view);
boolean sizeToFit = true;
if (attrs != null) {
Context context = view.getContext();
int minTextSize = (int) helper.getMinTextSize();
float precision = helper.getPrecision();
TypedArray ta = context.obtainStyledAttributes(
attrs,
R.styleable.AutofitTextView,
defStyle,
0);
sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
minTextSize);
precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
ta.recycle();
helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize)
.setPrecision(precision);
}
helper.setEnabled(sizeToFit);
return helper;
}
static void setupView(TextView view, AttributeSet attrs) {
Context context = view.getContext();
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PandroidCompat);
try {
int drawableLeftRes = typedArray.getResourceId(R.styleable
.PandroidCompat_drawableLeftPCompat, 0);
int drawableTopRes = typedArray.getResourceId(R.styleable
.PandroidCompat_drawableTopPCompat, 0);
int drawableRightRes = typedArray.getResourceId(R.styleable
.PandroidCompat_drawableRightPCompat, 0);
int drawableBottomRes = typedArray.getResourceId(R.styleable
.PandroidCompat_drawableBottomPCompat, 0);
Integer tintColor = null;
if (typedArray.hasValue(R.styleable.PandroidCompat_drawableTintPCompat)) {
tintColor = typedArray.getColor(R.styleable.PandroidCompat_drawableTintPCompat, 0);
}
Drawable drawableStart = getDrawableInternal(context, drawableLeftRes, tintColor);
Drawable drawableTop = getDrawableInternal(context, drawableTopRes, tintColor);
Drawable drawableEnd = getDrawableInternal(context, drawableRightRes, tintColor);
Drawable drawableBottom = getDrawableInternal(context, drawableBottomRes, tintColor);
view.setCompoundDrawablesWithIntrinsicBounds(drawableStart, drawableTop, drawableEnd, drawableBottom);
} finally {
typedArray.recycle();
}
}
private AutofitHelper(TextView view) {
final Context context = view.getContext();
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
mTextView = view;
mPaint = new TextPaint();
setRawTextSize(view.getTextSize());
mMaxLines = getMaxLines(view);
mMinTextSize = scaledDensity * DEFAULT_MIN_TEXT_SIZE;
mMaxTextSize = mTextSize;
mPrecision = DEFAULT_PRECISION;
}
@Override
public void onInitialized() {
final TextView regularTextView = mRegularTextViewRef.get();
if (regularTextView != null) {
final EmojiCompat compat = EmojiCompat.get();
final Context context = regularTextView.getContext();
regularTextView.setText(
compat.process(context.getString(R.string.regular_text_view, EMOJI)));
}
}
/**
* Returns a matcher that matches {@link android.widget.TextView} based on it's color.
*
* <p><b>This API is currently in beta.</b>
*/
@Beta
public static Matcher<View> hasTextColor(final int colorResId) {
return new BoundedMatcher<View, TextView>(TextView.class) {
private Context context;
@Override
protected boolean matchesSafely(TextView textView) {
context = textView.getContext();
int textViewColor = textView.getCurrentTextColor();
int expectedColor;
if (Build.VERSION.SDK_INT <= 22) {
expectedColor = context.getResources().getColor(colorResId);
} else {
expectedColor = context.getColor(colorResId);
}
return textViewColor == expectedColor;
}
@Override
public void describeTo(Description description) {
String colorId = String.valueOf(colorResId);
if (context != null) {
colorId = context.getResources().getResourceName(colorResId);
}
description.appendText("has color with ID " + colorId);
}
};
}
public HtmlAssetsImageGetter(TextView textView) {
this.context = textView.getContext();
}
@BindingAdapter("from")
public static void setFrom(final TextView textView, final ThreadOverviewItem.From[] from) {
final SpannableStringBuilder builder = new SpannableStringBuilder();
if (from != null) {
final boolean shorten = from.length > 1;
for (int i = 0; i < from.length; ++i) {
final ThreadOverviewItem.From individual = from[i];
if (builder.length() != 0) {
builder.append(", ");
}
final int start = builder.length();
if (individual instanceof ThreadOverviewItem.DraftFrom) {
final Context context = textView.getContext();
builder.append(context.getString(R.string.draft));
builder.setSpan(
new ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary)),
start,
builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
} else if (individual instanceof ThreadOverviewItem.NamedFrom) {
final ThreadOverviewItem.NamedFrom named = (ThreadOverviewItem.NamedFrom) individual;
builder.append(shorten ? EmailAddressUtil.shorten(named.name) : named.name);
if (!named.seen) {
builder.setSpan(
new StyleSpan(Typeface.BOLD),
start,
builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
if (from.length > 3) {
if (i < from.length - 3) {
builder.append(" … "); //TODO small?
i = from.length - 3;
}
}
} else {
throw new IllegalStateException(
String.format("Unable to render from type %s", individual.getClass().getName())
);
}
}
}
textView.setText(builder);
}
private static BetterLinkMovementExtended linkify(int linkifyMask, TextView textView) {
BetterLinkMovementExtended movementMethod = new BetterLinkMovementExtended(textView.getContext());
addLinks(linkifyMask, movementMethod, textView);
return movementMethod;
}