安卓计算器源代码eclipse

时间: 2023-09-04 20:08:01 浏览: 76
以下是一个简单的安卓计算器的源代码,使用Eclipse开发: MainActivity.java ```java package com.example.calculator; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements OnClickListener { Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnAdd, btnSub, btnMul, btnDiv, btnClr, btnEql; TextView tvInput; double val1 = Double.NaN; double val2; char op; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn0 = findViewById(R.id.btn0); btn1 = findViewById(R.id.btn1); btn2 = findViewById(R.id.btn2); btn3 = findViewById(R.id.btn3); btn4 = findViewById(R.id.btn4); btn5 = findViewById(R.id.btn5); btn6 = findViewById(R.id.btn6); btn7 = findViewById(R.id.btn7); btn8 = findViewById(R.id.btn8); btn9 = findViewById(R.id.btn9); btnAdd = findViewById(R.id.btnAdd); btnSub = findViewById(R.id.btnSub); btnMul = findViewById(R.id.btnMul); btnDiv = findViewById(R.id.btnDiv); btnClr = findViewById(R.id.btnClr); btnEql = findViewById(R.id.btnEql); tvInput = findViewById(R.id.tvInput); 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); btnAdd.setOnClickListener(this); btnSub.setOnClickListener(this); btnMul.setOnClickListener(this); btnDiv.setOnClickListener(this); btnClr.setOnClickListener(this); btnEql.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn0: tvInput.setText(tvInput.getText().toString() + "0"); break; case R.id.btn1: tvInput.setText(tvInput.getText().toString() + "1"); break; case R.id.btn2: tvInput.setText(tvInput.getText().toString() + "2"); break; case R.id.btn3: tvInput.setText(tvInput.getText().toString() + "3"); break; case R.id.btn4: tvInput.setText(tvInput.getText().toString() + "4"); break; case R.id.btn5: tvInput.setText(tvInput.getText().toString() + "5"); break; case R.id.btn6: tvInput.setText(tvInput.getText().toString() + "6"); break; case R.id.btn7: tvInput.setText(tvInput.getText().toString() + "7"); break; case R.id.btn8: tvInput.setText(tvInput.getText().toString() + "8"); break; case R.id.btn9: tvInput.setText(tvInput.getText().toString() + "9"); break; case R.id.btnAdd: compute(); op = '+'; break; case R.id.btnSub: compute(); op = '-'; break; case R.id.btnMul: compute(); op = '*'; break; case R.id.btnDiv: compute(); op = '/'; break; case R.id.btnClr: tvInput.setText(""); break; case R.id.btnEql: compute(); op = '='; tvInput.setText(String.valueOf(val1)); break; } } private void compute() { if (!Double.isNaN(val1)) { val2 = Double.parseDouble(tvInput.getText().toString()); switch (op) { case '+': val1 = val1 + val2; break; case '-': val1 = val1 - val2; break; case '*': val1 = val1 * val2; break; case '/': val1 = val1 / val2; break; case '=': break; } } else { val1 = Double.parseDouble(tvInput.getText().toString()); } tvInput.setText(""); } } ``` activity_main.xml ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="16dp" tools:context=".MainActivity"> <TextView android:id="@+id/tvInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/llButtons" android:layout_marginBottom="16dp" android:background="@android:color/white" android:gravity="end" android:maxLines="1" android:padding="16dp" android:textSize="36sp" /> <LinearLayout android:id="@+id/llButtons" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="16dp" android:orientation="horizontal" android:weightSum="4"> <Button android:id="@+id/btn7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/btn8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/btn9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/btnDiv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" /> <Button android:id="@+id/btn4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/btn5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/btn6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/btnMul" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" /> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/btn3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/btnSub" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" /> <Button android:id="@+id/btn0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" /> <Button android:id="@+id/btnClr" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/btnAdd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" /> <Button android:id="@+id/btnEql" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" /> </LinearLayout> </RelativeLayout> ``` 注意:此代码仅作为示例,可能存在一些缺陷。

相关推荐

最新推荐

recommend-type

java计算器源代码及其实验报告

java计算器源代码及其实验报告,自己敲的,不是很完美但是用来学习还是够的,希望师弟师妹学得更好。
recommend-type

Android Studio实现简易计算器(表格布局TableLayout)

主要为大家详细介绍了Android Studio实现简易计算器,表格布局TableLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

android开发计算器设计开发报告.doc

Android简单应用程序开发,简单计算器。 2程序说明: 1、计算器界面友好,方便使用。 2、具有基本的加、减、乘、除功能。 3、能够判断用户输入运算数是否正确。 4、支持小数运算。 5、具有清除功能,删除所有,c。
recommend-type

Java课程设计-简单计算器(含完整源代码)

Java课程设计,用JAVA语言实现一个简单地计算器,包含基本的加减乘除,清零等功能,内容中包含工程源代码下载的地址
recommend-type

java计算器编程源代码

本人应某位网友的要求用Java做了一个简易的计算器,包括加减乘除运算,sin,cos,ln,开方,取反等多种运算。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。