文章目录

  • 线性布局
  • 横屏界面实现
  • 横竖屏切换数值的简单保存
  • 计算器界面的实现

线性布局

  • 当布局方向为水平方向时:
    width设置为0dp,权重weight才会按比例分配。
  • 当布局方向为竖直方向时:
    height设置为0dp,权重weight才会按比例分配。

例子:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="0dp" android:layout_weight="1"/><Buttonandroid:layout_width="match_parent"android:layout_height="0dp" android:layout_weight="2"/><Buttonandroid:layout_width="match_parent"android:layout_height="0dp" android:layout_weight="3"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

横屏界面实现

方式一:直接创建一个land.xml配置文件


方式二:在layout下,建立两个布局文件,一个是横屏的一个是竖屏的(名称任意),然后再java文件中布置这两个文件就行

横竖屏切换数值的简单保存

onSaveInstanceState方法会在什么时候被执行,有这么几种情况:

1、当用户按下HOME键时。

这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则

2、长按HOME键,选择运行其他的程序时。

3、按下电源按键(关闭屏幕显示)时。

4、从activity A中启动一个新的activity时。

5、屏幕方向切换时,例如从竖屏切换到横屏时。

在屏幕切换之前,系统会销毁activity A,在屏幕切换之后系统又会自动地创建activity A,所以onSaveInstanceState一定会被执行

总而言之,onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据(当然你不保存那就随便你了)

public class MainActivity extends AppCompatActivity {Button bt1,bt2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt1 = findViewById(R.id.onebutton);bt2 = findViewById(R.id.twobutton);bt2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {bt1.setText(bt2.getText().toString());}});if(savedInstanceState!=null){String s = savedInstanceState.getString("KEY");bt1.setText(s);}}@Overrideprotected void onSaveInstanceState(@NonNull Bundle outState) {super.onSaveInstanceState(outState);outState.putString("KEY",bt1.getText().toString());}
}

计算器界面的实现

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><!--竖屏一个EditText,4*6个键实现计算器-->
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical"><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="3.82"android:background="@null"android:gravity="right|bottom"android:text=""android:textSize="30sp"android:id="@+id/editText"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp" android:layout_weight="6.18" android:orientation="horizontal"><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"><Buttonandroid:id="@+id/bmc"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="mc"android:textAllCaps="false"android:textSize="24sp" /><Buttonandroid:id="@+id/bac"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="AC"android:textAllCaps="false"android:textSize="24sp" /><Buttonandroid:id="@+id/b7"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="7"android:textSize="24sp" /><Buttonandroid:id="@+id/b4"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="4"android:textSize="24sp" /><Buttonandroid:id="@+id/b1"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="1"android:textSize="24sp" /><Buttonandroid:id="@+id/bpercent"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="%"android:textSize="24sp" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"><Buttonandroid:id="@+id/bmadd"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="m+"android:textAllCaps="false"android:textSize="24sp" /><Buttonandroid:id="@+id/bmul"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="*"android:textSize="24sp" /><Buttonandroid:id="@+id/b8"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="8"android:textSize="24sp" /><Buttonandroid:id="@+id/b5"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="5"android:textSize="24sp" /><Buttonandroid:id="@+id/b2"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="2"android:textSize="24sp" /><Buttonandroid:id="@+id/b0"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="0"android:textSize="24sp" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"><Buttonandroid:id="@+id/bmsub"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="m-"android:textAllCaps="false"android:textSize="24sp" /><Buttonandroid:id="@+id/bdiv"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="/"android:textSize="24sp" /><Buttonandroid:id="@+id/b9"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="9"android:textSize="24sp" /><Buttonandroid:id="@+id/b6"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="6"android:textSize="24sp" /><Buttonandroid:id="@+id/b3"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="3"android:textSize="24sp" /><Buttonandroid:id="@+id/bpoint"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ffffff"android:text="."android:textSize="24sp" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"><Buttonandroid:id="@+id/bmr"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="mr"android:textAllCaps="false"android:textSize="24sp" /><Buttonandroid:id="@+id/bdel"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="DEL"android:textAllCaps="false"android:textSize="24sp" /><Buttonandroid:id="@+id/badd"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="+"android:textSize="24sp" /><Buttonandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#E8E8E8"android:text="-"android:textSize="24dp" android:id="@+id/bsub"/><Buttonandroid:id="@+id/bequal"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="2"android:background="#03A9F4"android:text="="android:textColor="#FFFFFF"android:textSize="24sp" /></LinearLayout></LinearLayout></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

