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
|
{
|
/// <summary>
|
/// EditTextView 文本输入框
|
/// 支持换行
|
/// </summary>
|
public class EditTextView : View
|
{
|
public Action<View, FocusEventArgs> FoucsChanged;
|
|
/// <summary>
|
/// 当前视图
|
/// </summary>
|
/// <value>The android text.</value>
|
AndroidEditText currentAndroidEditText
|
{
|
get
|
{
|
return AndroidView as AndroidEditText;
|
}
|
set
|
{
|
AndroidView = value;
|
}
|
}
|
|
public static EditTextView Instance;
|
|
public Action<View, string> 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<View> EditorEnterAction;
|
/// <summary>
|
/// 文字大小
|
/// </summary>
|
/// <value>The size of the text.</value>
|
public float TextSize
|
{
|
get
|
{
|
return currentAndroidEditText.TextSize;
|
}
|
set
|
{
|
currentAndroidEditText.SetTextSize(ComplexUnitType.Sp, value);
|
}
|
}
|
|
/// <summary>
|
/// 文本
|
/// </summary>
|
/// <value>The text.</value>
|
public string Text
|
{
|
get
|
{
|
return currentAndroidEditText.Text;
|
}
|
set
|
{
|
if (value == null)
|
currentAndroidEditText.Text = "";
|
else
|
currentAndroidEditText.Text = value.TrimEnd('\0');
|
}
|
}
|
int textID;
|
/// <summary>
|
/// 根据ID获取对应的备注
|
/// </summary>
|
/// <value>The text I.</value>
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
/// 是否使能
|
/// </summary>
|
/// <value>true</value>
|
/// <c>false</c>
|
public override bool Enable
|
{
|
get
|
{
|
return currentAndroidEditText.Enabled;
|
}
|
set
|
{
|
currentAndroidEditText.Enabled = value;
|
}
|
}
|
|
/// <summary>
|
/// 显示提示信息
|
/// </summary>
|
/// <value>The placeholder.</value>
|
public string PlaceholderText
|
{
|
get
|
{
|
return currentAndroidEditText.Hint;
|
}
|
set
|
{
|
currentAndroidEditText.Hint = value;
|
}
|
}
|
|
uint placeholderTextColor;
|
/// <summary>
|
/// Gets or sets the color of the placeholder text.
|
/// </summary>
|
/// <value>The color of the placeholder text.</value>
|
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));
|
}
|
}
|
|
|
/// <summary>
|
/// 是否用*号隐藏字符
|
/// </summary>
|
/// <value>The secure text entry.</value>
|
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;
|
/// <summary>
|
/// Gets or sets a value indicating whether this instance is selected.
|
/// </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
|
{
|
if (!Background(SelectedImagePath))
|
{
|
BackgroundColor = BackgroundColor;
|
}
|
}
|
}
|
else
|
{
|
if (UnSelectedImagePath == null)
|
{
|
BackgroundColor = BackgroundColor;
|
}
|
else
|
{
|
if (!Background(UnSelectedImagePath))
|
{
|
BackgroundColor = BackgroundColor;
|
}
|
}
|
}
|
}
|
}
|
|
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());
|
}
|
|
}
|
|
/// <summary>
|
/// 刷新大小
|
/// </summary>
|
public override void Refresh()
|
{
|
base.Refresh();
|
IsSelected = isSelected;
|
}
|
|
uint textColor;
|
/// <summary>
|
/// 文字颜色
|
/// </summary>
|
/// <value>The color of the text.</value>
|
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;
|
/// <summary>
|
/// Texts the alignment.
|
/// </summary>
|
/// <param name="horizontalAlignment">Horizontal alignment.</param>
|
/// <param name="verticalAlignment">Vertical alignment.</param>
|
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;
|
}
|
}
|
|
}
|
|
/// <summary>
|
/// 选择时背景图路径
|
/// </summary>
|
/// <value>The selected image path.</value>
|
public string SelectedImagePath { get; set; }
|
|
/// <summary>
|
/// 非选中状态的背景图路径
|
/// </summary>
|
/// <value>The un selected image path.</value>
|
public string UnSelectedImagePath { get; set; }
|
|
/// <summary>
|
/// 输入文字变化事件
|
/// </summary>
|
public Action<View, string> TextChangeEventHandler;
|
|
public void SetSingleLine(bool bSingle, int maxLines = 0)
|
{
|
currentAndroidEditText.SetSingleLine(bSingle);
|
if (!bSingle) {
|
currentAndroidEditText.SetMaxLines(maxLines);
|
}
|
}
|
|
/// <summary>
|
/// 刷新placeholderUILabel布局
|
/// </summary>
|
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;
|
|
}
|
|
}
|
|
}
|