Created
May 1, 2017 20:10
-
-
Save cjlotz/ce03548bea1ee16b6c0951a38404c414 to your computer and use it in GitHub Desktop.
Full custom view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.Content; | |
using Android.Util; | |
using Android.Support.Design.Widget; | |
using Android.Runtime; | |
using System; | |
using Android.Views; | |
using Android.Content.Res; | |
using Android.Text; | |
using Android.Views.InputMethods; | |
namespace InTouch.Controls | |
{ | |
[Register("intouch.controls.ITTextInputLayout")] | |
public class ITTextInputLayout : TextInputLayout, IITComposite | |
{ | |
private bool _subscribed; | |
public EventHandler TextChanged; | |
public ITTextInputLayout(Context context) : this(context, null) | |
{ | |
} | |
public ITTextInputLayout(Context context, IAttributeSet attrs) : base(context, attrs) | |
{ | |
LayoutInflater inflator = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService); | |
inflator.Inflate(Resource.Layout.control_textinput, this); | |
Control = (TextInputEditText) EditText; | |
TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.ITTextInputLayout); | |
try | |
{ | |
InputType = (InputTypes)a.GetInt(Resource.Styleable.ITTextInputLayout_android_inputType, (int)InputTypes.ClassText); | |
ImeOptions = (ImeAction)a.GetInt(Resource.Styleable.ITTextInputLayout_android_imeOptions, (int)ImeAction.None); | |
MaxLines = a.GetInt(Resource.Styleable.ITTextInputLayout_android_maxLines, 1); | |
Text = a.GetString(Resource.Styleable.ITTextInputLayout_android_text); | |
Header = a.GetString(Resource.Styleable.ITTextInputLayout_ItcHeader); | |
Error = a.GetString(Resource.Styleable.ITTextInputLayout_ItcError); | |
} | |
finally | |
{ | |
a.Recycle(); | |
} | |
} | |
protected ITTextInputLayout(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) | |
{ | |
} | |
#region Properties | |
public string Text | |
{ | |
get { return Control.Text; } | |
set { Control.Text = value; } | |
} | |
public int MaxLines | |
{ | |
get { return Control.MaxLines; } | |
set { Control.SetMaxLines(value); } | |
} | |
public InputTypes InputType | |
{ | |
get { return Control.InputType; } | |
set { Control.InputType = value; } | |
} | |
public ImeAction ImeOptions | |
{ | |
get { return Control.ImeOptions; } | |
set { Control.ImeOptions = value; } | |
} | |
public string Header | |
{ | |
get { return Hint; } | |
set { Hint = value; } | |
} | |
public new string Error | |
{ | |
get { return base.Error; } | |
set | |
{ | |
string error = value as string; | |
if (error != null) | |
{ | |
ErrorEnabled = true; | |
base.Error = error; | |
} | |
else | |
{ | |
ErrorEnabled = false; | |
base.Error = null; | |
} | |
} | |
} | |
private TextInputEditText Control { get; set; } | |
#endregion | |
#region Methods | |
protected override void OnAttachedToWindow() | |
{ | |
base.OnAttachedToWindow(); | |
Control.AfterTextChanged += HandleAfterTextChanged; | |
_subscribed = true; | |
} | |
protected override void OnDetachedFromWindow() | |
{ | |
base.OnDetachedFromWindow(); | |
Control.AfterTextChanged -= HandleAfterTextChanged; | |
_subscribed = false; | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
if (Control != null && _subscribed) | |
{ | |
Control.AfterTextChanged -= HandleAfterTextChanged; | |
_subscribed = false; | |
} | |
} | |
base.Dispose(disposing); | |
} | |
#endregion | |
#region Event Handlers | |
private void HandleAfterTextChanged(object sender, EventArgs e) | |
{ | |
TextChanged?.Invoke(this, e); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment