androidstudio计算机代码,AndroidStudio实现简易计算器

androidstudio计算机代码,AndroidStudio实现简易计算器

2023年7月7日发(作者:)

androidstudio计算机代码,AndroidStudio实现简易计算器⽬的根据书本例题的计算器界⾯,开发⼀个简单的计算器,并实现功能。⼯具及环境使⽤java语⾔,在Android studio平台上进⾏开发功能设计实现加减乘除的运算功能和清屏的功能设计思路⾸先设计⼀个可视化的界⾯,供⽤户输⼊数据并查看结果。⽤户通过点击相应按钮输⼊正确的表达式,计算器进⾏相应的加减乘除运算,且可以进⾏⼩数和整数的运算代码selector_orange_(点击按钮会有灰⾊阴影效果)selector_white_ (点击按钮会有灰⾊阴影效果)shape_rectangle_ (运算符功能键的背景颜⾊设置)shape_rectangle_orange_ (点击按钮之后的灰⾊阴影设置)shape_rectangle_pe_rectangle_white_ivity_(UI设计)界⾯效果图:ckage ator;import patActivity;import ;import ;import ;import xt;public class MainActivity extends AppCompatActivity implements kListener {//创建Button对象 也就是activity_⾥所设置的IDButton zero,one,two,three,four,five,six,seven,eight,nine,spot;Button mul,div,plus,min;Button cls,equal;EditText result;boolean clr_flag; //判断et编辑⽂本框中是否清空@Overrideprotected void onCreate(Bundle savedInstanceState) {te(savedInstanceState);setContentView(ty_main);//实例化对象setContentView(ty_main);zero= (Button) findViewById();one= (Button) findViewById();two= (Button) findViewById();three= (Button) findViewById();four= (Button) findViewById();five= (Button) findViewById();six= (Button) findViewById();seven= (Button) findViewById();eight= (Button) findViewById();nine= (Button) findViewById();spot= (Button) findViewById();plus= (Button) findViewById();min= (Button) findViewById();mul= (Button) findViewById();div= (Button) findViewById();cls= (Button) findViewById();equal= (Button) findViewById();result= (EditText) findViewById();//给按钮设置点击事件lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);lickListener(this);}@Overridepublic void onClick(View v) {String str=t().toString();//获取⽂本框中的⽂本内容switch (()){case :case :case :case :case :case :case :case :case :case :case :if(clr_flag){clr_flag=false;str="";t("");}t(str+((Button)v).getText());break;case :case :case :case :if(clr_flag){clr_flag=false;str="";t("");}if(ns("+")||ns("-")||ns("×")||ns("÷")) {str=ing(0,f(" "));}t(str+" "+((Button)v).getText()+" ");break;case :if(clr_flag)clr_flag=false;str="";t("");break;case : //单独运算最后结果getResult();//调⽤下⾯的⽅法break;}}private void getResult() {String exp=t().toString();if(exp==null||("")) return ;//因为没有运算符所以不⽤运算if(!ns(" ")){return ;}if(clr_flag){clr_flag=false;return;}clr_flag=true;//截取运算符前⾯的字符串String s1=ing(0,f(" "));//截取的运算符String op=ing(f(" ")+1,f(" ")+2);//截取运算符后⾯的字符串String s2=ing(f(" ")+3);double cnt=0;if(!("")&&!("")){double d1=ouble(s1);double d2=ouble(s2);if(("+")){cnt=d1+d2;}if(("-")){cnt=d1-d2;}if(("×")){cnt=d1*d2;}if(("÷")){if(d2==0) cnt=0;else cnt=d1/d2;}if(!ns(".")&&!ns(".")&&!("÷")) {int res = (int) cnt;t(res+"");}else {t(cnt+"");}}//如果s1不是空 s2是空 就执⾏下⼀步else if(!("")&&("")){double d1=ouble(s1);if(("+")){cnt=d1;}if(("-")){cnt=d1;}if(("×")){cnt=0;}if(("÷")){cnt=0;}if(!ns(".")) {int res = (int) cnt;t(res+"");}else {t(cnt+"");}}//如果s1是空 s2不是空 就执⾏下⼀步else if(("")&&!("")){double d2=ouble(s2);if(("+")){cnt=d2;}if(("-")){cnt=0-d2;}if(("×")){cnt=0;}if(("÷")){cnt=0;}if(!ns(".")) {int res = (int) cnt;t(res+"");}else {t(cnt+"");}}else {t("");}}}功能实现效果:

总结这次的计算器实验收获很多,在操作和学习的过程中,结合了书本,同学和⽹上视频的知识,⼀边操作⼀边加深着Android Studio的界⾯设置命令和效果,以及功能的实现加强了java语⾔的基础,是很好的⼀次学习实践。当然中间会有很多的问题,⽐如说减法不知道为什么实现不了运算,看了很多遍也找到究竟什么问题,慢慢改着改着突然就成功了。java语⾔的学习,还要继续加强,移动编程技术,也要好好的学,好好的练。没有分组,⼀个⼈,所以要更加的努⼒,提⾼⾃⼰的⽔平,跟上⽼师的进度,努⼒完成每⼀次的任务,只有⾃⼰真真正正的去努⼒了,才会有收获,才会成功。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688675591a161696.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信