好友
阅读权限10
听众
最后登录1970-1-1
|
楼主|
南语喃
发表于 2017-11-27 12:36
关于Xamarin,国内的资源非常缺乏。
我也是刚接触Xamarin,手动百度Android,然后翻译成C#。
源码就自己做的一个计算器。
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using System.Collections.Generic;
using Android.Views;
using TidyScriptCore;
using Android.Text;
using Android.Views.InputMethods;
using Android.Content;
using System;
using Android.Runtime;
using System.Threading;
namespace Calculator
{
[Activity(Label = "Calculator", MainLauncher = true,WindowSoftInputMode = (SoftInput.StateHidden))]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RequestWindowFeature( Android.Views.WindowFeatures.NoTitle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//
//界面设计
//Code 1/2
//Bs 1/4
//Result 1/4
WindowsInfo Info = new WindowsInfo();
Info.activity = this;
int Height = Info.Height;
int Width = Info.Width;
//
List<string> Keys = new List<string>();
Keys.AddRange(new List<string>() { "A", "Cos()", "Sin()", "Tan()" });
Keys.AddRange(new List<string>() { "Rand()",",","*","/"});
Keys.AddRange(new List<string>() { "()", "[]", "+", "-" });
Keys.AddRange(new List<string>() { "1","2","3","=","4","5","6","←","7","8","9","Enter","^","0",".","Ans","X","Y","3.14159","Figure"});
//
EditText tBox_Code = (EditText)FindViewById(Resource.Id.tBox_Code);
LinearLayout llV = (LinearLayout)FindViewById(Resource.Id.llV);
TextView tBox_Result = (TextView)FindViewById(Resource.Id.tBox_Result);
CheckBox cBox = (CheckBox)FindViewById(Resource.Id.cBox);
//
tBox_Result.Text = "\nBy 南语喃";
tBox_Code.Top = 0;
ViewGroup.LayoutParams lp = tBox_Code.LayoutParameters;
lp.Height = Height / 5;
lp = tBox_Result.LayoutParameters;
lp.Height = Height / 7;
lp = cBox.LayoutParameters;
lp.Height = 50;
//
while (Keys.Count != 0)
{
LinearLayout Liner = new LinearLayout(Application.Context);
Liner.Orientation = Orientation.Horizontal;
Liner.Measure(0, 0);
Button b = new Button(Application.Context);
b.Text = Keys[0];
b.Measure(0, 0);
b.Click += B_Click;
Process_LongCB(b);
Keys.RemoveAt(0);
Liner.AddView(b);
int CountOfB = 0;
while (CountOfB < 3 && Keys.Count != 0)
{
CountOfB++;
b = new Button(Application.Context);
b.Text = Keys[0];
b.Measure(0, 0);
b.Click += B_Click;
Process_LongCB(b);
Keys.RemoveAt(0);
Liner.AddView(b);
}
llV.AddView(Liner);
}
tBox_Code.Click += (sender, e) =>
{
if (cBox.Checked)
{
HideInput(Application, tBox_Code);
}
};
}
private class OTouchL : Activity,View.IOnTouchListener
{
public LongDelete LD = new LongDelete();
public bool OnTouch(View v, MotionEvent e)
{
Button b = (Button)v;
if (b.Text.ToLower() == "←")
{
if (e.Action == MotionEventActions.Down)
{
LD.Start();
}else if(e.Action == MotionEventActions.Up)
{
LD.Finish();
}
}
return true;
}
}
private void Process_LongCB(Button b)
{
if (b.Text.ToLower() == "←")
{
EditText tBox_Code = (EditText)FindViewById(Resource.Id.tBox_Code);
OTouchL OTL = new OTouchL();
MHandler mh = new MHandler();
mh.tBox = tBox_Code;
OTL.LD.hd = mh;
b.SetOnTouchListener(OTL);
}
}
public static void HideInput(Context context, View view)
{
InputMethodManager inputMethodManager = (InputMethodManager)context.GetSystemService(Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(view.WindowToken, 0);
}
private void B_Click(object sender, System.EventArgs e)
{
Button b = (Button)sender;
EditText tBox_Code = (EditText)FindViewById(Resource.Id.tBox_Code);
TextView tBox_Result = (TextView)FindViewById(Resource.Id.tBox_Result);
if (b.Text.ToLower() == "figure")
{
UScript Script = new UScript();
Config.FileSplit = "/";
Config.StartPath = Application.Context.FilesDir.AbsolutePath;
Script.Eval_Caclulator(tBox_Code.Text);
tBox_Result.Text = Log.ToString();
}
else if (b.Text.ToLower() == "←")
{
Delete();
}
else if (b.Text.ToLower() == "enter")
{
Inset("\n");
}else if(b.Text .ToLower().IndexOf("()")!=-1|| b.Text.ToLower().IndexOf("[]") != -1)
{
Inset(b.Text);
tBox_Code.SetSelection(tBox_Code.SelectionStart-1);
}
else
{
Inset(b.Text);
}
}
private void Delete()
{
EditText tBox_Code = (EditText)FindViewById(Resource.Id.tBox_Code);
IEditable edit = tBox_Code.EditableText;
int Index = tBox_Code.SelectionStart;
if (Index != 0 && tBox_Code.Text.Length != 0)
{
edit.Delete(Index - 1, Index);
}
}
private void Inset(string Content)
{
EditText tBox_Code = (EditText)FindViewById(Resource.Id.tBox_Code);
int Index = tBox_Code.SelectionStart;
IEditable edit = tBox_Code.EditableText;
if (Index < 0 || Index > tBox_Code.Text.Length)
{
edit.Append(Content);
}
else
{
edit.Insert(Index, Content);
}
}
}
public class WindowsInfo
{
private Activity _activity = new Activity();
public Activity activity
{
get
{
return _activity;
}
set
{
_activity = value;
_activity.Window.WindowManager.DefaultDisplay.GetSize(Size);
}
}
//
private Point Size = new Point();
public int Width
{
get
{
return Size.X;
}
}
public int Height
{
get
{
return Size.Y;
}
}
}
public class LongDelete
{
public bool Switch = false;
private bool First = true;
public int Span = 100;
public Handler hd;
private void Run()
{
FlagStart:
int LSpan = Span;
int Count = 0;
while (Switch)
{
if (Count >= 20)
{
Message message = new Message();
message.Arg1 = 2;
hd.SendMessage(message);
Switch = false;
}
else
{
Message message = new Message();
message.Arg1 = 1;
hd.SendMessage(message);
Thread.Sleep(LSpan);
if (LSpan > 50)
{
LSpan = LSpan - 10;
}
}
Count++;
}
while (!Switch)
{
Thread.Sleep(Span);
}
goto FlagStart;
}
//
public void Start()
{
Switch=true;
if (First)
{
First = false;
Thread t = new Thread(Run);
t.Start();
}
}
public void Finish()
{
Switch = false;
}
}
public class MHandler : Handler
{
public EditText tBox;
public override void HandleMessage(Message msg)
{
base.HandleMessage(msg);
if (msg.Arg1 == 1)
{
Delete();
}else if(msg.Arg1 == 2)
{
tBox.Text = string.Empty;
}
}
private void Delete()
{
IEditable edit = tBox.EditableText;
int Index = tBox.SelectionStart;
if (Index != 0 && tBox.Text.Length != 0)
{
edit.Delete(Index - 1, Index);
}
}
}
}
|
|