using System; using Android.Content; using Android.Runtime; using Android.Views; using Android.Util; using Android.Graphics.Drawables; using Android.Views.Animations; using Android.Graphics; using Android.OS; using Android.Views.InputMethods; using Android.Text; namespace Shared { //å·²ç»å…¨é¢æ£€æŸ¥äº†ä»£ç /// <summary> /// Button 按键 /// </summary> public class Button : View { /// <summary> /// 当å‰è§†å›¾ /// </summary> /// <value>The android button.</value> AndroidButton currentButton { get { return AndroidView as AndroidButton; } set { AndroidView = value; } } /// <summary> /// æž„é€ å‡½æ•° /// </summary> public Button () { currentButton = new AndroidButton (Application.Activity, this); currentButton.SetTextSize (ComplexUnitType.Sp, Application.FontSize); } byte[] imageBytes; public byte[] ImageBytes { get { return imageBytes; } set { imageBytes = value; IsSelected = isSelected; } } /// <summary> /// æ–‡å—å¤§å° /// </summary> /// <value>The size of the text.</value> public float TextSize { get { return currentButton.TextSize; } set { currentButton.SetTextSize (ComplexUnitType.Sp, value); } } int textID; /// <summary> /// æ ¹æ®ID获å–对应的备注 /// </summary> /// <value>The text I.</value> public int TextID { get { return textID; } set { textID = value; Text = Language.StringByID (TextID); } } /// <summary> /// 文本 /// </summary> /// <value>The text.</value> public string Text { get { return currentButton.Text; } set { currentButton.Text = value; } } /// <summary> /// å—体åç§° /// </summary> /// <value>The name of the font.</value> public string FontName { get { return ""; } set { var typeFace = Typeface.CreateFromAsset (Shared.Application.Activity.Assets, "titilliumtext25l005.otf"); if (typeFace != null) { currentButton.SetTypeface (typeFace, TypefaceStyle.Normal); } } } bool isMoreLines; public bool IsMoreLines { get { return isMoreLines; } set { isMoreLines = value; currentButton.SetSingleLine (!value); } } /// <summary> /// 刷新 /// </summary> public override void Refresh () { base.Refresh (); IsSelected = isSelected; } uint textColor = 0xFFFFFFFF; /// <summary> /// æ–‡å—颜色 /// </summary> /// <value>The color of the text.</value> public uint TextColor { get { return textColor; } set { textColor = value; if (IsSelected) { return; } 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); currentButton.SetTextColor (Android.Graphics.Color.Argb (a, r, g, b)); } } uint selecteTextColor = 0xFFFFFFFF; /// <summary> /// æ–‡å—颜色 /// </summary> /// <value>The color of the text.</value> public uint SelectedTextColor { get { return selecteTextColor; } set { selecteTextColor = value; if (!IsSelected) { return; } byte r, g, b, a; r = (byte)(selecteTextColor / 256 / 256 % 256); g = (byte)(selecteTextColor / 256 % 256); b = (byte)(selecteTextColor % 256); a = (byte)(selecteTextColor / 256 / 256 / 256 % 256); currentButton.SetTextColor (Android.Graphics.Color.Argb (a, r, g, b)); } } bool isSelected; /// <summary> /// 选ä¸çŠ¶æ€ /// </summary> /// <value><c>true</c> if this instance is selected; otherwise, <c>false</c>.</value> public bool IsSelected { get { return isSelected; } set { isSelected = value; if (!IsCanRefresh) { return; } if (isSelected) { if (SelectedImagePath == null) { SelectedBackgroundColor = SelectedBackgroundColor; } else { Background (SelectedImagePath); } SelectedTextColor = selecteTextColor; } else { if (imageBytes != null) { AndroidView.Background = Bytes2Drawable(imageBytes); return; } if (UnSelectedImagePath == null) { BackgroundColor = BackgroundColor; } else { Background (UnSelectedImagePath); } TextColor = textColor; } } } TextAlignment textAlignment = TextAlignment.Center; /// <summary> /// æ–‡å—坹齿–¹å¼ /// </summary> /// <param name="horizontalAlignment">横å‘</param> /// <param name="verticalAlignment">纵å‘</param> public TextAlignment TextAlignment { get { return textAlignment; } set { textAlignment = value; switch (value) { case TextAlignment.TopLeft: currentButton.Gravity = GravityFlags.Top | GravityFlags.Left; break; case TextAlignment.TopCenter: currentButton.Gravity = GravityFlags.Top | GravityFlags.Center; break; case TextAlignment.TopRight: currentButton.Gravity = GravityFlags.Top | GravityFlags.Right; break; case TextAlignment.CenterLeft: currentButton.Gravity = GravityFlags.Center | GravityFlags.Left; break; case TextAlignment.Center: currentButton.Gravity = GravityFlags.Center | GravityFlags.Center; break; case TextAlignment.CenterRight: currentButton.Gravity = GravityFlags.Center | GravityFlags.Right; break; case TextAlignment.BottomLeft: currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Left; break; case TextAlignment.BottomCenter: currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Center; break; case TextAlignment.BottomRight: currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Right; break; } } } /// <summary> /// å†…è¾¹è· /// </summary> /// <value>The padding.</value> public override Padding Padding { get { return new Padding (currentButton.PaddingTop, currentButton.PaddingLeft, currentButton.PaddingBottom, currentButton.PaddingRight); } set { currentButton.SetPadding (value.Left, value.Top, value.Right, value.Bottom); } } string selectedImagePath; /// <summary> /// 选择时背景图路径 /// </summary> /// <value>The selected image path.</value> public string SelectedImagePath { get { return selectedImagePath; } set { selectedImagePath = value; IsSelected = isSelected; } } string unSelectedImagePath; /// <summary> /// éžé€‰ä¸çжæ€çš„背景图路径 /// </summary> /// <value>The un selected image path.</value> public string UnSelectedImagePath { get { return unSelectedImagePath; } set { unSelectedImagePath = value; IsSelected = isSelected; } } uint selectedBackgroundColor; /// <summary> /// 选择时背景颜色 /// </summary> /// <value>The color of the text.</value> public uint SelectedBackgroundColor { get { return selectedBackgroundColor; } set { selectedBackgroundColor = value; if (AndroidView.Background == null || AndroidView.Background.GetType () != typeof (GradientDrawable)) { AndroidView.Background = new Android.Graphics.Drawables.GradientDrawable (); } var gradientDrawable = (GradientDrawable)AndroidView.Background; gradientDrawable.SetCornerRadius (DensityUtil.Dip2Px (Radius)); byte r, g, b, a; r = (byte)(BorderColor / 256 / 256 % 256); g = (byte)(BorderColor / 256 % 256); b = (byte)(BorderColor % 256); a = (byte)(BorderColor / 256 / 256 / 256 % 256); gradientDrawable.SetStroke ((int)BorderWidth, Android.Graphics.Color.Argb (a, r, g, b)); r = (byte)(selectedBackgroundColor / 256 / 256 % 256); g = (byte)(selectedBackgroundColor / 256 % 256); b = (byte)(selectedBackgroundColor % 256); a = (byte)(selectedBackgroundColor / 256 / 256 / 256 % 256); gradientDrawable.SetColor (Android.Graphics.Color.Argb (a, r, g, b).ToArgb ()); } } internal class AndroidButton : Android.Widget.Button { View view; public AndroidButton (Context context, View view) : base (context) { this.view = view; SetBackgroundColor (Android.Graphics.Color.Transparent); SetPadding (0, 0, 0, 0); SetTextColor (Android.Graphics.Color.White); SetLines (1); SetEllipsize (true); } public override bool OnTouchEvent (MotionEvent e) { var v = base.OnTouchEvent (e); view?.TouchEvent (e); return true; } public void SetEllipsize (bool ellipsize) { if (ellipsize) { Ellipsize = TextUtils.TruncateAt.End; } else { Ellipsize = TextUtils.TruncateAt.Marquee; } } public override TextUtils.TruncateAt Ellipsize { get => base.Ellipsize; set => base.Ellipsize = value; } } } }