forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcQuick.cs
414 lines (342 loc) · 13.3 KB
/
CalcQuick.cs
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#region Copyright Syncfusion Inc. 2001-2019.
// Copyright Syncfusion Inc. 2001-2019. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
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.Graphics;
using Android.Views.InputMethods;
using Syncfusion.Calculate;
namespace SampleBrowser
{
public class CalcQuick : SamplePage, IDisposable
{
#region Fields
private CalcQuickBase calcQuickBase;
private ScrollView scrollView;
private EditText editTextA;
private EditText editTextB;
private EditText editTextC;
private TextViewExt result1;
private TextViewExt result2;
private TextViewExt result3;
private TextView textA;
private TextView textB;
private TextView textC;
private Context context;
#endregion
#region Methods
public override View GetSampleContent(Context context)
{
this.context = context;
CreateCalcQuickDesign(context);
return scrollView;
}
private void CreateCalcQuickDesign(Context context)
{
int width = context.Resources.DisplayMetrics.WidthPixels;
var density = context.Resources.DisplayMetrics.Density;
InitializeCalcQuick();
scrollView = new ScrollView(context);
scrollView.SetPadding(10, 10, 10, 20);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.Orientation = Orientation.Vertical;
linearLayout.Focusable = true;
linearLayout.FocusableInTouchMode = true;
LinearLayout linearLayout1 = new LinearLayout(context);
linearLayout1.Orientation = Orientation.Vertical;
TextView titleText = new TextView(context)
{
Text = "CalcQuick Calculation",
TextAlignment = TextAlignment.Center,
Gravity = GravityFlags.Center,
TextSize = 30
};
titleText.SetTypeface(null, TypefaceStyle.Bold);
titleText.SetWidth(width);
titleText.SetHeight((int)(50 * density));
TextView descText = new TextView(context)
{
Text = "This sample demonstrates the calculation via keys and expressions using CalcQuick.",
TextAlignment = TextAlignment.TextStart,
Gravity = GravityFlags.FillHorizontal,
TextSize = 12
};
descText.SetPadding(0, 10, 0, 0);
descText.SetWidth(width);
descText.SetHeight((int)(40 * density));
linearLayout1.AddView(titleText);
linearLayout1.AddView(descText);
LinearLayout linearLayout2 = new LinearLayout(context);
LinearLayout linearLayout3 = new LinearLayout(context);
LinearLayout linearLayout4 = new LinearLayout(context);
linearLayout2.Orientation = Orientation.Horizontal;
linearLayout3.Orientation = Orientation.Horizontal;
linearLayout4.Orientation = Orientation.Horizontal;
linearLayout2.SetPadding(0, 30, 0, 0);
linearLayout3.SetPadding(0, 25, 0, 0);
linearLayout4.SetPadding(0, 25, 0, 0);
textA = CreateTextView(context, "Key A", 15);
textA.SetWidth((int)(width * 0.5));
textA.SetHeight((int)(20 * density));
textB = CreateTextView(context, "Key B", 15);
textB.SetWidth((int)(width * 0.5));
textB.SetHeight((int)(20 * density));
textC = CreateTextView(context, "Key C", 15);
textC.SetWidth((int)(width * 0.5));
textC.SetHeight((int)(20 * density));
editTextA = CreateEditText(context, calcQuickBase["A"]);
editTextA.SetWidth(width - (int)(width * 0.5));
editTextA.SetHeight((int)(20 * density));
editTextA.SetSingleLine(true);
editTextB = CreateEditText(context, calcQuickBase["B"]);
editTextB.SetWidth(width - (int)(width * 0.5));
editTextB.SetHeight((int)(20 * density));
editTextB.SetSingleLine(true);
editTextC = CreateEditText(context, calcQuickBase["C"]);
editTextC.SetWidth(width - (int)(width * 0.5));
editTextC.SetHeight((int)(20 * density));
editTextC.SetSingleLine(true);
linearLayout2.AddView(textA);
linearLayout3.AddView(textB);
linearLayout4.AddView(textC);
linearLayout2.AddView(editTextA);
linearLayout3.AddView(editTextB);
linearLayout4.AddView(editTextC);
LinearLayout linearLayout5 = new LinearLayout(context);
linearLayout5.SetPadding(0, 15, 0, 0);
Button compute = new Button(context);
compute.Text = "Compute";
compute.Click += Compute_Click;
compute.SetWidth(width);
compute.SetHeight((int)(20 * density));
linearLayout5.AddView(compute);
LinearLayout linearLayout6 = new LinearLayout(context);
LinearLayout linearLayout7 = new LinearLayout(context);
LinearLayout linearLayout8 = new LinearLayout(context);
LinearLayout linearLayout9 = new LinearLayout(context);
linearLayout6.Orientation = Orientation.Horizontal;
linearLayout7.Orientation = Orientation.Horizontal;
linearLayout8.Orientation = Orientation.Horizontal;
linearLayout9.Orientation = Orientation.Horizontal;
linearLayout6.SetPadding(0, 40, 0, 0);
linearLayout7.SetPadding(0, 60, 0, 0);
linearLayout8.SetPadding(0, 60, 0, 0);
linearLayout9.SetPadding(0, 60, 0, 0);
TextView expTitle = CreateTextView(context, "Expressions", 22);
expTitle.SetTypeface(null, TypefaceStyle.Bold);
expTitle.SetWidth(width);
expTitle.SetHeight((int)(30 * density));
TextView expression1 = CreateTextView(context, "Sum([A],[B],[C])", 15);
expression1.SetWidth((int)(width * 0.5));
expression1.SetHeight((int)(20 * density));
TextView expression2 = CreateTextView(context, "PI()*([A]^2)", 15);
expression2.SetWidth((int)(width * 0.5));
expression2.SetHeight((int)(20 * density));
TextView expression3 = CreateTextView(context, "Concatenate([A],[B],[C])", 15);
expression3.SetWidth((int)(width * 0.5));
expression2.SetHeight((int)(20 * density));
result1 = CreateTextViewExt(context, calcQuickBase["Exp1"]);
result1.SetWidth(width - (int)(width * 0.5));
result1.SetHeight((int)(20 * density));
result1.SetSingleLine(true);
result2 = CreateTextViewExt(context, calcQuickBase["Exp2"]);
result2.SetWidth(width - (int)(width * 0.5));
result2.SetHeight((int)(20 * density));
result2.SetSingleLine(true);
result3 = CreateTextViewExt(context, calcQuickBase["Exp3"]);
result3.SetWidth(width - (int)(width * 0.5));
result3.SetHeight((int)(20 * density));
result3.SetSingleLine(true);
linearLayout6.AddView(expTitle);
linearLayout7.AddView(expression1);
linearLayout8.AddView(expression2);
linearLayout9.AddView(expression3);
linearLayout7.AddView(result1);
linearLayout8.AddView(result2);
linearLayout9.AddView(result3);
linearLayout.AddView(linearLayout1);
linearLayout.AddView(linearLayout2);
linearLayout.AddView(linearLayout3);
linearLayout.AddView(linearLayout4);
linearLayout.AddView(linearLayout5);
linearLayout.AddView(linearLayout6);
linearLayout.AddView(linearLayout7);
linearLayout.AddView(linearLayout8);
linearLayout.AddView(linearLayout9);
linearLayout.Touch += (object sender, View.TouchEventArgs e) =>
{
if (e.Event.Action == MotionEventActions.Up)
{
ClearFocus();
}
};
scrollView.AddView(linearLayout);
}
private void Compute_Click(object sender, EventArgs e)
{
calcQuickBase["A"] = editTextA.Text;
calcQuickBase["B"] = editTextB.Text;
calcQuickBase["C"] = editTextC.Text;
calcQuickBase.SetDirty();
result1.Text = calcQuickBase["Exp1"];
result2.Text = calcQuickBase["Exp2"];
result3.Text = calcQuickBase["Exp3"];
ClearFocus();
}
private void InitializeCalcQuick()
{
calcQuickBase = new CalcQuickBase();
calcQuickBase["A"] = "25";
calcQuickBase["B"] = "50";
calcQuickBase["C"] = "10";
calcQuickBase["Exp1"] = "=Sum([A],[B],[C])";
calcQuickBase["Exp2"] = "=PI()*([A]^2)";
calcQuickBase["Exp3"] = "=Concatenate([A],[B],[C])";
}
private TextView CreateTextView(Context context, string text, int size)
{
return new TextView(context)
{
Text = text,
Gravity = GravityFlags.Start,
TextSize = size
};
}
private EditText CreateEditText(Context context, string text)
{
return new EditText(context)
{
Text = text,
TextSize = 15
};
}
private TextViewExt CreateTextViewExt(Context context, string text)
{
return new TextViewExt(context)
{
Text = text,
TextSize = 15
};
}
private void ClearFocus()
{
if (editTextA.IsFocused || editTextB.IsFocused || editTextC.IsFocused)
{
editTextA.ClearFocus();
editTextB.ClearFocus();
editTextC.ClearFocus();
}
Activity activity = (Activity)context;
InputMethodManager inputMethodManager = (InputMethodManager)activity.GetSystemService(Activity.InputMethodService);
if (activity.CurrentFocus != null)
{
inputMethodManager.HideSoftInputFromWindow(activity.CurrentFocus.WindowToken, 0);
}
}
public override void Destroy()
{
scrollView = null;
calcQuickBase.Dispose();
calcQuickBase = null;
editTextA = null;
editTextB = null;
editTextC = null;
result1 = null;
result2 = null;
result3 = null;
textA = null;
textB = null;
textC = null;
context = null;
base.Destroy();
}
public void Dispose()
{
if (scrollView != null)
{
scrollView.Dispose();
scrollView = null;
}
if (calcQuickBase != null)
{
calcQuickBase.Dispose();
calcQuickBase = null;
}
if (editTextA != null)
{
editTextA.Dispose();
editTextA = null;
}
if (editTextB != null)
{
editTextB.Dispose();
editTextB = null;
}
if (editTextC != null)
{
editTextC.Dispose();
editTextC = null;
}
if (result1 != null)
{
result1.Dispose();
result1 = null;
}
if (result2 != null)
{
result2.Dispose();
result2 = null;
}
if (result3 != null)
{
result3.Dispose();
result3 = null;
}
if (textA != null)
{
textA.Dispose();
textA = null;
}
if (textB != null)
{
textB.Dispose();
textB = null;
}
if (textC != null)
{
textC.Dispose();
textC = null;
}
}
#endregion
}
public class TextViewExt : TextView
{
private Paint paintBorder;
public TextViewExt(Context context)
: base(context)
{
paintBorder = new Paint(PaintFlags.AntiAlias);
paintBorder.Color = Color.Black;
paintBorder.TextAlign = Android.Graphics.Paint.Align.Center;
paintBorder.SetStyle(Android.Graphics.Paint.Style.FillAndStroke);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
canvas.DrawLine(0, this.Height, this.Width, this.Height, paintBorder);
}
}
}