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.Graphics;
using Android.Graphics.Drawables;
namespace Shared
{
///
/// Button 按键
///
public class Button : View
{
///
/// 当前视图
///
/// The android button.
AndroidButton currentButton
{
get
{
return androidView as AndroidButton;
}
set
{
this.androidView = value;
}
}
public Button()
{
currentButton = new AndroidButton (Application.Activity);
currentButton.SetTextColor (Android.Graphics.Color.White);
currentButton.MouseDownEventHandler += (sender, e) => {
OnMouseDown();
};
currentButton.MouseUPEventHandler += (sender, e) => {
OnMouseUp();
};
currentButton.SizeChangeEventHandler+= (sender, e) => {
OnSizeChange(e);
};
}
///
/// 文字大小
///
/// The size of the text.
public float TextSize
{
get
{
return currentButton.TextSize;
}
set
{
currentButton.SetTextSize(ComplexUnitType.Sp, value);
}
}
int textID;
///
/// 根据ID获取对应的备注
///
/// The text I.
public int TextID
{
get
{
return textID;
}
set
{
textID = value;
Text = Language.StringByID (TextID);
}
}
///
/// 文本
///
/// The text.
public string Text
{
get
{
return currentButton.Text;
}
set
{
currentButton.Text = value;
}
}
///
/// 刷新
///
public override void Refresh()
{
base.Refresh ();
IsSelected = isSelected;
}
private uint textColor;
///
/// 文字颜色
///
/// The color of the text.
public uint TextColor
{
get
{
return textColor;
}
set
{
textColor = value;
byte r, g, b, a;
r = (byte)(this.textColor / 256 / 256 % 256);
g = (byte)(this.textColor / 256 % 256);
b = (byte)(this.textColor % 256);
a = (byte)(this.textColor / 256 / 256 / 256 % 256);
currentButton.SetTextColor(Android.Graphics.Color.Argb(a, r, g, b));
}
}
private bool isSelected;
private string beforeImagePath;
///
/// 选中状态
///
/// true if this instance is selected; otherwise, false.
public bool IsSelected
{
get
{
return this.isSelected;
}
set
{
this.isSelected = value;
if (this.RealView.Parent == null || Radius != 0)
{
return;
}
if (this.isSelected)
{
if (beforeImagePath == this.SelectedImagePath)
{
return;
}
if (this.Background(this.SelectedImagePath))
{
beforeImagePath = this.SelectedImagePath;
}
}
else
{
if (beforeImagePath == this.UnSelectedImagePath)
{
return;
}
if (this.Background(this.UnSelectedImagePath))
{
beforeImagePath = this.UnSelectedImagePath;
}
}
}
}
private TextAlignment textAlignment= TextAlignment.Center;
///
/// 文字对齐方式
///
/// Horizontal alignment.
/// Vertical alignment.
public TextAlignment TextAlignment {
get {
return this.textAlignment;
}
set {
this.textAlignment = value;
switch (value) {
case TextAlignment.TopLeft:
this. currentButton.Gravity = GravityFlags.Top | GravityFlags.Left;
break;
case TextAlignment.TopCenter:
this. currentButton.Gravity = GravityFlags.Top | GravityFlags.Center;
break;
case TextAlignment.TopRight:
this. currentButton.Gravity = GravityFlags.Top | GravityFlags.Right;
break;
case TextAlignment.CenterLeft:
this. currentButton.Gravity = GravityFlags.Center | GravityFlags.Left;
break;
case TextAlignment.Center:
this. currentButton.Gravity = GravityFlags.Center | GravityFlags.Center;
break;
case TextAlignment.CenterRight:
this. currentButton.Gravity = GravityFlags.Center | GravityFlags.Right;
break;
case TextAlignment.BottomLeft:
this. currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Left;
break;
case TextAlignment.BottomCenter:
this. currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Center;
break;
case TextAlignment.BottomRight:
this. currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Right;
break;
}
}
}
///
/// 内边距
///
/// The padding.
public override Padding Padding {
get {
return new Padding (this. currentButton.PaddingTop, this. currentButton.PaddingLeft,this. currentButton.PaddingBottom, this. currentButton.PaddingRight);
}
set {
//base.Padding = value;
this. currentButton.SetPadding (DensityUtil.Dip2Px( value.Left),DensityUtil.Dip2Px( value.Top),DensityUtil.Dip2Px( value.Right),DensityUtil.Dip2Px( value.Bottom));
}
}
private string selectedImagePath;
///
/// 选择时背景图路径
///
/// The selected image path.
public string SelectedImagePath
{
get
{
return this.selectedImagePath;
}
set{
this.selectedImagePath = value;
IsSelected = IsSelected;
}
}
private string unSelectedImagePath;
///
/// 非选中状态的背景图路径
///
/// The un selected image path.
public string UnSelectedImagePath
{
get
{
return this.unSelectedImagePath;
}
set
{
this.unSelectedImagePath = value;
IsSelected = IsSelected;
}
}
}
public class AndroidButton : Android.Widget.Button
{
public AndroidButton(Context context)
: base(context)
{
this.SetBackgroundColor (Android.Graphics.Color.Transparent);
this.SetPadding (0, 0, 0, 0);
}
public AndroidButton(Context context, IAttributeSet attrs)
: base(context, attrs) { }
protected AndroidButton(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
public AndroidButton(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle) { }
///
/// 点击弹起事件
///
public EventHandler MouseUPEventHandler;
///
/// 点击按下事件
///
public EventHandler MouseDownEventHandler;
///
/// 点击事件
///
/// The touch event.
/// E.
public override bool OnTouchEvent(Android.Views.MotionEvent e)
{
if (e.Action == Android.Views.MotionEventActions.Down && MouseDownEventHandler != null)
{
MouseDownEventHandler(this, EventArgs.Empty);
}
else if (e.Action == Android.Views.MotionEventActions.Up && MouseUPEventHandler != null)
{
MouseUPEventHandler(this, EventArgs.Empty);
}
return base.OnTouchEvent(e);
}
///
/// 大小变化事件
///
public EventHandler SizeChangeEventHandler;
public override ViewGroup.LayoutParams LayoutParameters
{
get
{
return base.LayoutParameters;
}
set
{
var layoutParams = base.LayoutParameters;
base.LayoutParameters = value;
if (layoutParams == null || layoutParams.Width != value.Width || layoutParams.Height != value.Height)
{
if (this.SizeChangeEventHandler != null)
{
this.SizeChangeEventHandler(this, new Size((int)this.LayoutParameters.Width, (int)this.LayoutParameters.Height));
}
}
}
}
}
}