Android学习记录(1):简单计算器的实现

这篇博客记录了作者在Android开发课程中的作业——创建一个简单的计算器应用。文章介绍了计算器的布局设计,包括垂直布局和各组件的比例设置,并概述了实现代码的主要部分,如定义Button和TextView,以及注册监听事件来处理计算功能。尽管已经实现了基本功能,但作者指出仍有待优化的地方,如删除功能和小数点输入的问题。
摘要由CSDN通过智能技术生成

一个简单的计算器程序

仅以此记录一下Android开发课程的作业,其中还有很多不足,需要进一步修改和优化。

布局展示:
在这里插入图片描述
布局代码:
采用垂直布局(vertical)方式,依次从上到下分为六个板块,为了比例美观,我将第一行的高度设置为150dp,其他的按行数平均分配。

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1">

        <TextView
            android:id="@+id/first_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp"/>
        <TextView
            android:id="@+id/option"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp"/>
        <TextView
            android:id="@+id/second_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp"/>
        <TextView
            android:id="@+id/equal_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp"/>
        <TextView
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp"/>

    </LinearLayout>

    <View
        android:layout_width="wrap_content"
        android:layout_height="2dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="C"/>
        <Button
            android:id="@+id/btn_surplus"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="%"/>
        <Button
            android:id="@+id/btn_root"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="√"/>
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="del"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/btn_7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="7"/>
        <Button
            android:id="@+id/btn_8"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="8"/>
        <Button
            android:id="@+id/btn_9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="30sp"
            android:text="9"/>

        <Button
            android:id="@+id/btn_plus"
            android:layout_width
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值