APP-计算器
在网课上学习了计算器的制作方法,并自己做了小常识,看完一便后,感觉自己会了,思想也明白了,但是当关掉视频真正开始做的时候会发向许多的问题,自己在前的好多语法用的并不是很熟练,但是反复的查阅资料,观看教程,还是可以完成完成这个项目的开发。
activity_main.xml:
<?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"> <TextView
android:id="@+id/tvScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge"/> <TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"> <TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_weight="1"/> </TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="1"/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_weight="1"/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_weight="1"/>
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_weight="1"/> </TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_weight="1"/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:layout_weight="1"/>
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:layout_weight="1"/>
<Button
android:id="@+id/btnX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:layout_weight="1"/> </TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:layout_weight="1"/>
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:id="@+id/btnResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:layout_weight="1"/>
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:layout_weight="1"/> </TableRow>
</TableLayout> </LinearLayout>
MainActivity.java:
package com.example.counte; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private TextView tvScrean;
private List<item> items=new ArrayList<item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tvScrean=(TextView)findViewById((R.id.tvScreen));
findViewById(R.id.btn0).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn1).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn2).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn3).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn4).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn5).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn6).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn7).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn8).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btn9).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btnAdd).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btnSub).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btnX).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btnDiv).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btnClear).setOnClickListener((View.OnClickListener) this);
findViewById(R.id.btnResult).setOnClickListener((View.OnClickListener) this); } public void onClick(View v){
switch (v.getId()){
case R.id.btn0:
tvScrean.append("0");
break;
case R.id.btn1:
tvScrean.append("1");
break;
case R.id.btn2:
tvScrean.append("2");
break;
case R.id.btn3:
tvScrean.append("3");
break;
case R.id.btn4:
tvScrean.append("4");
break;
case R.id.btn5:
tvScrean.append("5");
break;
case R.id.btn6:
tvScrean.append("6");
break;
case R.id.btn7:
tvScrean.append("7");
break;
case R.id.btn8:
tvScrean.append("8");
break;
case R.id.btn9:
tvScrean.append("9");
break;
case R.id.btnAdd:
items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
check();
items.add(new item(0,Types.Add));
tvScrean.setText("");//清空文本
break;
case R.id.btnSub:
items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
check();
items.add(new item(0,Types.Sub));
tvScrean.setText("");//清空文本
break;
case R.id.btnX:
items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
check();
items.add(new item(0,Types.X));
tvScrean.setText("");//清空文本
break;
case R.id.btnDiv:
items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
check();
items.add(new item(0,Types.Div));
tvScrean.setText("");//清空文本
break;
case R.id.btnClear:
tvScrean.setText("");
items.clear();//数组清空
break;
case R.id.btnResult:
items.add(new item(Double.parseDouble(tvScrean.getText().toString()),Types.Num));
check();
tvScrean.setText(items.get(0).value+"");
items.clear();//清空数组
break; }
}
public void check(){
if(items.size()>=3){ double a=items.get(0).value;
double b=items.get(2).value;
int opt=items.get(1).type; items.clear(); switch (opt){
case Types.Add:
items.add(new item(a+b,Types.Num));
break;
case Types.Sub:
items.add(new item(a-b,Types.Num));
break;
case Types.X:
items.add(new item(a*b,Types.Num));
break;
case Types.Div:
items.add(new item(a/b,Types.Num));
break;
}
}
}
}
item.java:
package com.example.counte;
public class item {
public item(double value,int type){
this.value=value;
this.type=type;
}
public double value=0;
public int type=0;
}
Types.java:
package com.example.counte;
public class Types {
public static final int Add=1;
public static final int Sub=2;
public static final int X=3;
public static final int Div=4;
public static final int Num=5;
}
但其中出现的最为明显的问题:

