温馨提示×

温馨提示×

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

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

android怎么制作简易计算器

发布时间:2020-07-30 09:38:08 来源:亿速云 阅读:228 作者:小猪 栏目:开发技术

小编这次要给大家分享的是android怎么制作简易计算器,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

之前有好好完成老师留过的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怎么制作简易计算器的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

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

AI