最近在学安卓的相关知识,第一个demo做了一个简易计算器,功能仿手机上自带的计算器,没有加括号,简单的四则运算,支持长表达式运算。此篇贴出本次简易计算器完整逻辑代码。
代码如下:
package com.example.zwkkkk1.caculator1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.math.BigDecimal;
import java.util.Stack;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static String TAG = "CACULATOR";
TextView txt_result, txt_edit;
boolean isOperateDown = false;//运算符是否已经按过一次,默认没有按过 false
boolean isDotDown = false;//. 是否已经按过一次,默认没有按过 false
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_edit = (TextView)findViewById(R.id.txt_edit);
txt_result = (TextView)findViewById(R.id.txt_result);
findViewById(R.id.btn_0).setOnClickListener(this);
findViewById(R.id.btn_1).setOnClickListener(this);
findViewById(R.id.btn_2).setOnClickListener(this);
findViewById(R.id.btn_3).setOnClickListener(this);
findViewById(R.id.btn_4).setOnClickListener(this);
findViewById(R.id.btn_5).setOnClickListener(this);
findViewById(R.id.btn_6).setOnClickListener(this);
findViewById(R.id.btn_7).setOnClickListener(this);
findViewById(R.id.btn_8).setOnClickListener(this);
findViewById(R.id.btn_9).setOnClickListener(this);
findViewById(R.id.btn_divide).setOnClickListener(this);
findViewById(R.id.btn_multi).setOnClickListener(this);
findViewById(R.id.btn_plus).setOnClickListener(this);
findViewById(R.id.btn_sub).setOnClickListener(this);
findViewById(R.id.btn_equal).setOnClickListener(this);
findViewById(R.id.btn_clear).setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);
findViewById(R.id.btn_equal).setOnClickListener(this);
findViewById(R.id.btn_dot).setOnClickListener(this);
}
public void onClick