Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Calculator with Android

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report