Android实现简易计算功能
作者:青丝缠光阴 发布时间:2022-01-10 20:42:59
标签:Android,计算
本文实例为大家分享了Android实现简易计算功能的具体代码,供大家参考,具体内容如下
效果如图:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_horizontal"
android:orientation="vertical"
android:padding="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="操作数:"
android:textSize="20sp" />
<EditText
android:id="@+id/firstNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入数值操作数"
android:textStyle="bold"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="算术运算:"
android:textSize="20sp">
</TextView>
<Spinner
android:id="@+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/sign" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="操作数:"
android:textSize="20sp" />
<EditText
android:id="@+id/secondNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入数值操作数"
android:textStyle="bold"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="等于:" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="计算结果"
android:padding="15dp"
android:textColor="#F44336"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sign">
<item>请选择运算符</item>
<item>+</item>
<item>-</item>
<item>*</item>
<item>/</item>
</string-array>
</resources>
MainActivity
package com.jld.homework;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String op;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) this.findViewById(R.id.operator);//获取活动布局中的Spinner对象
//为Spinner注册内部 * 对象
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//将Spinner选项的值赋值给成员变量op(保存算术运算符)
op = ((TextView) view).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//各类组件
Button calcButton = findViewById(R.id.calc);
EditText firstNum = findViewById(R.id.firstNum);
EditText secondNum = findViewById(R.id.secondNum);
TextView result = findViewById(R.id.result);
//计算核心
calcButton.setOnClickListener(v -> {
switch (op) {
case "+": {
double r = Double.parseDouble(firstNum.getText().toString()) + Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
case "-": {
double r = Double.parseDouble(firstNum.getText().toString()) - Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
case "*": {
double r = Double.parseDouble(firstNum.getText().toString()) * Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
case "/": {
double r = Double.parseDouble(firstNum.getText().toString()) / Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
default://非法情况报错
result.setText(R.string.ERROR);
break;
}
});
}
}
来源:https://blog.csdn.net/UZDW_/article/details/117369694


猜你喜欢
- SimpleDateFormat是处理日期格式转换的类。官方API_1.8关于SimpleDateFormat继承于DateFormate截
- 打注解@SpringBootTest的时候不会出现提示但是又导入了 <dependency> &nb
- Filter简介Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所
- 前言最近发现公司的微服务项目中没有统一的批量新增方法,公司用的是MP插件,遇到批量新增都是单独去去编写xml实现,费时费力,而MP自带的批插
- 场景最近在做数据分析项目,里面有这样一个业务:把匹配的数据打上标签,放到新的索引中。数据量:累计亿级的数据使用场景:可能会单次查询大量的数据
- 简介MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改
- 本文实例讲述了Android开发中应用程序分享功能。分享给大家供大家参考,具体如下:Intent shareIntent = new Int
- 一、概述本质上,这是一个包含有可选值的包装类,这意味着 Optional 类既可以含有对象也可以为空。Optional 是 Java 实现函
- 多租户(Multi-Tenant)是SaaS中的一个重要概念,它是一种软件架构技术,在多个租户的环境下,共享同一套系统实例,并且租户之间的数
- 本文实例讲述了Android开发之滑动数值选择器NumberPicker用法。分享给大家供大家参考,具体如下:简介:NumberPicker
- 说明:以下的代码基于httpclient4.5.2实现。我们要使用java的HttpClient实现get请求抓取网页是一件比较容易实现的工
- 开篇Druid号称是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。作为日常使用较多的数据库连接组件,纯粹个人兴趣研究下
- 使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存下载地址:http://code.google.co
- 在java多线程程序中,所有线程都不允许抛出未捕获的checked exception,也就是说各个线程需要自己把自己的checked ex
- 一、VSCode安装EmmyLua 二、添加配置文件三、设置配置文件执行完第二步会弹出添加好的launch.json配置文件,这个
- 如何高效地进行数组复制?如果把一个变量值复制给另外一个数组变量,那么2个变量指向托管堆上同一个引用。如果想在托管堆上创建另外的一份数组实例,
- 本文实例为大家分享了java获取当前时间年月日的具体代码,供大家参考,具体内容如下import java.text.ParseExcepti
- 目录1、什么是LockSupport?2、两类基本API3、LockSupport本质4、LockSupport例子5、LockSuppor
- 本文实例为大家分享了Android实现截长图功能的具体代码,供大家参考,具体内容如下先看看手机自带的长截屏功能: 机型: viv
- springmvc 中的 * 可以对请求进行判别, 在请求到达控制器之前, 把非法的请求给拦截掉下面来说一说, 它在springboot中的