Xamarin.Android实现简易计算器

背景

初学安卓开发,由于java不熟,AS也配置失败,于是转向了C#老本行😁

其实个人认为C#开发安卓比用java爽嘛……

采用了VS自带的模板"第一视图应用"

计算类采用了System.Data命名空间里的Compute方法,十分方便(但痛点在于得考虑输入的表达式是否合法,否则会出现内存闪退问题 (*  ̄︿ ̄) )

运行截图

废话少说直接上代码

布局:嵌套式页面

先在drawable文件夹里新建2个控件样式文件👇

elliptical_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <stroke
        android:color="#749FF4"
        android:width="3dip"/>
    <solid
        android:color="#0A59F7"/>
    <size
        android:width="62dp"
        android:height="62dp"/>
    <corners
        android:topLeftRadius="40dp"
        android:topRightRadius="40dp"
        android:bottomRightRadius="40dp"
        android:bottomLeftRadius="40dp"/>
</shape>

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:color="#F8F8F9"
        android:width="3dip"/>
    <solid
        android:color="#FCFCFC"/>
    <size
        android:width="62dp"
        android:height="62dp"/>
</shape>

布局

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include
        layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:foregroundGravity="left"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    android:id="@+id/layout1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="6dp"
            android:gravity="right"
            android:autoSizeTextType="uniform"
            android:id="@+id/tex_count"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            android:gravity="right"
            android:textSize="30sp"
            android:id="@+id/result"/>

    </LinearLayout>

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:rowCount="5"
        android:orientation="horizontal"
        android:layout_marginTop="12dp">

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="AC"
            android:textColor="#0A59F7"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="del"
            android:textSize="28sp"
            android:layout_margin="5dp"
            android:enabled="false"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_add"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="+"
            android:textColor="#0A59F7"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_sub"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="-"
            android:textColor="#0A59F7"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        

        <Button
            android:id="@+id/btn_nine"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="9"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_eight"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="8"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_seven"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="7"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_mul"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="×"
            android:textColor="#0A59F7"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        

        <Button
            android:id="@+id/btn_six"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="6"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_five"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="5"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_four"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="4"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_div"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="÷"
            android:textColor="#0A59F7"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        

        <Button
            android:id="@+id/btn_three"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="3"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_two"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="2"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_one"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="1"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_point"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="."
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_left"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="("
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_zero"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="0"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_right"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text=")"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/round_button"/>

        <Button
            android:id="@+id/btn_result"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="="
            android:enabled="false"
            android:textSize="30sp"
            android:layout_margin="5dp"
            android:background="@drawable/elliptical_button"/>
    </GridLayout>

</LinearLayout>

AboutLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="About"
        android:textSize="30sp"
        android:layout_gravity="center"
        android:textColor="#ff000000"
        android:textStyle="italic|bold"
        android:typeface="monospace" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


    </LinearLayout>
    <TextView
        android:text="Welcome ~"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/background_dark"
        android:layout_marginLeft="20dp"
        android:typeface="serif" />
    <TextView
        android:text="Thank you for downloading……"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/background_dark"
        android:layout_marginLeft="25dp"
        android:textStyle="italic"
        android:typeface="serif" />
    <TextView
        android:text="·This is a simple calculator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:textSize="19sp"
        android:textColor="#ff000000"/>
    <TextView
        android:text="You can press the button to edit formulas"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:textSize="19sp"
        android:textColor="#ff000000"/>
    <TextView
        android:text="Instructions :"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:textSize="19sp"
        android:textColor="#ff000000"
        android:textStyle="bold"
        android:layout_marginTop="15dp"/>
    <TextView
        android:text="Formulas should be input in a correct form !"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:textSize="19sp"
        android:textColor="#ffd50000"
        android:textStyle="bold|italic" />
    <TextView
        android:text="For example :"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:textSize="19sp"
        android:textColor="#ff000000"/>
    <TextView
        android:text="Formulas like  +=  or  4+=.2  are illogical ! "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:textSize="15sp"/>
    <TextView
        android:text="Neither are those like  ()2  or  3+))2 . "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:textSize="15sp"/>
    <TextView
        android:text="Email at"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="80dp"
        android:textSize="20sp"
        android:textColor="#ff000000"/>
    <TextView
        android:text="tony080410@outlook.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="20dp"
        android:textSize="16sp"
        android:textColor="#ff000000"
        android:textStyle="italic" />
    <TextView
        android:text="or lpj080410@outlook.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="5dp"
        android:textSize="16sp"
        android:textColor="#ff000000"
        android:textStyle="italic" />
    <TextView
        android:text="for more information"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="45dp"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:textColor="#ff000000"/>
    <TextView
        android:text="©All Rights Reserved Tony"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:layout_marginTop="40dp" />
    
</LinearLayout>

后台逻辑

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.App;
using Android.Content;
using Android.Widget;
using System.Collections.Generic;

namespace Simple_Calculator
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, View.IOnClickListener
    {
        TextView tex_count;
        TextView tex_result;
        Button btn_clear;
        Button btn_one;
        Button btn_two;
        Button btn_three;
        Button btn_four;
        Button btn_five;
        Button btn_six;
        Button btn_seven;
        Button btn_eight;
        Button btn_nine;
        Button btn_zero;
        Button btn_point;
        Button btn_add;
        Button btn_mul;
        Button btn_div;
        Button btn_sub;
        Button btn_left;
        Button btn_right;
        Button btn_result;
        Button btn_delete;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            AndroidX.AppCompat.Widget.Toolbar toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            tex_count = FindViewById<TextView>(Resource.Id.tex_count);
            tex_result = FindViewById<TextView>(Resource.Id.result);

            btn_one = FindViewById<Button>(Resource.Id.btn_one);
            btn_two = FindViewById<Button>(Resource.Id.btn_two);
            btn_three = FindViewById<Button>(Resource.Id.btn_three);
            btn_four = FindViewById<Button>(Resource.Id.btn_four);
            btn_five = FindViewById<Button>(Resource.Id.btn_five);
            btn_six = FindViewById<Button>(Resource.Id.btn_six);
            btn_seven = FindViewById<Button>(Resource.Id.btn_seven);
            btn_eight = FindViewById<Button>(Resource.Id.btn_eight);
            btn_nine = FindViewById<Button>(Resource.Id.btn_nine);
            btn_zero = FindViewById<Button>(Resource.Id.btn_zero);
            btn_clear = FindViewById<Button>(Resource.Id.btn_clear);
            btn_add = FindViewById<Button>(Resource.Id.btn_add);
            btn_sub = FindViewById<Button>(Resource.Id.btn_sub);
            btn_mul = FindViewById<Button>(Resource.Id.btn_mul);
            btn_div = FindViewById<Button>(Resource.Id.btn_div);
            btn_point = FindViewById<Button>(Resource.Id.btn_point);
            btn_result = FindViewById<Button>(Resource.Id.btn_result);
            btn_left = FindViewById<Button>(Resource.Id.btn_left);
            btn_right = FindViewById<Button>(Resource.Id.btn_right);
            btn_delete = FindViewById<Button>(Resource.Id.btn_delete);

            
            //为每一个控件注册监听器
            btn_clear.SetOnClickListener(this);
            btn_one.SetOnClickListener(this);
            btn_two.SetOnClickListener(this);
            btn_three.SetOnClickListener(this);
            btn_four.SetOnClickListener(this);
            btn_five.SetOnClickListener(this);
            btn_six.SetOnClickListener(this);
            btn_seven.SetOnClickListener(this);
            btn_eight.SetOnClickListener(this);
            btn_nine.SetOnClickListener(this);
            btn_zero.SetOnClickListener(this);
            btn_point.SetOnClickListener(this);
            btn_add.SetOnClickListener(this);
            btn_mul.SetOnClickListener(this);
            btn_sub.SetOnClickListener(this);
            btn_div.SetOnClickListener(this);
            btn_result.SetOnClickListener(this);
            btn_right.SetOnClickListener(this);
            btn_left.SetOnClickListener(this);
            btn_delete.SetOnClickListener(this);



        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu_main, menu);
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            int id = item.ItemId;
            if (id == Resource.Id.action_about)
            {
                Intent intent_about = new Intent(this, typeof(AboutActivity));
                StartActivity(intent_about);
                return true;
            }

            return base.OnOptionsItemSelected(item);
        }



        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        public void OnClick(View v)
        {
            switch (v.Id)
            {
                case Resource.Id.btn_clear:
                    tex_count.Text = string.Empty;
                    tex_result.Text = string.Empty;
                    btn_clear.Text = "AC";
                    btn_result.Enabled = false;
                    btn_delete.Enabled = false;
                    break;
                case Resource.Id.btn_one:
                    tex_count.Text += "1";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_two:
                    tex_count.Text += "2";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_three:
                    tex_count.Text += "3";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_four:
                    tex_count.Text += "4";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_five:
                    tex_count.Text += "5";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_six:
                    tex_count.Text += "6";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_seven:
                    tex_count.Text += "7";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_eight:
                    tex_count.Text += "8";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_nine:
                    tex_count.Text += "9";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_point:
                    tex_count.Text += ".";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_zero:
                    tex_count.Text += "0";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_add:
                    tex_count.Text += "+";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_mul:
                    tex_count.Text += "*";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_sub:
                    tex_count.Text += "-";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_div:
                    tex_count.Text += "/";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_right:
                    tex_count.Text += ")";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_left:
                    tex_count.Text += "(";
                    btn_clear.Text = "C";
                    btn_result.Enabled = true;
                    btn_delete.Enabled = true;
                    break;
                case Resource.Id.btn_result:
                    //判断输入表达式是否合法
                    if (CheckBracket(tex_count.Text) == false
                        || tex_count.Text.StartsWith(".")
                        || tex_count.Text.StartsWith("*")
                        || tex_count.Text.StartsWith("/")
                        || tex_count.Text.StartsWith(")")
                        || tex_count.Text.EndsWith("*")
                        || tex_count.Text.EndsWith("/")
                        || tex_count.Text.EndsWith("(")
                        || tex_count.Text.EndsWith("+")
                        || tex_count.Text.EndsWith("-")
                        || tex_count.Text.EndsWith(".")
                        || tex_count.Text.Contains("..")
                        || tex_count.Text.Contains("+-")
                        || tex_count.Text.Contains("++")
                        || tex_count.Text.Contains("--")
                        || tex_count.Text.Contains("**")
                        || tex_count.Text.Contains("//")
                        || tex_count.Text.Contains("+*")
                        || tex_count.Text.Contains("+/")
                        || tex_count.Text.Contains("+)")
                        || tex_count.Text.Contains("-)")
                        || tex_count.Text.Contains("*)")
                        || tex_count.Text.Contains("/)")
                        || tex_count.Text.Contains("()")
                        || tex_count.Text.Contains(".)")
                        || tex_count.Text.Contains(".(")
                        || tex_count.Text.Contains(".*")
                        || tex_count.Text.Contains("./")
                        || tex_count.Text.Contains(".+")
                        || tex_count.Text.Contains(".-")
                        || tex_count.Text.Contains("0(")
                        || tex_count.Text.Contains("1(")
                        || tex_count.Text.Contains("2(")
                        || tex_count.Text.Contains("3(")
                        || tex_count.Text.Contains("4(")
                        || tex_count.Text.Contains("5(")
                        || tex_count.Text.Contains("6(")
                        || tex_count.Text.Contains("7(")
                        || tex_count.Text.Contains("8(")
                        || tex_count.Text.Contains("9(")
                        || CheckPoint(tex_count.Text) == false)
                        tex_result.Text = "Error";
                    else
                    {
                        tex_result.Text = CalculatorClass.calculate(tex_count.Text).ToString();
                        btn_result.Enabled = false;
                        btn_delete.Enabled = false;

                    }

                    break;
                case Resource.Id.btn_delete:
                    tex_count.Text = tex_count.Text.Substring(0, tex_count.Text.Length - 1);
                    if (tex_count.Text.Length == 0)
                    {
                        btn_result.Enabled = false;
                        btn_delete.Enabled = false;
                        btn_clear.Text = "AC";
                    }
                    break;
                default: break;
            }
        }
        public static bool CheckBracket(string inputStr)
        {
            //检查括号匹配
            Stack<char> leftBracket = new Stack<char>();
            for (int i = 0; i < inputStr.Length; i++)
            {
                if (inputStr[i] == '(')
                {
                    leftBracket.Push(inputStr[i]);
                }
                else if (inputStr[i] == ')')
                {
                    if (leftBracket.Count == 0 || '(' != leftBracket.Pop())
                    {
                        return false;
                    }
                }
            }
            if (leftBracket.Count > 0)
            {
                return false;
            }
            return true;
        }
        public static bool CheckPoint(string inputStr)
        {
            //检测是否有类似于"23.23.23”的情况
            Stack<char> point = new Stack<char>();
            for (int i = 0; i < inputStr.Length; i++)
            {
                if (inputStr[i] == '.'
                    || inputStr[i] == '+'
                    || inputStr[i] == '-'
                    || inputStr[i] == '*'
                    || inputStr[i] == '/'
                    || inputStr[i] == '('
                    || inputStr[i] == ')')
                    point.Push(inputStr[i]);
            }
            string str = string.Concat(point);
            if (str.Contains(".."))
                return false;
            return true;
        }
    }
}

AboutActivity.cs

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simple_Calculator
{
    [Activity(Label = "AboutActivity")]
    public class AboutActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.AboutLayout);
        }
    }
}

CalculatorClass.cs

using System.Data;

namespace Simple_Calculator
{
    internal class CalculatorClass
    {
        internal static double calculate(string expression)
        {
            object result = new DataTable().Compute(expression, "");
            return double.Parse(result + "");
        }
    }
}

注意事项

①修改menu.xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <item
        android:id="@+id/action_about"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>
②修改styles.xml文件

注意AppTheme一定要设为Theme.Design.Light,否则无法正常显示按钮样式

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.Design.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />

</resources>
③修改strings.xml文件
<resources>
    <string name="app_name">Simple Calculator</string>
    <string name="action_settings">About</string>
</resources>
④在项目引用里添加System.Data

否则Compute方法无法正常使用

如图

写在最后

①附上项目结构图
②xamarin.android国内资源太少,鄙人开发过程中历经坎坷,以后会持续更新,替大家踩坑!
③这个项目总体来说还算完美,若有问题欢迎各位大佬指出修改!

时间紧,没写什么注释,恳请原谅……

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值