利用Android stdio实现较复杂计算器

Java代码

package com.gb;import androidx.appcompat.app.AppCompatActivity;import android.content.res.Configuration;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import org.javia.arity.Symbols;
import org.javia.arity.SyntaxException;import java.math.BigDecimal;public class MainActivity extends AppCompatActivity implements View.OnClickListener {Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;Button btn_mul,btn_div,btn_add,btn_sub,btn_yu;Button btn_clr,btn_del,btn_eq;EditText et_input;Button sin,tan,cos,lg,ln,sqrt,square,cubic,factorial;Button twoND,eightND,symbol,leftk,rightk,reciprocal,Exit;boolean clr_flag;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);EditText edit=(EditText)findViewById(R.id.et_input);edit.setInputType(InputType.TYPE_NULL);Configuration config = getResources().getConfiguration();if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){sin= (Button) findViewById(R.id.sin);cos= (Button) findViewById(R.id.cos);tan= (Button) findViewById(R.id.tan);lg= (Button) findViewById(R.id.lg);ln= (Button) findViewById(R.id.ln);sqrt= (Button) findViewById(R.id.sqrt);//开根号square= (Button) findViewById(R.id.square);//平方cubic= (Button) findViewById(R.id.cubic);//立方factorial= (Button) findViewById(R.id.factorial);//阶乘twoND= (Button) findViewById(R.id.twoND);eightND= (Button) findViewById(R.id.eightND);symbol= (Button) findViewById(R.id.H);//正负号leftk= (Button) findViewById(R.id.leftk);rightk= (Button) findViewById(R.id.rightk);reciprocal= (Button) findViewById(R.id.reciprocal); //倒数}btn_0= (Button) findViewById(R.id.btn_0);btn_1= (Button) findViewById(R.id.btn_1);btn_2= (Button) findViewById(R.id.btn_2);btn_3= (Button) findViewById(R.id.btn_3);btn_4= (Button) findViewById(R.id.btn_4);btn_5= (Button) findViewById(R.id.btn_5);btn_6= (Button) findViewById(R.id.btn_6);btn_7= (Button) findViewById(R.id.btn_7);btn_8= (Button) findViewById(R.id.btn_8);btn_9= (Button) findViewById(R.id.btn_9);btn_pt= (Button) findViewById(R.id.btn_pt);btn_add= (Button) findViewById(R.id.btn_add);btn_sub= (Button) findViewById(R.id.btn_sub);btn_mul= (Button) findViewById(R.id.btn_mul);btn_div= (Button) findViewById(R.id.btn_div);btn_yu= (Button) findViewById(R.id.btn_yu);btn_clr= (Button) findViewById(R.id.btn_clr);btn_del= (Button) findViewById(R.id.btn_del);btn_eq= (Button) findViewById(R.id.btn_eq);et_input= (EditText) findViewById(R.id.et_input);Exit= (Button) findViewById(R.id.Exit);//退出程序}public boolean onKeyDown(int keyCode, KeyEvent event) {TextView textView1 = findViewById(R.id.et_input);//获取竖屏时结果区BigDecimal output1 = new BigDecimal(textView1.getText().toString());int bitPos1 = textView1.getText().toString().indexOf(".");int i1 = (int) (textView1.getText().toString().length() - bitPos1 - 1);//获取小数点后有多少位switch (keyCode) {// 音量减小case KeyEvent.KEYCODE_VOLUME_DOWN:String test1 = output1.setScale(i1 - 1, BigDecimal.ROUND_HALF_UP).toString();//设置小数点位数为i-1位textView1.setText(test1);return true;case KeyEvent.KEYCODE_VOLUME_UP:String test3 = output1.setScale(i1 + 1, BigDecimal.ROUND_HALF_UP).toString();//设置小数点位数为i-1位textView1.setText(test3);return true;}return super.onKeyDown(keyCode, event);}//音量调整小数位数public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.caidan, menu);return true;}//菜单@Overridepublic void onClick(View v) {String str = et_input.getText().toString();Configuration config = getResources().getConfiguration();if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){Log.i("info", "landscape"); // 横屏}  else if(config.orientation == Configuration.ORIENTATION_PORTRAIT){Log.i("info", "portrait"); // 竖屏}switch (v.getId()) {case R.id.btn_0:case R.id.btn_1:case R.id.btn_2:case R.id.btn_3:case R.id.btn_4:case R.id.btn_5:case R.id.btn_6:case R.id.btn_7:case R.id.btn_8:case R.id.btn_9:case R.id.btn_pt:case R.id.rightk:case R.id.leftk:case R.id.sqrt:et_input.setText(str + ((Button) v).getText());break;case R.id.sin:case R.id.cos:case R.id.tan:case R.id.lg:case R.id.ln:et_input.setText(str +((Button) v).getText()+"(");break;case R.id.twoND:int str1= Integer.valueOf(str);et_input.setText(Integer.toBinaryString(str1));break;case R.id.eightND:int str2= Integer.valueOf(str);et_input.setText(Integer.toOctalString(str2));break;case R.id.H:int str3= Integer.valueOf(str);et_input.setText(Integer.toHexString(str3));break;case R.id.reciprocal:String s4="1/";et_input.setText(s4+str);break;case R.id.factorial:et_input.setText(str +"!");break;case R.id.square:String s1="^2";et_input.setText(str +s1);break;case R.id.cubic:String s3="^3";et_input.setText(str +s3);break;case R.id.btn_yu:case R.id.btn_add:case R.id.btn_sub:case R.id.btn_mul:case R.id.btn_div:et_input.setText(str +((Button) v).getText());break;case R.id.btn_clr:if (clr_flag)clr_flag = false;str = "";et_input.setText("");break;case R.id.btn_del: //判断是否为空,然后在进行删除if (clr_flag) {clr_flag = false;str = "";et_input.setText("");} else if (str != null && !str.equals("")) {et_input.setText(str.substring(0, str.length() - 1));}break;case R.id.btn_eq: //单独运算最后结果
//                getResult();//调用下面的方法
//                break;String strContent = et_input.getText().toString();try {Symbols s = new Symbols();double res = s.eval(strContent);String res1=String.valueOf(res);int bitPos1 = res1.indexOf(".");int i1 = (int) (res1.length() - bitPos1 - 1);//获取小数点后有多少位if(i1>=13){et_input.setText(res1.substring(0, res1.length() - 4));}else if(res1 == "Infinity"){et_input.setText("分母不能为0!!");}else{et_input.setText(String.valueOf(res));}} catch (SyntaxException e) {Toast.makeText(MainActivity.this, "错误!", Toast.LENGTH_SHORT).show();}break;case R.id.Exit:System.exit(0);break;}}}

如下

竖屏布局代码如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"tools:ignore="MissingDefaultResource"><EditTextandroid:id="@+id/et_input"android:layout_width="match_parent"android:layout_height="60dp"android:layout_marginTop="50dp"android:gravity="right"android:paddingRight="5dp"android:paddingBottom="5dp"android:textSize="30sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_clr"android:layout_width="wrap_content"android:layout_height="80dp"android:backgroundTint="#FF0000"android:onClick="onClick"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="C"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_del"android:layout_width="78dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:backgroundTint="#FF0000"android:onClick="onClick"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="de"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_div"android:layout_width="85dp"android:layout_height="80dp"android:text="÷"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:onClick="onClick"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/Exit"android:layout_width="85dp"android:layout_height="80dp"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="->"android:onClick="onClick"android:backgroundTint="#FF0000"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_1"android:layout_width="85dp"android:layout_height="80dp"android:text="1"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"android:onClick="onClick"/><Buttonandroid:id="@+id/btn_2"android:layout_width="85dp"android:layout_height="80dp"android:text="2"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:onClick="onClick"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_3"android:layout_width="85dp"android:layout_height="80dp"android:text="3"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:onClick="onClick"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_mul"android:layout_width="85dp"android:layout_height="80dp"android:text="×"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:onClick="onClick"android:backgroundTint="#FF0000"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_4"android:layout_width="85dp"android:layout_height="80dp"android:text="4"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"android:onClick="onClick"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_5"android:layout_width="85dp"android:layout_height="80dp"android:text="5"android:textSize="30sp"android:onClick="onClick"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_6"android:layout_width="85dp"android:layout_height="80dp"android:text="6"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:onClick="onClick"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_sub"android:layout_width="85dp"android:layout_height="80dp"android:text="-"android:textSize="30sp"android:onClick="onClick"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"android:gravity="center_horizontal"><Buttonandroid:layout_width="85dp"android:layout_height="80dp"android:id="@+id/btn_7"android:text="7"android:onClick="onClick"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/><Buttonandroid:layout_width="85dp"android:layout_height="80dp"android:id="@+id/btn_8"android:text="8"android:onClick="onClick"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/><Buttonandroid:layout_width="85dp"android:layout_height="80dp"android:id="@+id/btn_9"android:text="9"android:onClick="onClick"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_add"android:layout_width="85dp"android:layout_height="80dp"android:text="+"android:onClick="onClick"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_0"android:layout_width="wrap_content"android:layout_height="80dp"android:backgroundTint="#FF0000"android:onClick="onClick"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="0"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_yu"android:layout_width="85dp"android:layout_height="80dp"android:text="%"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"android:onClick="onClick"/><Buttonandroid:layout_width="85dp"android:layout_height="80dp"android:id="@+id/btn_pt"android:text="."android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:onClick="onClick"android:backgroundTint="#FF0000"/><Buttonandroid:id="@+id/btn_eq"android:layout_width="85dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:text="="android:onClick="onClick"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"android:backgroundTint="#FF0000"/></LinearLayout>
</LinearLayout>

剩下代码明天写。

安卓实现横竖计算器(一)相关推荐

  1. android dpi计算器,安卓多功能计算器 One++ Calculator 1.7.5 中文多语免费版

    安卓多功能计算器 One++ Calculator 中文解锁版由大眼仔~旭(www.dayanzai.me)发布.One++ Calculator 不是普通的计算器,而是数字公式计算的集合.现在市面上 ...

  2. android角度计算器,Calckit高级版一款安卓多功能计算器,内置高度定制的科学计算器...

    前言 手机中的计算器功能你多久用一次呢,我相信对于多数人来说计算器是一个低频使用软件. 其原因可能是用不上计算功能也很可能是单一的计算功能并不能满足你对于计算的需求. 那么如果现在有一款可以完成中学到 ...

  3. 苹果和安卓手机的计算器,为什么1÷3×3的结果不等于1?

    这样的问题在前几年的时候就有所讨论,那个时候都是在纠结苹果手机还是安卓手机的计算器更好用,于是大家在无意当中发现了好多的bug,其中就有这个. 当初我看到的时候也是非常的不解,因为这太让人失望了.这属 ...

  4. java编写安卓计算器_安卓实现简单计算器

    实现一个计算器 ,有加减乘除功能,小数点和清除操作. 这是学校安卓老师布置的作业,计算器说实话实现起来挺多坑的,之前在算法比赛中见过这种题,用来熟悉安卓的布局的确是挺好的一个小案例,不过需要挺多逻辑处 ...

  5. 自制安卓版iPhone计算器

    新手初学安卓一周,勿喷. 在看完Activity之后决定做一个计算器.根据网上的资料和慕课网计算器的教材,仿照着做了一个iPone计算器. 先看看效果图 好了,直接给源码,XML用的是LinearLa ...

  6. Android计算器输入错误纠正,减法都算错?安卓惊爆计算器低级错误Bug

    [IT168 资讯]最近小编我在网上闲逛的时候发现有很多用户都在反映一个非常惊悚的问题:使用Android手机内置的计算器时有些最简单的减法都会算错,例如14.52-14.49,再例如8.03-7.9 ...

  7. 安卓AndroidStudio实现计算器

    一.内容: 基于AndroidStudio,实现一个简易的计算器(界面+简单的计算). 下图是整体界面: 二.思路 首先设计界面,activity_main.xml 计算器界面,需要:文本框TextV ...

  8. android实现计算器功能吗,安卓实现一个计算器的功能

    新建项目工程 第一步 布局的设置 android:layout_width="368dp" android:layout_height="495dp" andr ...

  9. 安卓开发 科学计算器

    说明 代码逻辑部分是copy的,为了应付课设,就自己弄个布局,本人小白啥也不懂,参考 链接链接地址 运行结果 activity_main.xml <?xml version="1.0& ...

最新文章

  1. Java项目:个人博客系统(前后端分离+java+vue+Springboot+ssm+mysql+maven+redis)
  2. 远程实习 | 达特茅斯学院招收网络嵌入和图挖掘方向研究型实习生
  3. javacc的源码构建
  4. 有关子数组最大累加和的算法小结
  5. Windows上卸载SqlServer数据库
  6. 【PAT甲级 进制转换】1019 General Palindromic Number (20 分) Java版 7/7通过
  7. 买iPhone 11的要不再等等?iPhone 12首曝:全系5G,回归经典造型
  8. MVC开发中的常见错误-02-在应用程序配置文件中找不到名为“OAEntities”的连接字符串。...
  9. 若依如何解决请求地址存在中文出现异常?
  10. 2017IEC计算机第二次作业
  11. 使用 matlab 数字图像处理(三)—— 实现图像的旋转(不使用 imrotate)
  12. 红包活动竟藏着这么多玩法(附使用技巧)
  13. kafka no record information is available
  14. etc2 纹理压缩_ETC纹理的紧缩压缩
  15. 洛特卡-沃尔泰拉模型(Lotka-Volterra model)
  16. Hbase——常见错误
  17. .stl,.obj 转换为.stp/.step,.x_t
  18. [Practical.Vim(2012.9)].Drew.Neil.Tip19学习摘要
  19. java script基础入门·2
  20. 出色项目经理技能 ——人际交往技能

热门文章

  1. cas 6 单点登录登出管理
  2. wsyoneself的一周年创作纪念日
  3. 计算平均成绩(函数专题),输入某位同学各门课的成绩,输出平均成绩。输入的成绩均为五级制成绩,五级制成绩转换为百分之成绩的规则如下:'A'换为百分之成绩为95分,'B'对应85分,C对应75分,'D'
  4. 【零声教育】C/C++Linux服务器开发/高级架构师 课程
  5. 阿里云开源业界首个面向NLP场景深度迁移学习框架
  6. Matlab微分方程的求解
  7. 股价大跌时如何抄底?
  8. JAVA程序修改PDF内容,使用Java和Itext编辑PDF文本
  9. GitHub Universe 直播 | Enterprise Day 1 日程
  10. FCU-HiL测试介绍