Android Studio实现简单计算器

本文介绍了如何在Android Studio中实现一个简单的计算器应用。通过解析`activity_3.xml`布局文件和`Activity3.java`源代码,展示了如何设计用户界面并处理计算逻辑,最终实现基本的加减乘除功能。
摘要由CSDN通过智能技术生成

代码:

activity_3.xml

<?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=".Activity3">
    <RelativeLayout
        android:id="@+id/r1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#1000"
        tools:ignore="MissingConstraints">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text1"
            android:layout_margin="6dp"
            android:textSize="24sp"
            android:text="">

        </TextView>
        <TextView
            android:id="@+id/textv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="0"
            android:padding="20dp"
            android:textSize="30sp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="22dp"
                    android:onClick="onClick"
                    android:text="%"
                    android:backgroundTint="#A67D3D"
                    android:textColor="#171616"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="22dp"
                    android:onClick="onClicka"
                    android:text="CE"
                    android:backgroundTint="#A67D3D"
                    android:textColor="#171616"
                    tools:ignore="OnClick" />

                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="22dp"
                    android:onClick="onClick"
                    android:textAllCaps="false"
                    android:text="Back"
                    android:backgroundTint="#A67D3D"
                    android:textColor="#171616"/>

                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#E91E63"
                    android:backgroundTint="#8E2323"
                    android:onClick="onClick"
                    android:text="÷"
                    android:textSize="24dp"
                    app:backgroundTint="#2196F3" />
            </LinearLayout>



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="24dp"
                    android:onClick="onClick"
                    android:text="1"
                    android:backgroundTint="#0000FF"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="24dp"
                    android:onClick="onClick"
                    android:text="2"
                    android:backgroundTint="#0000FF"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="24dp"
                 
  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值