android简易计算器的制作

所属分类: 软件编程 / Android 阅读数: 115
收藏 0 赞 0 分享

之前有好好完成老师留过的C++大作业,使用MFC制作通讯录。所以用AS写一个安卓的计算器并不是很难,但还是想上手操作一下,写一个只有简单加减乘除运算的小计算器,后面可能会考虑加一些其他的稍微复杂的计算功能。下面是步骤。

1.首先创建一个empty activity,取名为MyStudyCalculator。

2.打开activity_main.xml文件,创建两个编辑框(EditText)、四个按钮(Button)、一个文本框(TextView),并设置相应的id。其中编辑框作用是让用户填入两个数字,四个按钮分别对应四种不同的运算(需要对按钮分别添加响应事件),文本框用于显示运算结果。我另外添加了两个文本框,一个用于显示标题,一个显示作者,对于该计算器来说没有任何作用。下面给出代码:

//第一个数字
  <EditText
    android:id="@+id/first"
    android:layout_width="85dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="56dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="168dp"
    android:hint="@string/num1"
    app:layout_constraintEnd_toStartOf="@+id/second"
    app:layout_constraintHorizontal_bias="0.621"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //第二个数字
  <EditText
    android:id="@+id/second"
    android:layout_width="85dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="64dp"
    android:layout_marginTop="168dp"
    android:hint="@string/num2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //第二个数字
  <TextView
    android:id="@+id/res"
    android:layout_width="96dp"
    android:layout_height="33dp"
    android:layout_marginBottom="84dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:gravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.481"
    app:layout_constraintStart_toStartOf="parent" />
 
  //加法按钮
  <Button
    android:id="@+id/button1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="260dp"
    android:onClick="SUM"
    android:text="+"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.391"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //减法按钮
  <Button
    android:id="@+id/button2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="30dp"
    android:layout_marginEnd="148dp"
    android:layout_marginTop="8dp"
    android:onClick="SUB"
    android:text="-"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.595" />
 
  //乘法按钮
  <Button
    android:id="@+id/button3"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="152dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:onClick="MUL"
    android:text="*"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.393"
    app:layout_constraintStart_toStartOf="parent" />
 
  //除法按钮
  <Button
    android:id="@+id/button4"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="148dp"
    android:layout_marginTop="8dp"
    android:onClick="DIV"
    android:text="/"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.678" />
 
  //标题
  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="36dp"
    android:text="丑陋的而且只能算加减乘除的计算机"
    android:textSize="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //作者
  <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="11dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="496dp"
    android:text="TimberWolf"
    android:textSize="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.99"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

3.打开MainActivity.java写四个按钮对应的方法,代码如下:

//加法
  public void SUM(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 + num2;
    res.setText(String.valueOf(ans));
  }
  //减法
  public void SUB(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 - num2;
    res.setText(String.valueOf(ans));
  }
  //乘法
  public void MUL(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 * num2;
    res.setText(String.valueOf(ans));
  }
  //除法
  public void DIV(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 / num2;
    res.setText(String.valueOf(ans));
  }

4.看似代码很长,其实都是一样的,很简单就完成了,其中MainActivity.java中的代码我没有给出注释,就是几种类型的转换。欢迎大佬指点,初学者不懂的可以给我留言。下面给出仿真机运行效果。

更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习

关于Android计算器功能的实现,查看专题:Android计算器 进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多