移动技术开发:简单计算器界面

1 实验名称

简单计算器界面

2实验目的

掌握基本布局管理器的使用方法和基本控件的使用方法,以及事件监听处理的使用方法

3 实验源代码

布局文件代码:

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="0"
        android:gravity="right|bottom"
        android:minLines="2"
        android:enabled="false"
        />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="5"
        >

        <Button
            style="@style/btnStyle"
            android:text="MC"
            />

        <Button
            style="@style/btnStyle"
            android:text="MR"
            />

        <Button
            style="@style/btnStyle"
            android:text="MS"
            />

        <Button
            style="@style/btnStyle"
            android:text="M+"
            />

        <Button
            style="@style/btnStyle"
            android:text="M-"
            />

        <Button
            style="@style/btnStyle"
            android:text="←"
            />

        <Button
            style="@style/btnStyle"
            android:text="CE"
            />

        <Button
            style="@style/btnStyle"
            android:text="C"
            />

        <Button
            style="@style/btnStyle"
            android:text="±"
            />

        <Button
            style="@style/btnStyle"
            android:text="√"
            />

        <Button
            style="@style/btnStyle"
            android:text="7"
            />

        <Button
            style="@style/btnStyle"
            android:text="8"
            />

        <Button
            style="@style/btnStyle"
            android:text="9"
            />

        <Button
            style="@style/btnStyle"
            android:text="/"
            />

        <Button
            style="@style/btnStyle"
            android:text="%"
            />

        <Button
            style="@style/btnStyle"
            android:text="4"
            />

        <Button
            style="@style/btnStyle"
            android:text="5"
            />

        <Button
            style="@style/btnStyle"
            android:text="6"
            />

        <Button
            style="@style/btnStyle"
            android:text="*"
            />

        <Button
            style="@style/btnStyle"
            android:text="1/X"
            />


    </GridLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btnOne"
            style="@style/btnStyle"
            android:text="1"
            />
        <Button
            android:id="@+id/btnTwo"
            style="@style/btnStyle"
            android:text="2"
            android:layout_toRightOf="@id/btnOne"
            />
        <Button
            android:id="@+id/btnThree"
            style="@style/btnStyle"
            android:text="3"
            android:layout_toRightOf="@id/btnTwo"
            />
        <Button
            android:id="@+id/bar"
            style="@style/btnStyle"
            android:text="-"
            android:layout_toRightOf="@id/btnThree"
            />
        <Button
            android:id="@+id/btnEqual"
            android:layout_width="80dp"
            android:layout_height="122dp"
            android:text="="
            android:layout_toRightOf="@id/bar"
            />

        <Button
            android:id="@+id/btnAdd"
            style="@style/btnStyle"
            android:text="+"
            android:layout_toLeftOf="@id/btnEqual"
            android:layout_alignBottom="@+id/btnEqual"
            />

        <Button
            android:id="@+id/btnDot"
            style="@style/btnStyle"
            android:text="+"
            android:layout_toLeftOf="@id/btnAdd"
            android:layout_alignBottom="@+id/btnEqual"
            />

        <Button
            android:layout_width="162dp"
            android:layout_height="60dp"
            android:text="0"
            android:layout_toLeftOf="@id/btnDot"
            android:layout_alignBottom="@id/btnEqual"
            />

    </RelativeLayout>
</LinearLayout>

java代码:

java 复制代码
package com.example.linearlayoutdemo;

import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}

4 实验运行结果图

5 实验总结

在calculator.xml布局文件中对计算器的整体进行布局,包括一个文本编辑框、十个数字按钮以及一些功能按钮。

在写布局文件的过程中,一些功能键名称无法从键盘上直接输入,要从别的地方复制粘贴过来才行。

相关推荐
默默coding的程序猿4 分钟前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
在未来等你18 分钟前
JVM调优实战 Day 15:云原生环境下的JVM配置
java·jvm·性能优化·虚拟机·调优
funnycoffee12319 分钟前
Huawei 6730 Switch software upgrade example版本升级
java·前端·华为
Java初学者小白20 分钟前
秋招Day15 - Redis - 缓存设计
java·数据库·redis·缓存
缘来是庄20 分钟前
设计模式之组合模式
java·设计模式·组合模式
DKPT22 分钟前
Java组合模式实现方式与测试方法
java·笔记·学习·设计模式·组合模式
G探险者37 分钟前
《如何在 Spring 中实现 MQ 消息的自动重连:监听与发送双通道策略》
java·开发语言·rpc
刘大猫2644 分钟前
Datax安装及基本使用
java·人工智能·算法
Kyrie_Li1 小时前
(十五)Spring Test
java·后端·spring
马里奥Mario1 小时前
拯救被日志拖垮的线程池:Logback异步化改造实战
java