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; using Android.Graphics.Drawables; using Android.Graphics; namespace Shared { /// /// EditText /// public class EditText : View { public Action FoucsChanged; /// /// 当前视图 /// /// The android text. AndroidEditText currentAndroidEditText { get { return AndroidView as AndroidEditText; } set { AndroidView = value; } } public static EditText Instance; public Action KeyEventAction; public EditText() { Instance = this; currentAndroidEditText = new AndroidEditText(Application.Activity,this); TextSize = Application.FontSize; currentAndroidEditText.ViewAttachedToWindow += CurrentAndroidEditText_ViewAttachedToWindow; currentAndroidEditText.ViewDetachedFromWindow += CurrentAndroidEditText_ViewDetachedFromWindow; } private void CurrentAndroidEditText_TextChanged(object sender, TextChangedEventArgs e) { TextChangeEventHandler?.Invoke(this, e.Text.ToString()); } private void CurrentAndroidEditText_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e) { FoucsChanged?.Invoke(this, new FocusEventArgs { Focus = e.HasFocus }); } private void CurrentAndroidEditText_EditorAction(object sender, Android.Widget.TextView.EditorActionEventArgs e) { //if (e.Event == null) //{ // return; //} if (e.ActionId == ImeAction.Send || e.ActionId == ImeAction.Done || e.ActionId == ImeAction.Next || e.ActionId == ImeAction.Search || (e.Event != null && e.Event.Action == KeyEventActions.Down && e.Event.KeyCode == Keycode.Enter)) { Application.HideSoftInput(); EditorEnterAction?.Invoke(this); } } private void CurrentAndroidEditText_ViewDetachedFromWindow(object sender, Android.Views.View.ViewDetachedFromWindowEventArgs e) { ClearAllViewEvent(); } private void CurrentAndroidEditText_ViewAttachedToWindow(object sender, Android.Views.View.ViewAttachedToWindowEventArgs e) { InitAllViewEvent(); } internal override void InitAllViewEvent() { base.InitAllViewEvent(); currentAndroidEditText.TextChanged += CurrentAndroidEditText_TextChanged; currentAndroidEditText.FocusChange += CurrentAndroidEditText_FocusChange; currentAndroidEditText.EditorAction += CurrentAndroidEditText_EditorAction; } internal override void ClearAllViewEvent() { base.ClearAllViewEvent(); currentAndroidEditText.TextChanged -= CurrentAndroidEditText_TextChanged; currentAndroidEditText.FocusChange -= CurrentAndroidEditText_FocusChange; currentAndroidEditText.EditorAction -= CurrentAndroidEditText_EditorAction; } public bool Foucs { get { return currentAndroidEditText.IsFocused; } set { currentAndroidEditText.Focusable = value; currentAndroidEditText.RequestFocus(); currentAndroidEditText.FocusableInTouchMode = value; currentAndroidEditText.RequestFocusFromTouch(); if (value) { var imm = (InputMethodManager)Application.Activity.GetSystemService(Context.InputMethodService); imm.ShowSoftInput(AndroidView, ShowFlags.Forced); } } } public Action EditorEnterAction; /// /// 文字大小 /// /// The size of the text. public float TextSize { get { return currentAndroidEditText.TextSize; } set { currentAndroidEditText.SetTextSize(ComplexUnitType.Sp, value); } } /// /// 文本 /// /// The text. public string Text { get { return currentAndroidEditText.Text; } set { if (value == null) currentAndroidEditText.Text = ""; else currentAndroidEditText.Text = value.TrimEnd('\0'); } } int textID; /// /// 根据ID获取对应的备注 /// /// The text I. public int TextID { get { return textID; } set { textID = value; Text = Language.StringByID(TextID); } } /// /// 是否使能 /// /// true /// false public override bool Enable { get { return currentAndroidEditText.Enabled; } set { currentAndroidEditText.Enabled = value; } } /// /// 显示提示信息 /// /// The placeholder. public string PlaceholderText { get { return currentAndroidEditText.Hint; } set { currentAndroidEditText.Hint = value; } } uint placeholderTextColor; /// /// Gets or sets the color of the placeholder text. /// /// The color of the placeholder text. public uint PlaceholderTextColor { get { return placeholderTextColor; } set { placeholderTextColor = value; byte r, g, b, a; r = (byte)(placeholderTextColor / 256 / 256 % 256); g = (byte)(placeholderTextColor / 256 % 256); b = (byte)(placeholderTextColor % 256); a = (byte)(placeholderTextColor / 256 / 256 / 256 % 256); currentAndroidEditText.SetHintTextColor(Android.Graphics.Color.Argb(a, r, g, b)); } } bool isNumberKeyboardType = false; /// /// 设置键盘类型 是否为数字类型键盘,注意会与*号隐藏字符冲突 /// public bool IsNumberKeyboardType { get { return isNumberKeyboardType; } set { isNumberKeyboardType = value; if (value) { currentAndroidEditText.InputType = InputTypes.ClassNumber; } else { currentAndroidEditText.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword; } } } /// /// 是否用*号隐藏字符 /// /// The secure text entry. public bool SecureTextEntry { get { return currentAndroidEditText.InputType != InputTypes.TextVariationVisiblePassword; } set { if (value) { currentAndroidEditText.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword; } else { currentAndroidEditText.InputType = InputTypes.TextVariationVisiblePassword; } } } 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 (SelectedImagePath == null) { SelectedBackgroundColor = SelectedBackgroundColor; } else { if (!Background(SelectedImagePath)) { BackgroundColor = BackgroundColor; } } } else { if (UnSelectedImagePath == null) { BackgroundColor = BackgroundColor; } else { if (!Background(UnSelectedImagePath)) { BackgroundColor = BackgroundColor; } } } } } uint selectedBackgroundColor; /// /// 选择时背景颜色 /// /// The color of the text. 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()); } } /// /// 刷新大小 /// public override void Refresh() { base.Refresh(); IsSelected = isSelected; } 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); currentAndroidEditText.SetTextColor(Android.Graphics.Color.Argb(a, r, g, b)); } } public static int FocusBottom{ get{ return AndroidEditText.Bottom; } } //大家新年好!编译人生代码,运行幸福程序;升级应用功能,超出项目需求。2019年,你的幸福数据在加载中。。。。。 TextAlignment textAlignment = TextAlignment.Center; /// /// Texts the alignment. /// /// Horizontal alignment. /// Vertical alignment. public TextAlignment TextAlignment { get { return textAlignment; } set { textAlignment = value; switch (value) { case TextAlignment.TopLeft: currentAndroidEditText.Gravity = GravityFlags.Top | GravityFlags.Left; break; case TextAlignment.TopCenter: currentAndroidEditText.Gravity = GravityFlags.Top | GravityFlags.Center; break; case TextAlignment.TopRight: currentAndroidEditText.Gravity = GravityFlags.Top | GravityFlags.Right; break; case TextAlignment.CenterLeft: currentAndroidEditText.Gravity = GravityFlags.Center | GravityFlags.Left; break; case TextAlignment.Center: currentAndroidEditText.Gravity = GravityFlags.Center | GravityFlags.Center; break; case TextAlignment.CenterRight: currentAndroidEditText.Gravity = GravityFlags.Center | GravityFlags.Right; break; case TextAlignment.BottomLeft: currentAndroidEditText.Gravity = GravityFlags.Bottom | GravityFlags.Left; break; case TextAlignment.BottomCenter: currentAndroidEditText.Gravity = GravityFlags.Bottom | GravityFlags.Center; break; case TextAlignment.BottomRight: currentAndroidEditText.Gravity = GravityFlags.Bottom | GravityFlags.Right; break; } } } /// /// 选择时背景图路径 /// /// The selected image path. public string SelectedImagePath { get; set; } /// /// 非选中状态的背景图路径 /// /// The un selected image path. public string UnSelectedImagePath { get; set; } /// /// 输入文字变化事件 /// public Action TextChangeEventHandler; public void SetSingleLine(bool bSingle, int maxLines = 3) { currentAndroidEditText.SetSingleLine(bSingle); if (!bSingle) { currentAndroidEditText.SetMaxLines(maxLines); } } class AndroidEditText : Android.Widget.EditText { EditText editText; public AndroidEditText(Context context,EditText editText) : base(context) { SetTextColor(Android.Graphics.Color.White); SetPadding(0, 0, 0, 0); TextAlignment = Android.Views.TextAlignment.Center; SetSingleLine(true); this.editText = editText; } protected override void OnFocusChanged (bool gainFocus, [GeneratedEnum] FocusSearchDirection direction, Rect previouslyFocusedRect) { base.OnFocusChanged (gainFocus, direction, previouslyFocusedRect); var location = new int[2]; this.GetLocationOnScreen(location); if (gainFocus) { Bottom = location [1] + this.Height; } else{ Bottom = 0; } } public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e) { if (e != null && e.KeyCode == Keycode.Del && e.Action == KeyEventActions.Down) { editText?.KeyEventAction?.Invoke(editText,"Del"); } return base.OnKeyDown(keyCode, e); } public static int Bottom; //public override Android.Views.InputMethods.IInputConnection OnCreateInputConnection(Android.Views.InputMethods.EditorInfo outAttrs) //{ // return new ZanyInputConnection(base.OnCreateInputConnection(outAttrs), true,this); //} //class ZanyInputConnection : InputConnectionWrapper,ISpannable //{ // AndroidEditText androidEditText; // public ZanyInputConnection(IInputConnection target, bool mutable,AndroidEditText androidText):base(target,mutable) // { // this.androidEditText = androidText; // } // public override bool SendKeyEvent(KeyEvent e) // { // if (e.Action == KeyEventActions.Down && e.KeyCode == Keycode.Del) // { // string text = androidEditText.Text; // if (text.Length > 0) // { // string newText = text.Substring(0, text.Length - 1); // androidEditText.Text=newText; // Selection.SetSelection(this, newText.Length); // } // return false; // } // return base.SendKeyEvent(e); // } //} } } public class FocusEventArgs : EventArgs { /// /// true 为获取到焦点 /// false 焦点消失 /// public bool Focus; } }