利用android studio制作简易的计算器

利用android studio制作简易的计算器

Android Studio简介

Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调试。
在IDEA的基础上,Android Studio 增加了:

  • 基于Gradle的构建支持
  • Android 专属的重构和快速修复
  • 提示工具以捕获性能、可用性、版本兼容性等问题
  • 支持ProGuard 和应用签名
  • 基于模板的向导来生成常用的 Android 应用设计和组件
  • 功能强大的布局编辑器,可以让你拖拉 UI 控件并进行效果预览

正文

我这里提供一个计算器ui参考图:
在这里插入图片描述

设计思路

在设计的时候,我们可以根据要设计的程序,采用适合的布局,在Android中有七大布局,分别是:

  • LinearLayout(线性布局)
  • RelativeLayout(相对布局)
  • TableLayout(表格布局)
  • FrameLayout(帧布局)
  • AbsoluteLayout(绝对布局)
  • GridLayout(网格布局)
  • ConstraintLayout(约束布局)

目前android最新的默认布局是ConstraintLayout(约束布局)

上代码

这么多布局中,最适合做计算器的,是网格布局,因为计算器中有很多按钮,如果用其他的布局会增加很多代码,其效果也是一样的。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_row="4"
    android:stretchColumns="3">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:editable="false"
            android:inputType="number"
            android:textSize="30dp"
            android:background="@color/text_bk2">
        </EditText>
        
        <TableRow>
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text1"
                android:layout_weight="1"
                android:text="mc" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text1"
                android:layout_weight="1"
                android:text="m+" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text1"
                android:layout_weight="1"
                android:text="m-" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text1"
                android:layout_weight="1"
                android:text="mr" />
        </TableRow>

        <TableRow>
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="C" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="/" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="X" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="del" />
        </TableRow>
        
        <TableRow>
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="7" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="8" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="9" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="-" />
        </TableRow>

        <TableRow>
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="4" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="5" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="6" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="+" />
        </TableRow>

        <TableRow>
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="1" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="2" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="3" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk1"
                android:textColor="@color/text_text3"
                android:layout_weight="1"
                android:text="%" />
        </TableRow>

        <TableRow>
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="00" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="0" />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk2"
                android:textColor="@color/text_text2"
                android:layout_weight="1"
                android:text="." />
            <Button
                android:layout_width="0dp"
                android:layout_height="65dp"
                android:textSize="20dp"
                android:background="@color/text_bk3"
                android:textColor="@color/text_text4"
                android:layout_weight="1"
                android:text="=" />
        </TableRow>
    </TableLayout>
colors.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="text_bk1">#F5F5F5</color>
    <color name="text_bk2">#FCFCFC</color>
    <color name="text_bk3">#5ADCED</color>
    <color name="text_text1">#878787</color>
    <color name="text_text2">#000000</color>
    <color name="text_text3">#5ADCED</color>
    <color name="text_text4">#FCFCFC</color>
</resources>

运行结果:
在这里插入图片描述


我是ots-luo,码字不易,写教程也不易,如果觉得文章不错,可以点赞评论,感谢支持!!


更多文章记得关注我的博客
网站文章对应:点击传送

——————————END——————————

  • 13
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是最新android studio制作简易计算器的代码: ``` package com.example.calculator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText input1, input2; Button add, subtract, multiply, divide; TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); input1 = findViewById(R.id.input1); input2 = findViewById(R.id.input2); add = findViewById(R.id.add); subtract = findViewById(R.id.subtract); multiply = findViewById(R.id.multiply); divide = findViewById(R.id.divide); result = findViewById(R.id.result); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num1 = Integer.parseInt(input1.getText().toString()); int num2 = Integer.parseInt(input2.getText().toString()); int sum = num1 + num2; result.setText("Result: " + sum); } }); subtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num1 = Integer.parseInt(input1.getText().toString()); int num2 = Integer.parseInt(input2.getText().toString()); int difference = num1 - num2; result.setText("Result: " + difference); } }); multiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num1 = Integer.parseInt(input1.getText().toString()); int num2 = Integer.parseInt(input2.getText().toString()); int product = num1 * num2; result.setText("Result: " + product); } }); divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num1 = Integer.parseInt(input1.getText().toString()); int num2 = Integer.parseInt(input2.getText().toString()); int quotient = num1 / num2; result.setText("Result: " + quotient); } }); } } ``` 希望能对您有所帮助!现在您有什么需要我帮忙回答的问题吗?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值