在android studio中做一个简单计算器APP 您所在的位置:网站首页 火鸟cms 在android studio中做一个简单计算器APP

在android studio中做一个简单计算器APP

2023-09-18 21:27| 来源: 网络整理| 查看: 265

实验要求

1.界面按书上例2-9设计

2.实现其具体功能:

1)代码注释

2)在博客上写一个开发日志

实现过程

1.新建项目

使用Android Studio新建一个空项目

2.添加布局

在这里,我是用的是课本例2-9的网格布局,在界面设计区域中设置一个6行4列的网格布局,第1行为显示数据的文本标签,第2行为清除数据的按钮,第3~6行均划分为4列,共安排16个按钮,分别代表了数字0、1、2、......、9及加、减、乘、除、等号等符号。代码如下:

  

3.MainActivity的编写

这部分就是对各个按钮点击事件的处理,也就是对键入的数字进行运算,判断之类的。代码如下:

package com.example.calculate; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity{ private StringBuilder show_equation=new StringBuilder();//显示运算式 private ArrayList calculate_equation;//计算式 private int signal=0;//为0 时表示刚输入状态;为1 时表示当前在输出结果上继续输入 @Override protected void onCreate(Bundle savedInstanceState) { //Activity的生命周期法 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 show_equation=new StringBuilder(); calculate_equation=new ArrayList(); Button zero=(Button)findViewById(R.id.zero); Button one=(Button)findViewById(R.id.one); Button two=(Button)findViewById(R.id.two); Button three=(Button)findViewById(R.id.three); Button four=(Button)findViewById(R.id.four); Button five=(Button)findViewById(R.id.five); Button six=(Button)findViewById(R.id.six); Button seven=(Button)findViewById(R.id.seven); Button eight=(Button)findViewById(R.id.eight); Button nine=(Button)findViewById(R.id.nine); Button cls=(Button)findViewById(R.id.cls); Button div=(Button)findViewById(R.id.div); Button mul=(Button)findViewById(R.id.mul); Button sub=(Button)findViewById(R.id.sub); Button add=(Button)findViewById(R.id.add); final Button equal=(Button)findViewById(R.id.equal); final Button point=(Button)findViewById(R.id.spot); final EditText res


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有