Android入门之计算器

本文作者分享了自己使用Android Studio开发计算器应用的过程,包括界面设计、百分比布局的使用、按钮边框设置、点击反馈效果、异常处理、手机震动功能的实现以及中缀表达式转后缀表达式算法的应用。通过这个项目,作者加深了对Java和Android开发的理解。
摘要由CSDN通过智能技术生成

源代码:https://download.csdn.net/download/a1203991686/10609101

1. 前言

这是我的第一个安卓app,从界面到算法都是自己写的,期间经历了各种各样的bug,也学到的各种各样的知识,加深了对Java的使用,也了解了如何写一个Android应用。因为我Java也才入门,安卓更不用说(第一行代码第三章都没看完),所以代码写的肯定非常粗糙,望大家谅解。后期我会对其进行改进,也会上传到此博客

2. 准备

1. 编译器:Android Studio

虽说AndroidStudio有很多不如人意的地方(主要还是谷歌是一个404小公司),特别是gradle。但是其作为现在谷歌力推的编译器,还是很值得信赖的。基于Idea,所以界面、插件等方面都不用说,而且从Idea过来简直就是秒适应(毕竟他就相当于一个加强版Idea)。所以我个人推荐AndroidStudio。

2. 所需知识:

  1. 有一个简单的Java基础。
    我也就学到异常和泛型这块,IO、多线程都没学
  2. 对Android开发有一个简单的了解
    我是看第一行代码看到第三章基础空间以及布局就开始写了
  3. 对计算器算法的大致了解
    中缀表达式转后缀表达式,后缀表达式的运算

3. 学会使用百度谷歌等搜索引擎

很多东西书上没讲,你不知道怎么实现,就直接百度或者google,找到别人的博客,然后好好看一遍,就算你看不懂,但是一定要看一遍,对其有一个大致的了解

4. 会使用AndroidStudio

至少懂得如何使用快捷键,懂得编译器界面上各个部分干嘛的

3. 实现效果,截图

1. 实现效果

  1. 能实现表达式的运算(废话,好歹是个计算器)
  2. 当你表达式存在问题时,不让你输入,或者不让你按下等号,并且某些情况会提示你
  3. 能对负数进行计算
  4. 有些简单的自动补全功能
  5. 能抛出异常

2. 截图

这里写图片描述


代码部分

1. 界面

1. 使用百分比布局

我选择的是百分比界面,但是由于百分比布局是新增的布局,所以你得手动添加其相应的依赖

  • 打开 app/build.gradle 文件,在dependencies闭包中添加这样一行代码
dependencies {
   
    implementation 'com.android.support:percent:28.0.0-alpha3'
}
  • 添加完毕后,编辑面板顶部会出现一行提示,点击Sync now稍等片刻即可
    这里写图片描述

2. button周边的边框

因为Android对button不能通过设置属性来达到显示边框的功能,所以我们只能创建一个图片文件,在图片中设置背景色以及边框的宽度和颜色,然后把button的background属性设置为对应图片

  • 首先在app/src/main/res/drawable文件夹中新建一个Drawable resource file
  • 在弹出的窗口中设置文件名,文件名随意,但是要简单易懂,而且编译器不报错,例如我设置的shape_blue_bg
  • 然后切换到text视图,把代码修改为
一定要把下面代码中的注释删掉
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#f0ffff"/> //设置背景颜色,也就是你按键想要显示的颜色
    <stroke
        android:width="0.01dp" //设置边框的宽度
        android:color="#ccc0c0c0"/> //设置边框的颜色
</shape>
  • 然后再在你的布局文件(例如app/src/main/res/layout/activity_main.xml)中,对需要显示边框的button中添加设置背景这行代码
一定要把下面代码中的注释删掉
<Button
        android:id="@+id/button0"
        android:background="@drawable/click_white" //设置button的背景
        />

3. 实现button点击变色功能

这里写图片描述

  • 首先在app/src/main/res/drawable文件夹中新建一个Drawable resource file,随意起名,例如我的click_blue
  • 把代码修改为
一定要把下面代码中的注释删掉
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_click_blue_bg" //设置背景,就是上面的有边框的文件,你也可以就设置成颜色,或者其他图片
        android:state_enabled="true"
        android:state_pressed="true"/> //当button被点击时
    <item android:drawable="@drawable/shape_white_bg"//设置背景,就是上面的有边框的文件,你也可以就设置成颜色,或者其他图片
        android:state_enabled="true"
        android:state_pressed="false"/> //当button未被点击时
</selector>
  • 你可以看到他需要两个drawable文件,一个是点击时的显示效果,一个是未点击时的显示效果,而在设置边框时还只创建了一个文件,也就是他不点击时的文件,接下来,我们还得创建一个跟他一样的文件,只是颜色不同而已
  • 创建一个边框背景文件,随意命名
  • 把代码修改为如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <stroke
        android:width="0.01dp"
        android:color="#ccc0c0c0"/>
</shape>
  • 然后再在你的布局文件(例如app/src/main/res/layout/activity_main.xml)中,对需要显示边框的button中添加设置背景这行代码
一定要把下面代码中的注释删掉
<Button
        android:id="@+id/button0"
        android:background="@drawable/click_blue" //设置button的背景,也就是你刚刚创建的那个起选择按压那个背景不按压那个背景的文件
        />

4. 隐藏app默认自带标题栏

  • 在代码中,在onCreate()中,在super.onCreate(savedInstanceState)之后setContentView(R.layout.activity_main)之前添加
// 隐藏标题栏
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
   
	actionBar.hide();
}

5. 隐藏系统通知栏

  • 在上面一样的地方,添加
// 隐藏通知栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

