本文实例为大家分享了android studio实现简易计算器app的具体代码,供大家参考,具体内容如下
效果演示
布局文件
java文件
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; } //加减乘除、求余简单运算方法 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不能为除数",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; } //数字按钮事件 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); } //回退按钮事件 public void backsprce(view view) { if (viewstr.length() == 0) return; index = viewstr.length(); viewstr.deletecharat(--index); tv2.settext(viewstr); } //符号改变按钮事件 public void change(view view) { string alls = viewstr.tostring(); string[] asmd = {"+","-","×","÷","%"}; int inde = 0; string a = null; //保存字符串中运算符部分 string b = null; //保存字符串中数字部分 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); } //加减乘除按钮事件 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) <= '9') { tv1.settext(calc(viewstr)); string a = calc(viewstr); viewstr = temp.append(a).append('+'); tv2.settext(viewstr); index = viewstr.length(); return; } } char a = viewstr.charat(index-1); if (a == '+') { return; } if (a == '-' || a == '×' || a == '÷' || a == '%') { viewstr.setcharat(index-1,'+'); }else { viewstr.append("+"); index ; } tv2.settext(viewstr); } public void subtraction(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) <= '9') { tv1.settext(calc(viewstr)); string a = calc(viewstr); viewstr = temp.append(a).append('-'); tv2.settext(viewstr); index = viewstr.length(); return; } } char a = viewstr.charat(index-1); if (a == '-') { return; } if (a == '+' || a == '×' || a == '÷' || a == '%') { viewstr.setcharat(index-1,'-'); }else { viewstr.append("-"); index ; } tv2.settext(viewstr); } public void multiplication(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) <= '9') { tv1.settext(calc(viewstr)); string a = calc(viewstr); viewstr = temp.append(a).append('×'); tv2.settext(viewstr); index = viewstr.length(); return; } } char a = viewstr.charat(index-1); if (a == '×') { return; } if (a == '+' || a == '-' || a == '÷' || a == '%') { viewstr.setcharat(index-1,'×'); }else { viewstr.append("×"); index ; } tv2.settext(viewstr); } public void division(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) <= '9') { tv1.settext(calc(viewstr)); string a = calc(viewstr); viewstr = temp.append(a).append('÷'); tv2.settext(viewstr); index = viewstr.length(); return; } } char a = viewstr.charat(index-1); if (a == '÷') { return; } if (a == '+' || a == '-' || a == '×' || a == '%') { viewstr.setcharat(index-1,'÷'); }else { viewstr.append("÷"); index ; } tv2.settext(viewstr); } //求余按钮事件 public void surplus(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) <= '9') { tv1.settext(calc(viewstr)); string a = calc(viewstr); viewstr = temp.append(a).append('%'); tv2.settext(viewstr); index = viewstr.length(); return; } } char a = viewstr.charat(index-1); if (a == '%') { return; } if (a == '+' || a == '-' || a == '×' || a == '÷') { viewstr.setcharat(index-1,'%'); }else { viewstr.append("%"); index ; } tv2.settext(viewstr); } //求倒数按钮事件 public void reciprocal(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()); if (a == 0) { toast.maketext(this,"0不能为除数",toast.length_short).show(); return; } clearc(view); viewstr.append(1/a); index = string.valueof(1/a).length(); tv2.settext(viewstr); } //平方按钮事件 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(a*a); index = string.valueof(a*a).length(); tv2.settext(viewstr); } //开平方按钮事件 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。