温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android Studio怎么实现简单计算器APP

发布时间:2021-04-17 12:28:34 来源:亿速云 阅读:252 作者:小新 栏目:移动开发

这篇文章主要介绍Android Studio怎么实现简单计算器APP,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一、简介:用Android Studio 实现一个简单的计算器APP,并在蓝叠模拟器中运行。

该计算器只能实现两位数字的四则运算。

二、代码

activity_main.xml   ---界面设计

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:layout_below="@+id/textView"
 android:layout_alignParentStart="true"
 android:rowCount="6"
 android:columnCount="4"
 > <!--设置网格为6行4列-->
 <!--显示文本组件,占1行4列-->
 <TextView
 android:id="@+id/text"
 android:layout_width="350dp"
 android:layout_height="wrap_content"
 android:layout_columnSpan="4"
 android:layout_marginLeft="4px"
 android:gravity="left"
 android:textSize="50dp" />
 
 <!--清除按钮,占1行4列-->
 <Button
 android:id="@+id/btnClear"
 android:layout_width="353dp"
 android:layout_height="wrap_content"
 android:layout_columnSpan="4"
 android:text="清除"
 android:textSize="26sp" />
 
 <!--以下按钮为数字按钮和函数按钮,每个占1行1列-->
 <Button
 android:id="@+id/btn1"
 android:text="1"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn2"
 android:text="2"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn3"
 android:text="3"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btnPlus"
 android:text="+"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn4"
 android:text="4"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn5"
 android:text="5"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn6"
 android:text="6"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btnSubtract"
 android:text="-"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn7"
 android:text="7"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn8"
 android:text="8"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn9"
 android:text="9"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btnMultiply"
 android:text="*"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btnPoint"
 android:text="."
 android:textSize="26sp" />
 <Button
 android:id="@+id/btn0"
 android:text="0"
 android:textSize="26sp" />
 <Button
 android:id="@+id/btnSum"
 android:text="="
 android:textSize="26sp" />
 <Button
 android:id="@+id/btnDivide"
 android:text="/"
 android:textSize="26sp" />
 
</GridLayout>

界面:

Android Studio怎么实现简单计算器APP

MainActivity.java---功能实现

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
 Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
 btnClear,btnPlus,btnSubtract,btnMultiply,btnDivide,btnSum,btnPoint;
 TextView text;
 String str = "";
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 btn0 = (Button) findViewById(R.id.btn0);
 btn1 = (Button) findViewById(R.id.btn1);
 btn2 = (Button) findViewById(R.id.btn2);
 btn3 = (Button) findViewById(R.id.btn3);
 btn4 = (Button) findViewById(R.id.btn4);
 btn5 = (Button) findViewById(R.id.btn5);
 btn6 = (Button) findViewById(R.id.btn6);
 btn7 = (Button) findViewById(R.id.btn7);
 btn8 = (Button) findViewById(R.id.btn8);
 btn9 = (Button) findViewById(R.id.btn9);
 btnClear = (Button) findViewById(R.id.btnClear);
 btnPlus = (Button) findViewById(R.id.btnPlus);
 btnSubtract = (Button) findViewById(R.id.btnSubtract);
 btnMultiply = (Button) findViewById(R.id.btnMultiply);
 btnDivide = (Button) findViewById(R.id.btnDivide);
 btnPoint = (Button) findViewById(R.id.btnPoint);
 btnSum = (Button) findViewById(R.id.btnSum);
 
 text = (TextView) findViewById(R.id.text) ;
 
 btn0.setOnClickListener(this);
 btn1.setOnClickListener(this);
 btn2.setOnClickListener(this);
 btn3.setOnClickListener(this);
 btn4.setOnClickListener(this);
 btn5.setOnClickListener(this);
 btn6.setOnClickListener(this);
 btn7.setOnClickListener(this);
 btn8.setOnClickListener(this);
 btn9.setOnClickListener(this);
 btnClear.setOnClickListener(this);
 btnPlus.setOnClickListener(this);
 btnSubtract.setOnClickListener(this);
 btnMultiply.setOnClickListener(this);
 btnDivide.setOnClickListener(this);
 btnPoint.setOnClickListener(this);
 btnSum.setOnClickListener(new click());
 //给所有按钮注册点击事件
 }
 
 @Override
 public void onClick(View v) {
 String input=text.getText().toString();
 switch (v.getId()){
 case R.id.btn0:
 case R.id.btn1:
 case R.id.btn2:
 case R.id.btn3:
 case R.id.btn4:
 case R.id.btn5:
 case R.id.btn6:
 case R.id.btn7:
 case R.id.btn8:
 case R.id.btn9:
 case R.id.btnPoint:
 
 text.setText(input+((Button)v).getText());
 break;
 
 case R.id.btnPlus:
 case R.id.btnSubtract:
 case R.id.btnMultiply:
 case R.id.btnDivide:
 
 text.setText(input + " " + ((Button)v).getText() + " "); //给运算符前后加空格,好判断
 break;
 
 case R.id.btnClear:
 text.setText("");
 break;
 }
 }
 class click implements View.OnClickListener {
 public void onClick (View v) {
 getResult();
 }
 }
 
 private void getResult () {
 String str1 = text.getText().toString();
 if(str1 == null || str1.equals("")){
 return;
 }
 if(!str1.contains(" ")){
 return ;
 }
 
 double result = 0;
// 第一个数的字符串
 String s1 = str1.substring(0,str1.indexOf(" "));
// 运算符
 String op = str1.substring(str1.indexOf(" ")+1,str1.indexOf(" ")+2);
// 第二个数的字符串
 String s2 = str1.substring(str1.indexOf(" ")+3);
 
 double d1 = Double.parseDouble(s1);//将数字字符串转为double类型
 double d2 = Double.parseDouble(s2);
 if (op.equals("+")) { //加法运算
 result = d1 + d2;
 } else if (op.equals("-")) { //减法运算
 result = d1 - d2;
 } else if (op.equals("*")) { //乘法运算
 result = d1 * d2;
 } else if (op.equals("/")) { //除法运算
 if (d2 == 0) { //如果被除数是0
 result = 0; //则结果是0
 }
 else {
 result = d1 / d2;
 }
 }
 
 text.setText(str1 + " = " + result); //显示计算结果
 
 if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//如果两个整数且不是出发运算
 int r = (int) result; //则结果转为整数
 text.setText(str1 + " = " + r );
 }
 
 }
 
}

三、运行测试

Android Studio怎么实现简单计算器APP

Android Studio怎么实现简单计算器APP

Android Studio怎么实现简单计算器APP

Android Studio怎么实现简单计算器APP

测试结果:

1.可以计算简单两位数的四则运算,但是如果计算超过2位数的运算,则会出现异常使程序退出。

2.四则运算中,结果可以为负数,但是运算数若为负数,则会出现异常,原因是该程序公式为【数字1 + 运算符 +数字二】,若输入负数,即多出一位运算符,则会抛出异常。 

以上是“Android Studio怎么实现简单计算器APP”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI