using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Util; using Android.Views.InputMethods; using Android.Text; namespace Shared { /// /// TextView /// public class TextView : View { /// /// 当前视图 /// /// The android text. AndroidTextView currentAndroidTextView { get { return AndroidView as AndroidTextView; } set { AndroidView = value; } } public TextView() { currentAndroidTextView = new AndroidTextView(Application.Activity,this); } /// /// 文字大小 /// /// The size of the text. public float TextSize { get { return currentAndroidTextView.TextSize; } set { currentAndroidTextView.SetTextSize(ComplexUnitType.Sp, value); } } /// /// 文本 /// /// The text. public string Text { get { return currentAndroidTextView.Text; } set { if (currentAndroidTextView.Text == value) { return; } currentAndroidTextView.Text = value; } } int textID; /// /// 根据ID获取对应的备注 /// /// The text I. public int TextID { get { return textID; } set { textID = value; Text = Language.StringByID(TextID); } } bool isSelected; /// /// Gets or sets a value indicating whether this instance is selected. /// /// true if this instance is selected; otherwise, false. public bool IsSelected { get { return isSelected; } set { isSelected = value; if (!IsCanRefresh) { return; } if (isSelected) { if (!Background(SelectedImagePath)) { BackgroundColor = BackgroundColor; } } else { if (!Background(UnSelectedImagePath)) { BackgroundColor = BackgroundColor; } } } } /// /// 刷新大小 /// public override void Refresh() { base.Refresh(); IsSelected = isSelected; currentAndroidTextView.Gravity = GravityFlags.Center; } uint textColor; /// /// 文字颜色 /// /// The color of the text. public uint TextColor { get { return textColor; } set { textColor = value; byte r, g, b, a; r = (byte)(textColor / 256 / 256 % 256); g = (byte)(textColor / 256 % 256); b = (byte)(textColor % 256); a = (byte)(textColor / 256 / 256 / 256 % 256); currentAndroidTextView.SetTextColor(Android.Graphics.Color.Argb(a, r, g, b)); } } bool isMoreLines; public bool IsMoreLines { get { return isMoreLines; } set { isMoreLines = value; currentAndroidTextView.SetSingleLine(!value); } } /// /// 选择时背景图路径 /// /// The selected image path. public string SelectedImagePath { get; set; } /// /// 非选中状态的背景图路径 /// /// The un selected image path. public string UnSelectedImagePath { get; set; } class AndroidTextView : Android.Widget.TextView { View _view; public AndroidTextView(Context context, View view) : base(context) { _view = view; SetTextColor(Android.Graphics.Color.White); SetPadding(0, 0, 0, 0); TextAlignment = Android.Views.TextAlignment.Center; SetSingleLine(true); SetTextSize(ComplexUnitType.Sp, 10); Ellipsize = TextUtils.TruncateAt.Marquee; SetMarqueeRepeatLimit(int.MaxValue); System.Threading.Tasks.Task.Run(() => { System.Threading.Thread.Sleep(1000); Application.RunOnMainThread(() => { Selected = true; }); }); } public override bool IsFocused { get { return true; } } /// /// 重写点击事件 /// /// true, if touch event was oned, false otherwise. /// E. public override bool OnTouchEvent(MotionEvent e) { if (_view != null) { _view.TouchEvent(e); } return base.OnTouchEvent(e); } } } }