wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
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));
                    }
                }
            }
        }
 
    }
}