6. 修改app名称

  • app/src/java/res/values/strings.xml文件中将第二行(一般默认是第二行,不是第二行的话就在其他地方找,反正编辑器一定会自动创建这行代码的)
<resources>
    <string name="app_name">小柯基的calculator</string> //“小柯基的calculator”就是我的app名称,你可以换成任何自己想要的名字
</resources>

7. 修改app图标

因为篇幅文体我就不再这说了,分享博客地址
https://blog.csdn.net/zhangkaidsy/article/details/74852470

8. 将默认字体修改为非衬线字体

由于默认字体我觉得有点粗了,而网上的使用极细字体的方法又太细了,所以我就想到了修改字体的方法,还是在layout设置布局的xml文件中,找到你要修改字体的组件,添加一行android:typeface=“sans”

<Button
        android:id="@+id/button0"
        android:text="@string/num0"
        android:typeface="sans" //这行代码就是修改
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

详情见https://blog.csdn.net/l_lhc/article/details/51769245

9. 字体能适应显示框而改变大小

大家在很多app中都看到过一个功能,就是你输入的文字,能随着你输入的文字的数量而快占满显示框的时候,自动缩小字体以适应显示框
**图片**

  • 同样是在显示布局的文件中,在显示组件中添加一行代码
 <TextView
        android:id="@+id/text_view"
        app:autoSizeTextType="uniform" //开启字体自适应功能
        app:layout_heightPercent="25%"
        app:layout_widthPercent="100%" />
  • 但是这种方法存在一个问题,就是它自动适应输入框大小,就是说,你的输入框有多大,他显示的字体就有多大,所以我们得对他做个限制。限制最大字体和最小字体以及每次变化的字体的大小
 <TextView
        android:id="@+id/text_view"
        app:autoSizeMaxTextSize="80sp" //最大字体
        app:autoSizeMinTextSize="20sp" //最小字体
        app:autoSizeStepGranularity="4sp" //每次变化的字体大小
        app:autoSizeTextType="uniform" //开启字体自适应功能
        app:layout_heightPercent="25%"
        app:layout_widthPercent="100%" />

10. 关于组件之间配色问题

自己百度(手动doge)

11. 代码

layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/buttonC"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@drawable/click_yellow"
        android:gravity="center"
        android:text="@string/C"
        android:textColor="#6f6f6f"
        android:textSize="30sp"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button0"
        android:layout_width="81dp"
        android:layout_height="81dp"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@+id/buttonC"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num0"
        android:typeface="sans"
        android:textSize="30sp"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonDot"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@id/button0"
        android:background="@drawable/click_yellow"
        android:gravity="center"
        android:text="@string/Dot"
        android:textSize="30sp"
        android:textColor="#6f6f6f"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonEqual"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@id/buttonDot"
        android:background="#58a6e3 "
        android:gravity="center"
        android:text="@string/equal"
        android:textColor="#fff"
        android:textSize="40sp"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button1"
        android:layout_above="@id/buttonC"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num1"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        android:textSize="30sp"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button2"
        android:layout_above="@id/button0"
        android:layout_toEndOf="@id/button1"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num2"
        android:textSize="30sp"
        android:textColor="#6f6f6f"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button3"
        android:layout_above="@id/buttonDot"
        android:layout_toEndOf="@id/button2"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num3"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonAdd"
        android:layout_above="@id/buttonEqual"
        android:layout_toEndOf="@id/button3"
        android:background="@drawable/click_blue_light"
        android:gravity="center"
        android:text="@string/add"
        android:textColor="#019eff"
        android:textSize="40sp"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button4"
        android:layout_above="@id/button1"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num4"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button5"
        android:layout_above="@id/button2"
        android:layout_toEndOf="@id/button4"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num5"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button6"
        android:layout_above="@id/button3"
        android:layout_toEndOf="@id/button5"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num6"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonDec"
        android:layout_above="@id/buttonAdd"
        android:layout_toEndOf="@id/button6"
        android:background="@drawable/click_blue_light"
        android:gravity="center"
        android:text="@string/dec"
        android:textColor="#019eff"
        android:textSize="40sp"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="81dp"
        android:layout_above="@id/button4"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num7"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button8"
        android:layout_width="81dp"
        android:layout_height="81dp"
        android:layout_above="@id/button5"
        android:layout_marginStart="0dp"
        android:layout_toEndOf="@id/button7"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num8"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/button9"
        android:layout_above="@+id/button6"
        android:layout_toEndOf="@id/button8"
        android:background="@drawable/click_white"
        android:gravity="center"
        android:text="@string/num9"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#6f6f6f"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonMulti"
        android:layout_above="@id/buttonDec"
        android:layout_toEndOf="@id/button9"
        android:background="@drawable/click_blue_light"
        android:gravity="center"
        android:text="@string/multi"
        android:textColor="#019eff"
        android:textSize="40sp"
        android:typeface="sans"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonAC"
        android:layout_above="@+id/button7"
        android:layout_alignParentStart="true"
        android:background="@drawable/click_blue_light"
        android:gravity="center"
        android:text="@string/AC"
        android:textSize="30sp"
        android:textColor="#e47c5e"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonBracketLeft"
        android:layout_above="@id/button8"
        android:layout_toEndOf="@id/buttonAC"
        android:background="@drawable/click_blue_light"
        android:gravity="center"
        android:text="@string/BracketLeft"
        android:textSize="30sp"
        android:typeface="sans"
        android:textColor="#019eff"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="25%" />

    <Button
        android:id="@+id/buttonBracketRight"
        android:layout_above="@id/button9"
        android:layout_toEndOf="@id/buttonBracketLeft"
        android:background="@drawable/click_blue_light"
        android:gravity="center"
        android:text="@string/BracketRight"
        android:textSize="30sp"
        android:textColor="#019eff"
        android:typeface
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值