using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Util;
using Android.Views.InputMethods;
using Android.Text;
using Android.Graphics.Drawables;
using Android.Graphics;
namespace Shared
{
///
/// EditTextView 文本输入框
/// 支持换行
///
public class EditTextView : View
{
public Action FoucsChanged;
///
/// 当前视图
///
/// The android text.
AndroidEditText currentAndroidEditText
{
get
{
return AndroidView as AndroidEditText;
}
set
{
AndroidView = value;
}
}
public static EditTextView Instance;
public Action KeyEventAction;
public EditTextView()
{
Instance = this;
currentAndroidEditText = new AndroidEditText(Application.Activity,this);
//currentAndroidEditText.SetSingleLine(false);
//currentAndroidEditText.SetMaxLines(10);
//TextSize = Application.FontSize;
TextSize = 14;
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);
}
}
int _MaxLength = 1000;
public int MaxLength
{
get
{
return _MaxLength;
}
set
{
_MaxLength = value;
}
}
///
/// 是否使能
///
/// 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));
}
}
///
/// 是否用*号隐藏字符
///
/// 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 = 0)
{
currentAndroidEditText.SetSingleLine(bSingle);
if (!bSingle) {
currentAndroidEditText.SetMaxLines(maxLines);
}
}
///
/// 刷新placeholderUILabel布局
///
public void InitIosPlaceholderUILabelWithHeight(int mHeight)
{
}
public void HideSoftInput()
{
var imm = (Android.Views.InputMethods.InputMethodManager)Application.Activity.GetSystemService(Context.InputMethodService);
if (Shared.EditTextView.Instance != null)
{
imm.HideSoftInputFromWindow(Shared.EditTextView.Instance.AndroidView.WindowToken, 0); //强制隐藏键盘
}
else
{
imm.ToggleSoftInput(0, Android.Views.InputMethods.HideSoftInputFlags.NotAlways);
}
}
class AndroidEditText : Android.Widget.EditText
{
EditTextView editText;
public AndroidEditText(Context context, EditTextView editText)
: base(context)
{
SetTextColor(Android.Graphics.Color.White);
SetPadding(10, 10, 10, 10);
TextAlignment = Android.Views.TextAlignment.Center;
Gravity = GravityFlags.Top | GravityFlags.Left;
SetSingleLine(false);
//SetMaxLines(20);
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;
}
}
}