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
|
{
|
/// <summary>
|
/// Button 按键
|
/// </summary>
|
public class Button : View
|
{
|
/// <summary>
|
/// 当前视图
|
/// </summary>
|
/// <value>The android button.</value>
|
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);
|
};
|
}
|
|
/// <summary>
|
/// 文字大小
|
/// </summary>
|
/// <value>The size of the text.</value>
|
public float TextSize
|
{
|
get
|
{
|
return currentButton.TextSize;
|
}
|
set
|
{
|
currentButton.SetTextSize(ComplexUnitType.Sp, value);
|
}
|
}
|
|
int textID;
|
/// <summary>
|
/// 根据ID获取对应的备注
|
/// </summary>
|
/// <value>The text I.</value>
|
public int TextID
|
{
|
get
|
{
|
return textID;
|
}
|
set
|
{
|
textID = value;
|
Text = Language.StringByID (TextID);
|
}
|
}
|
|
/// <summary>
|
/// 文本
|
/// </summary>
|
/// <value>The text.</value>
|
public string Text
|
{
|
get
|
{
|
return currentButton.Text;
|
}
|
set
|
{
|
|
currentButton.Text = value;
|
}
|
}
|
|
/// <summary>
|
/// 刷新
|
/// </summary>
|
public override void Refresh()
|
{
|
base.Refresh ();
|
IsSelected = isSelected;
|
}
|
|
private 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)(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;
|
/// <summary>
|
/// 选中状态
|
/// </summary>
|
/// <value><c>true</c> if this instance is selected; otherwise, <c>false</c>.</value>
|
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;
|
/// <summary>
|
/// 文字对齐方式
|
/// </summary>
|
/// <param name="horizontalAlignment">Horizontal alignment.</param>
|
/// <param name="verticalAlignment">Vertical alignment.</param>
|
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;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 内边距
|
/// </summary>
|
/// <value>The padding.</value>
|
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;
|
/// <summary>
|
/// 选择时背景图路径
|
/// </summary>
|
/// <value>The selected image path.</value>
|
public string SelectedImagePath
|
{
|
get
|
{
|
return this.selectedImagePath;
|
}
|
set{
|
this.selectedImagePath = value;
|
IsSelected = IsSelected;
|
}
|
}
|
|
private string unSelectedImagePath;
|
/// <summary>
|
/// 非选中状态的背景图路径
|
/// </summary>
|
/// <value>The un selected image path.</value>
|
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) { }
|
|
/// <summary>
|
/// 点击弹起事件
|
/// </summary>
|
public EventHandler MouseUPEventHandler;
|
/// <summary>
|
/// 点击按下事件
|
/// </summary>
|
public EventHandler MouseDownEventHandler;
|
|
/// <summary>
|
/// 点击事件
|
/// </summary>
|
/// <returns>The touch event.</returns>
|
/// <param name="e">E.</param>
|
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);
|
}
|
|
/// <summary>
|
/// 大小变化事件
|
/// </summary>
|
public EventHandler<Size> 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));
|
}
|
}
|
}
|
}
|
|
}
|
}
|