Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple Calculator with Android

Shulou Source: shulou.com Published: 2022-06-03 05:04:07 10月05日 Update

This article mainly introduces Android how to achieve a simple calculator, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The details are as follows

Function

1. Four operations of addition, subtraction, multiplication and division

2. Return to 0

3. Fallback

4. real-time operation

Configuration

Add the following code to build.gradle (app)

BuildFeatures {viewBinding = true}

The location to join is as follows

CompileOptions {sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8} buildFeatures {viewBinding = true}

Overall Arrangement

Code

Public class MainActivity extends AppCompatActivity implements View.OnClickListener {private String tv= "0"; private ActivityMainBinding inflate; private String count= ""; private boolean isMax=false; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); inflate = ActivityMainBinding.inflate (getLayoutInflater ()); setContentView (inflate.getRoot ()); inflate.zero.setOnClickListener (this); inflate.yi.setOnClickListener (this); inflate.er.setOnClickListener (this) Inflate.san.setOnClickListener (this); inflate.si.setOnClickListener (this); inflate.wu.setOnClickListener (this); inflate.liu.setOnClickListener (this); inflate.qi.setOnClickListener (this); inflate.ba.setOnClickListener (this); inflate.jiu.setOnClickListener (this); inflate.add.setOnClickListener (this); inflate.sub.setOnClickListener (this); inflate.mul.setOnClickListener (this) Inflate.div.setOnClickListener (this); inflate.point.setOnClickListener (this); inflate.equal.setOnClickListener (this); inflate.dec.setOnClickListener (this); inflate.ac.setOnClickListener (this);} private void jisuanqi () {List strs=new ArrayList (); StringBuilder f = new StringBuilder (); for (int iTun0) I1) {tv= tv.substring (0, tv.length ()-1);} else {tv= "0";} jisuanqi (); break Case R.id.equal: if (! String.valueOf (tv.charAt (tv.length ()-1)). Matches ("(\ d +)") {tv= tv.substring (0, tv.length ()-1);} count=tv; jisuanqi (); tv=count Inflate.tv.setText (tv); break;} Thank you for reading this article carefully. I hope the article "how to implement a simple Calculator in Android" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Article calculator code operation addition subtraction multiplication and division four operations value location interest content function at the same time four layout more friends knowledge editing industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn macOS Shulou Information Linux