using Android.Graphics; using Android.Text; using Android.Text.Method; using Android.Views; using Android.Widget; using AndroidX.Annotations; using AndroidX.Emoji.Text; using System; using System.Common; using System.Linq; using Color = Android.Graphics.Color; // ReSharper disable once CheckNamespace namespace System { /// /// 对 类的扩展函数集 /// public static class TextViewExtensions { /// /// 设置 ,字符串中可能有 emoji 字符 /// https://developer.android.google.cn/reference/android/widget/TextView#setHint(java.lang.CharSequence) /// /// 可能有 emoji 字符的字符串 public static void SetHintByMaybeEmoji(this TextView textView, string maybeEmojiStr) { // https://developer.android.google.cn/guide/topics/ui/look-and-feel/emoji-compat#using-emojicompat-without-widgets var processed = EmojiCompat.Get().ProcessFormatted(maybeEmojiStr.ToJavaString()); textView.HintFormatted = processed; } /// /// Sets the text color for all the states (normal, selected, focused) to be this color. /// https://developer.android.google.cn/reference/android/widget/TextView#setTextColor(int) /// /// /// A color value in the form 0xAARRGGBB. Do not pass a resource ID. To get a color value from a resource ID, call . public static void SetTextColor(this TextView textView, [ColorInt] int color) { var color_ = new Color(color); textView.SetTextColor(color_); } /// /// 添加或替换一组 /// /// /// public static void AddFilters(this TextView textView, params IInputFilter[] filters) { var items = textView.GetFilters(); if (items != null && items.Any()) { // 根据Type去重添加 var types = filters.Where(x => x != null).Select(x => x.GetType()).Distinct(); filters = items.Where(x => x != null && !types.Contains(x.GetType())).Concat(filters).ToArray(); } textView.SetFilters(filters); } /// /// 给 设置 /// 功能用途:文字的超链接 /// /// public static void SetLinkMovementMethod(this TextView textView) { textView.MovementMethod = LinkMovementMethod.Instance; } /// /// 设置粗体 /// /// /// 是否粗体 /// 是否全部文字使用粗体 /// public static void SetBold(this TextView textView, bool isBold = true, bool isBoldOnly = true) { var paint = textView.Paint; if (paint != null) { paint.FakeBoldText = isBold; if (isBoldOnly) { if (isBold) { textView.SetTypeface(textView.Typeface, TypefaceStyle.Bold); } else { textView.SetTypeface(textView.Typeface, TypefaceStyle.Normal); } } } } /// public static void SetText(this TextView textView, string? value, bool showVisibility = true) { var isNullOrEmpty = string.IsNullOrEmpty(value); textView.Text = value ?? string.Empty; if (!isNullOrEmpty) { if (showVisibility) { if (textView.Visibility != ViewStates.Visible) { textView.Visibility = ViewStates.Visible; } } } } /// /// 设置文本 /// /// /// 要设置的字符串 /// 如果要设置的字符串则使用此值 /// 如果为,则当设置的字符串不为或时,使= public static void SetText(this TextView textView, string? value, string value2, bool showVisibility = true) { value = string.IsNullOrEmpty(value) ? value2 : value; SetText(textView, value, showVisibility); } /// /// 设置输入框的类型(手机号码) /// /// public static void SetPhoneNumberType(this TextView textView) { textView.KeyListener = DigitsKeyListener.GetInstance(accepted: Constants.Digits); } } }