界面如下:

简单计算代码实现:

package com.example.cal;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends AppCompatActivity implements View.OnClickListener{EditText input;//mc,m+,m-,mrButton bmC,bmAdd,bmSub,bmR;//+,-,*,/Button bAdd,bSub,bMul,bDiv;//=,%,.,ac,delButton bEqual,bPercent,bPoint,bAc,bDel;//0-9Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {input = findViewById(R.id.editText);input.setOnClickListener(this);bmC = findViewById(R.id.bmc);bmC.setOnClickListener(this);bmAdd = findViewById(R.id.bmadd);bmAdd.setOnClickListener(this);bmSub = findViewById(R.id.bmsub);bmSub.setOnClickListener(this);bmR = findViewById(R.id.bmr);bmR.setOnClickListener(this);bAdd = findViewById(R.id.badd);bAdd.setOnClickListener(this);bSub= findViewById(R.id.bsub);bSub.setOnClickListener(this);bMul= findViewById(R.id.bmul);bMul.setOnClickListener(this);bDiv = findViewById(R.id.bdiv);bDiv.setOnClickListener(this);bEqual = findViewById(R.id.bequal);bEqual.setOnClickListener(this);bPercent = findViewById(R.id.bpercent);bPercent.setOnClickListener(this);bPoint = findViewById(R.id.bpoint);bPoint.setOnClickListener(this);bAc = findViewById(R.id.bac);bAc.setOnClickListener(this);bDel = findViewById(R.id.bdel);bDel.setOnClickListener(this);b0 = findViewById(R.id.b0);b0.setOnClickListener(this);b1 = findViewById(R.id.b1);b1.setOnClickListener(this);b2 = findViewById(R.id.b2);b2.setOnClickListener(this);b3 = findViewById(R.id.b3);b3.setOnClickListener(this);b4 = findViewById(R.id.b4);b4.setOnClickListener(this);b5 = findViewById(R.id.b5);b5.setOnClickListener(this);b6 = findViewById(R.id.b6);b6.setOnClickListener(this);b7 = findViewById(R.id.b7);b7.setOnClickListener(this);b8 = findViewById(R.id.b8);b8.setOnClickListener(this);b9 = findViewById(R.id.b9);b9.setOnClickListener(this);}@Overridepublic void onClick(View view) {String temp = input.getText().toString();switch (view.getId()){case R.id.bac:input.setText("0");break;case R.id.bdel:if(temp.length()<=1){input.setText("0");}else {input.setText(temp.substring(0,temp.length()-1));}break;case R.id.bpercent:case R.id.badd:case R.id.bsub:case R.id.bmul:case R.id.bdiv:input.setText(temp+" "+((Button)view).getText().toString()+" ");break;case R.id.b0:case R.id.b1:case R.id.b2:case R.id.b3:case R.id.b4:case R.id.b5:case R.id.b6:case R.id.b7:case R.id.b8:case R.id.b9:case R.id.bpoint:input.setText(temp+((Button)view).getText().toString());break;case R.id.bequal:getResult();break;}}private void getResult() {String temp = input.getText().toString();if(TextUtils.isEmpty(temp)){return;}double one=Double.parseDouble(temp.substring(0,temp.indexOf(" ")));String fuHao=temp.substring(temp.indexOf(" ")+1,temp.indexOf(" ")+2);double two = 0;if(!"%".equals(fuHao)) {two = Double.parseDouble(temp.substring(temp.indexOf(" ") + 3, temp.length()));}double result = 0;if("+".equals(fuHao)){result=one+two;}else if("-".equals(fuHao)){result=one-two;}else if("*".equals(fuHao)){result=one*two;}else if("/".equals(fuHao)){if(two==0) return;result=one/two;}else if("%".equals(fuHao)){result=one/100;}input.setText(String.valueOf(result));}
}