APP-计算器的更多相关文章
- 第一个安卓app——计算器
几天前,我花了一天时间,结合这段时间所学知识开发出了一个简单的计算器,它由两个TextView和23个Button组成,代码会放在文章结尾. TextView TextView:上面一个TextVie ...
- 复利计算器app发布
复利计算器app发布 抱歉:由于无法实现服务端的持续开启,发布的app仅为简单的单机版,暂时舍弃了c/s版本的一些功能,如:投资动态管理功能. 应用详情博客:请点击这里 apk下载地址1(百度手机助手 ...
- ★房贷计算器 APP
一.目的 1. 这是一个蛮有用的小工具 2. 之前看了很多demo,第一次来完全的自己实现一个APP 3. 完成之后提交 App Store 4. 作为Good Coder的提交审核材料 二.排期 周 ...
- app自动化测试之实战应用(魅族计算器)
模拟魅族计算器加法计算: from appium import webdriver desired_caps = {} desired_caps['deviceName'] = '621QECQ23D ...
- 第一个APP:IOS做简单运算的计算器
步骤: 1.打开Xcode,单机Creat a new Xcode project 2.左边选择ios下Application,右边选择single view Application 3.填写项目名称 ...
- 【IOS开发笔记03-视图相关】简单计算器的实现
UIView 经过前几天的快速学习,我们初步了解的IOS开发的一些知识,中间因为拉的太急,忽略了很多基础知识点,这些知识点单独拿出来学习太过枯燥,我们在今后的项目中再逐步补齐,今天我们来学习APP视图 ...
- App测试
(1)App独特测试点: 客户端兼容性测试:系统版本.不同深度定制的rom.屏幕分辨率.中断测试.安装.卸载.升级.对其他程序的干扰等 需要的一些工具: appnium / lr / jmeter ...
- WWDC 后苹果最新 App Store 审核条款!
WWDC 2016 大会之后,苹果公司发布了四个全新平台:iOS,macOS,watchOS 和 tvOS.并且在此之后,苹果应用商店审核条款也同时进行了更新——貌似不算进行了更新,简直就是重 ...
- Windows下部署Appium教程(Android App自动化测试框架搭建)
摘要: 1,appium是开源的移动端自动化测试框架: 2,appium可以测试原生的.混合的.以及移动端的web项目: 3,appium可以测试ios.android.firefox os: 4,a ...
- 菜鸟学Android编程——简单计算器《一》
菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...
随机推荐
- jdbc oracle 连接串
jdbc.url配置为: jdbc:oracle:thin:@xxx.xx.xx.xx:1521:orclpdb 报错: java.sql.SQLException: Listenerrefused ...
- STM32中ARM系列编译工具链的编译宏选择(__CC_ARM、__ICCARM__、__GNUC__、__TASKING__)
一 前言 stm32 f103中.关系到一个选择何种编译宏的问题.这里就梳理一下吧. 二 正文 1 在 core_cm3.h 文件中,有如下代码: #if defined ( __CC_ARM ) ...
- 103-PHP定义一个类
<?php class ren{ //定义人类 } class mao{ //定义猫类 } new ren(); //实例化人类 new mao(); //实例化猫类 new mao(); // ...
- flink笔记(三) flink架构及运行方式
架构图 Job Managers, Task Managers, Clients JobManager(Master) 用于协调分布式执行.它们用来调度task,协调检查点,协调失败时恢复等. Fli ...
- tableau创建蜘蛛图
tableau官方案例2:创建起点和终点的路径地图 (spider Maps) 源数据样式: 应用:交通图 步骤及注意: 将Line Group (Path ID)维度放入标记卡详细信息 默认的为聚 ...
- javascript中new操作符的原理
javascript中的new是一个语法糖,对于学过c++,java 和c#等面向对象语言的人来说,以为js里面是有类和对象的区别的,实现上js并没有类,一切皆对象,比java还来的彻底 new的过程 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:输出
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- css代码实现列表等宽
实现上面的手机页面,我们会遇到一个自适应的问题,但是手机页面的屏幕大小不一致,自适应的问题不是百分比可以好好解决的,我采用下面的布局:display:flex; <!DOCTYPE html&g ...
- 2,The AudioContext was not allowed to start.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on t ...
- mybatis的批量update
方法有三种:1.通过java代码batch方式,xml文件只需一条update语句.java代码繁琐 2.xml使用foreach,“;”分割多条update语句,要求:jdbc的url需加上allo ...