Android开发实现的简单计算器功能【附完整demo源码下载】
程序员文章站
2022-04-30 12:28:22
本文实例讲述了android开发实现的简单计算器功能。分享给大家供大家参考,具体如下:
这个android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦;...
本文实例讲述了android开发实现的简单计算器功能。分享给大家供大家参考,具体如下:
这个android计算器虽然还有点小bug,不过简单的计算功能还是没问题的哦;
先上图看效果
比较简单,所以我就没怎么写注释,应该一看就能明白的
有不明白的可以发信问我
先贴mainactivity.java代码
package com.example.calculator; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bp, bs, bm, bd, bc, be; imageview delete; textview tv; edittext show; string showstring = "", option = ""; int showfirst = 0; string exception = ""; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); b0 = (button) findviewbyid(r.id.bt_0); b1 = (button) findviewbyid(r.id.bt_1); b2 = (button) findviewbyid(r.id.bt_2); b3 = (button) findviewbyid(r.id.bt_3); b4 = (button) findviewbyid(r.id.bt_4); b5 = (button) findviewbyid(r.id.bt_5); b6 = (button) findviewbyid(r.id.bt_6); b7 = (button) findviewbyid(r.id.bt_7); b8 = (button) findviewbyid(r.id.bt_8); b9 = (button) findviewbyid(r.id.bt_9); bp = (button) findviewbyid(r.id.bt_plus); bs = (button) findviewbyid(r.id.bt_sub); bm = (button) findviewbyid(r.id.bt_mutilate); bd = (button) findviewbyid(r.id.bt_div); bc = (button) findviewbyid(r.id.bt_c); be = (button) findviewbyid(r.id.bt_equ); b1.setonclicklistener(this); b2.setonclicklistener(this); b3.setonclicklistener(this); b4.setonclicklistener(this); b5.setonclicklistener(this); b6.setonclicklistener(this); b7.setonclicklistener(this); b8.setonclicklistener(this); b9.setonclicklistener(this); b0.setonclicklistener(this); bp.setonclicklistener(this); bs.setonclicklistener(this); bm.setonclicklistener(this); bd.setonclicklistener(this); bc.setonclicklistener(this); be.setonclicklistener(this); show = (edittext) findviewbyid(r.id.et_show); delete = (imageview) findviewbyid(r.id.iv_delete); delete.setonclicklistener(this); tv=(textview) findviewbyid(r.id.author); tv.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. the action bar will // automatically handle clicks on the home/up button, so long // as you specify a parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onclick(view v) { switch (v.getid()) { case r.id.bt_0: showstring += "0"; break; case r.id.bt_1: showstring += "1"; break; case r.id.bt_2: showstring += "2"; break; case r.id.bt_3: showstring += "3"; break; case r.id.bt_4: showstring += "4"; break; case r.id.bt_5: showstring += "5"; break; case r.id.bt_6: showstring += "6"; break; case r.id.bt_7: showstring += "7"; break; case r.id.bt_8: showstring += "8"; break; case r.id.bt_9: showstring += "9"; break; case r.id.bt_plus: if (showstring.equals("")) exception = "先输入数值哦"; else { showfirst = integer.parseint(showstring); showstring = ""; option = "+"; } break; case r.id.bt_sub: if (showstring.equals("")) exception = "先输入数值哦"; else { showfirst = integer.parseint(showstring); showstring = ""; option = "-"; } break; case r.id.bt_mutilate: if (showstring.equals("")) exception = "先输入数值哦"; else { showfirst = integer.parseint(showstring); showstring = ""; option = "*"; } break; case r.id.bt_div: if (showstring.equals("")) exception = "先输入数值哦"; else { showfirst = integer.parseint(showstring); showstring = ""; option = "/"; } break; case r.id.bt_equ: if (option.equals("+")) showstring = showfirst + integer.parseint(showstring) + ""; else if (option.equals("-")) { showstring = showfirst - integer.parseint(showstring) + ""; } else if (option.equals("*")) { showstring = showfirst * integer.parseint(showstring) + ""; } else if (option.equals("/")) { if (showstring.equals("0")) { exception = "除数不能为0!"; } else showstring = showfirst / integer.parseint(showstring) + ""; } break; case r.id.bt_c: showstring = ""; break; case r.id.iv_delete: toast.maketext(mainactivity.this, showstring + "已被清空", toast.length_short).show(); showstring = ""; break; case r.id.author: toast.maketext(mainactivity.this, "郑明亮\n软件工程\nqq:1072307340", toast.length_short).show(); break; default: break; } if (exception.equals("")) show.settext(showstring); else { show.settext(exception); exception = ""; } // 设置文本框颜色; if (!show.gettext().tostring().equals("")) { delete.setbackgroundcolor(r.drawable.delete_gray); } else { delete.setbackgroundresource(r.drawable.delete); } } }
再贴布局activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.calculator.mainactivity" > <relativelayout android:layout_width="match_parent" android:layout_height="wrap_content" > <edittext android:id="@+id/et_show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入数字" /> <imageview android:id="@+id/iv_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@id/et_show" android:src="@drawable/delete_and_deletegray" > </imageview> </relativelayout> <gridlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_show" android:focusable="false" android:gravity="center" android:layout_margintop="25dp" android:columncount="4" android:horizontalspacing="0dp" android:orientation="horizontal" android:stretchmode="none" > <button android:id="@+id/bt_1" android:layout_height="wrap_content" android:text="1" /> <button android:id="@+id/bt_2" android:text="2" /> <button android:id="@+id/bt_3" android:text="3" /> <button android:id="@+id/bt_div" android:text="/" /> <button android:id="@+id/bt_4" android:text="4" /> <button android:id="@+id/bt_5" android:text="5" /> <button android:id="@+id/bt_6" android:text="6" /> <button android:id="@+id/bt_mutilate" android:text="x" /> <button android:id="@+id/bt_7" android:text="7" /> <button android:id="@+id/bt_8" android:text="8" /> <button android:id="@+id/bt_9" android:text="9" /> <button android:id="@+id/bt_sub" android:text="-" /> <button android:id="@+id/bt_0" android:layout_columnspan="2" android:layout_gravity="fill_horizontal" android:text="0" android:width="2dp" /> <button android:id="@+id/bt_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="c" /> <button android:id="@+id/bt_plus" android:layout_gravity="fill_vertical" android:layout_rowspan="2" android:text="+" /> <button android:id="@+id/bt_equ" android:layout_columnspan="3" android:layout_gravity="fill_horizontal" android:text="=" /> </gridlayout> <textview android:id="@+id/author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="author:bri" /> <textview android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test:www.jb51.net" /> </linearlayout>
我还写了一个drawable的xml,自己看吧
delete_and_deletegray.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/delete_gray" android:state_focused="false" android:state_pressed="false"></item> <item android:drawable="@drawable/delete" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/> <item android:drawable="@drawable/delete" android:state_pressed="true" android:state_selected="false"/> <item android:drawable="@drawable/delete" android:state_focused="true" android:state_pressed="true"/> </selector>
附:完整实例代码点击此处本站下载。
ps:这里再为大家推荐几款计算工具供大家进一步参考借鉴:
在线一元函数(方程)求解计算工具:
科学计算器在线使用_高级计算器在线计算:
在线计算器_标准计算器:
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: C# 控制台定时器
推荐阅读
-
Android开发实现实时检测蓝牙连接状态的方法【附源码下载】
-
Android开发实现可拖动排序的ListView功能【附源码下载】
-
C#实现的Windows剪贴板监视器功能实例【附demo源码下载】
-
Android开发之自定义view实现通讯录列表A~Z字母提示效果【附demo源码下载】
-
Android开发之HttpClient异步请求数据的方法详解【附demo源码下载】
-
Android开发实现的简单计算器功能【附完整demo源码下载】
-
Android编程调用系统自带的拍照功能并返回JPG文件示例【附demo源码下载】
-
微信小程序实现点击按钮移动view标签的位置功能示例【附demo源码下载】
-
Android编程实现的微信支付功能详解【附Demo源码下载】
-
JS实现的简单拖拽购物车功能示例【附源码下载】