Android计算器简单实现相关推荐

  1. Android开发——简单计算器实现

    计算器项目,要求实现加.减.乘.除.求倒数.求平方根等简单运算. 真机调试结果如下图: 布局文件:main_activity.xml <?xml version="1.0" ...

  2. Android实现简单的计算器功能

    Android实现简单的计算器功能 **前言:**通过Android实现简单的计算器功能,实现简单的加.减.乘.除操作. 效果图如下: 第一步 布局文件:activity_main.xml <? ...

  3. github android 计算器,Android studio实现简单的计算器

    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析及概要设计 目的 开发一个简单的计算器App,使之能够完成加减乘除混合运算 工具及环境 使用 ...

  4. Android 计算器(Calculator)三角函数默认角度计算

    Android计算器源码路径:alps/packages/apps/Calculator/ 具体计算代码在 文件CalculatorExpressionEvaluator.java 函数evaluat ...

  5. Calcu 计算器简单去广告教程+修改应用名称

    Calcu Calcu是安卓手机上的一款简约,小巧,强大的计算器. 简洁.美观.强大,这是开发者对 CALCU 的定义.12 种主题可选,按键可自定义,可以说 CALCU 是一部可私人定制的计算器. ...

  6. Android Studio简单设置

    2019独角兽企业重金招聘Python工程师标准>>> Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面 ...

  7. 简单Android手机APP地图,android最简单手机地图APP(只需5分钟)

    android最简单手机地图APP--只有三部分. 第一部分 首先建立一个MapActivity在setContentView(R.layout.activity_map);中创建一个代码如下. [h ...

  8. android仿微信的activity平滑水平切换动画,Android实现简单底部导航栏 Android仿微信滑动切换效果...

    Android实现简单底部导航栏 Android仿微信滑动切换效果 发布时间:2020-10-09 19:48:00 来源:脚本之家 阅读:96 作者:丶白泽 Android仿微信滑动切换最终实现效果 ...

  9. Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能)

    Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能) 导读 动态演示调用python库的tkinter带你进入GUI世界(计算器简单功能) 目录 ...

  10. 【转】Android Studio简单设置

    原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...

最新文章

  1. 20169211 2016-2017-2 《移动平台开发实践》 第十周实验总结
  2. 三十八,反射的应用:工厂模式
  3. 用MathType编辑带点星号的流程
  4. python语句print(tuple(range(2)))_Python学习(四)数据结构 —— list tuple range
  5. 李航:未来若干年,AI 技术发展可能会进入平缓期
  6. windows服务器管理(1)——WinServer2012 开启中文语言包(无需下载补丁)
  7. CV2.imread得到的图像显示成蓝色的原因和3种BGR转RGB的方法
  8. vue学习-MVVM的实现原理
  9. 建模与仿真matlab论文,基于MATLAB的无线信道建模与仿真.doc
  10. linux node安装菜鸟教程,手把手告诉你如何安装多个版本的node
  11. 虚拟机桥接模式配置网络
  12. Django详细教程(图文)
  13. 【Andrew Gelman多元统计】(基于R)
  14. Webots学习笔记 1.创建自己的仿真机器人模型
  15. DCS是分布式控制系统的英文缩写(Distributed Control System)
  16. 新手福利!超详细图标设计的七大原则
  17. 议程速递 | 7月27日分论坛议程一览
  18. 用DP解决排列组合问题
  19. MD制作服装导入DAZ
  20. 【85期分享】4款小清新PPT模板免费下载

热门文章

  1. 挑战程序设计竞赛是c语言编写的嘛,POJ 2115 C Looooops 题解《挑战程序设计竞赛》...
  2. android最新版本8.0,安卓8.0系统安装包下载
  3. 嵌入式硬件入门——EEPROM(AT24C02+I2C协议)
  4. nero刻录软件中文版|nero express刻录软件 (附教程)
  5. ghost系统之优劣?
  6. arm-linux-gcc交叉编译器和gcc编译器的下载地址
  7. 谷歌卫星地图下载助手
  8. 如何保障科技产品供应链的安全?
  9. 电驴服务器搜索文件排序,eMule如何搜索文件
  10. 注册表修改服务器连接数量,如何通过Win10注册表更改时间服务器参数值?