In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize the simple calculator App by Android Studio". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Android Studio how to achieve a simple calculator App" bar!
Effect demonstration
Layout file
Java file
Import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;import androidx.annotation.Nullable;public class MainActivity extends Activity {private TextView tv1; private TextView tv2; private StringBuilder viewStr; private int index = 0; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main_calc) Tv1 = (TextView) findViewById (R.id.mini); tv2 = (TextView) findViewById (R.id.max); viewStr = new StringBuilder (); index = 0;} / / simple operation method public String calc (StringBuilder strB) {String allS = strB.toString (); String [] asmd = {"+", "-", "×", "/", "%"} Int x = 0; int y = 0; String result = null; for (int I = 0; I
< 5; i ++) { int inde = strB.indexOf(asmd[i]); if (inde >0) {String a = allS.split (asmd [I]) [0]; String b = allS.split (asmd [I]) [1]; if (I = = 0) {result = String.valueOf (Integer.parseInt (a) + Integer.parseInt (b));} if (I = = 1) {result = String.valueOf (Integer.parseInt (a)-Integer.parseInt (b)) } if (I = = 2) {result = String.valueOf (Integer.parseInt (a) * Integer.parseInt (b));} if (I = = 3) {if (Integer.parseInt (b) = = 0) {Toast.makeText (this, "0 cannot be a divisor", Toast.LENGTH_SHORT). Show () Result = String.valueOf (Integer.parseInt (a));} else {result = String.valueOf (Integer.parseInt (a) / Integer.parseInt (b));}} if (I = = 4) {result = String.valueOf (Integer.parseInt (a)% Integer.parseInt (b)) } return result;} / / numeric button event public void number_0 (View view) {viewStr.append ("0"); index + +; tv2.setText (viewStr);} public void number_1 (View view) {viewStr.append ("1"); index + +; tv2.setText (viewStr);} public void number_2 (View view) {viewStr.append ("2"); index + + Tv2.setText (viewStr);} public void number_3 (View view) {viewStr.append ("3"); index + +; tv2.setText (viewStr);} public void number_4 (View view) {viewStr.append ("4"); index + +; tv2.setText (viewStr);} public void number_5 (View view) {viewStr.append ("5"); index + +; tv2.setText (viewStr) } public void number_6 (View view) {viewStr.append ("6"); index + +; tv2.setText (viewStr);} public void number_7 (View view) {viewStr.append ("7"); index + +; tv2.setText (viewStr);} public void number_8 (View view) {viewStr.append ("8"); index + +; tv2.setText (viewStr);} public void number_9 (View view) {viewStr.append ("9"); index + + Tv2.setText (viewStr);} / / fallback button event public void backsprce (View view) {if (viewStr.length () = = 0) return; index = viewStr.length (); viewStr.deleteCharAt (--index); tv2.setText (viewStr);} / / symbol change button event public void change (View view) {String allS = viewStr.toString () String [] asmd = {"+", "-", "×", "/", "%"}; int inde = 0; String a = null; / / saves the operator part of the string String b = null; / / saves the numeric part of the string for (int I = 0; I)
< 5; i ++) { inde = viewStr.indexOf(asmd[i]); if (inde != -1) { a = asmd[i]; break; } } //A 字符形 改变 A 正负值 if (inde == -1) { clearc(view); int c = - Integer.parseInt(allS); viewStr.append(String.valueOf(c)); index = String.valueOf(c).length(); tv2.setText(viewStr); } //A + 字符串形 改变 A 正负值 暂时无法实现,待后续优化 if (inde == index - 1) { return;// clearc(view);// int c = - Integer.valueOf(allS.split(a)[0]);// viewStr.append(String.valueOf(c));// index = String.valueOf(c).length();// tv2.setText(viewStr); } //A + B 字符串形 改变 B 正负值 if (inde >= 0 & & inde
< index) { clearc(view); b = allS.split(a)[0]; int c = - Integer.parseInt(allS.split(a)[1]); viewStr.append(b).append(a).append(String.valueOf(c)); index = String.valueOf(c).length(); tv2.setText(viewStr); } } //清空按钮事件 public void clearc(View view) { StringBuilder temp = new StringBuilder(); viewStr = temp.append(""); tv2.setText(viewStr); index = 0; } public void clearce(View view) { StringBuilder temp = new StringBuilder(); viewStr = temp.append(""); tv1.setText(""); tv2.setText(viewStr); index = 0; } //等于按钮事件 public void equal(View view) { String[] asmd = {"+","-","×","÷","%"}; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde >0 & & inde! = index-1) {tv1.setText (calc (viewStr)); String a = calc (viewStr); viewStr = temp.append (a); tv2.setText (viewStr); index = viewStr.length (); return } else if (inde > 0 & & inde = = index-1) {viewStr.deleteCharAt (--index); tv1.setText (viewStr); tv2.setText (viewStr);}} tv1.setText (viewStr); tv2.setText (viewStr) Add, subtract, multiply and divide button event public void addition (View view) {if (viewStr.length () = 0) return; String [] asmd = {"+", "-", "×", "/", "%"}; StringBuilder temp = new StringBuilder (); for (int I = 0; I)
< 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde >0 & & viewStr.charAt (index-1) > ='0' & viewStr.charAt (index-1) 0 & & viewStr.charAt (index-1) > ='0' & & viewStr.charAt (index-1) 0 & & viewStr.charAt (index-1) > ='0' & & viewStr.charAt (index-1) 0 & viewStr.charAt (index-1) > ='0' & & viewStr.charAt (index-1) 0 & & viewStr.charAt (index-1) > = '0' & & viewStr.charAt (index-1)-1) {return }} int a = Integer.parseInt (viewStr.toString (). Trim ()); if (a = = 0) {Toast.makeText (this, "0 cannot not be a divisor", Toast.LENGTH_SHORT). Show (); return;} clearc (view); viewStr.append (1gamma); index = String.valueOf (1gamma). Length () Tv2.setText (viewStr);} / Square Button event public void square (View view) {if (viewStr.length () = = 0) return; String [] asmd = {"+", "-", "×", "/", "%"}; for (int I = 0; I
< 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde >-1) {return;}} int a = Integer.parseInt (viewStr.toString (). Trim ()); clearc (view); viewStr.append (Apoca); index = String.valueOf (Ayoga). Length (); tv2.setText (viewStr);} / squared button event public void squareroot (View view) {if (viewStr.length () = = 0) return String [] asmd = {"+", "-", "×", "/", "%"}; for (int I = 0; I
< 5; i ++) { int inde = viewStr.indexOf(asmd[i]); if (inde >-1) {return;}} int a = Integer.parseInt (viewStr.toString (). Trim ()); clearc (view); viewStr.append ((int) Math.sqrt (a)); index = String.valueOf ((int) Math.sqrt (a)). Length (); tv2.setText (viewStr) }} at this point, I believe you have a deeper understanding of "Android Studio how to implement the simple calculator App". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.