Android Studio 制作简易计算器

这篇文章详细描述了如何在Android应用中创建一个包含数字和运算符号的按钮布局,用于实现简单的算术计算。主要展示了`MainActivity`中的Button初始化和点击事件处理,涉及到了XML布局和事件监听。
摘要由CSDN通过智能技术生成

/>

<Button

android:id=“@+id/bt_sub”

android:layout_width=“80dp”

android:layout_height=“80dp”

android:text=“-”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“10dp”

android:orientation=“horizontal”

android:gravity=“center_horizontal”

<Button

android:id=“@+id/bt_4”

android:layout_width=“80dp”

android:layout_height=“80dp”

android:text=“4”

android:gravity=“right|bottom”

android:textSize=“30sp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:id=“@+id/bt_5”

android:layout_width=“80dp”

android:layout_height=“80dp”

android:text=“5”

android:gravity=“right|bottom”

android:textSize=“30sp”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:id=“@+id/bt_6”

android:layout_width=“80dp”

android:layout_height=“80dp”

android:text=“6”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:id=“@+id/bt_add”

android:layout_width=“80dp”

android:layout_height=“80dp”

android:text=“+”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<LinearLayout

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:layout_marginTop=“10dp”

android:gravity=“center_horizontal”>

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“vertical”

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

<Button

android:layout_width=“80dp”

android:layout_height=“80dp”

android:id=“@+id/bt_1”

android:text=“1”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:layout_width=“80dp”

android:layout_height=“80dp”

android:id=“@+id/bt_2”

android:text=“2”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:layout_width=“80dp”

android:layout_height=“80dp”

android:id=“@+id/bt_3”

android:text=“3”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“horizontal”

android:layout_marginTop=“10dp”>

<Button

android:layout_width=“170dp”

android:layout_height=“80dp”

android:id=“@+id/bt_0”

android:text=“0”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:layout_width=“80dp”

android:layout_height=“80dp”

android:id=“@+id/bt_pt”

android:text=“.”

android:textSize=“30sp”

android:gravity=“right|bottom”

android:layout_marginLeft=“10dp”

android:background=“@drawable/white_selector”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

<Button

android:id=“@+id/bt_eq”

android:layout_width=“80dp”

android:layout_height=“170dp”

android:layout_marginLeft=“10dp”

android:background=“@drawable/orange_selector”

android:gravity=“right|bottom”

android:text=“=”

android:textSize=“30sp”

android:paddingRight=“15sp”

android:paddingBottom=“15sp”

/>

Mainactivity的代码:

package com.example.administrator.calculatordemo;

import android.app.Activity;

import android.content.DialogInterface;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener{

Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt;

Button bt_mul,bt_div,bt_add,bt_sub;

Button bt_clr,bt_del,bt_eq;

EditText et_input;

boolean clr_flag; //判断et中是否清空

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//实例化对象

setContentView(R.layout.activity_main);

bt_0= (Button) findViewById(R.id.bt_0);

bt_1= (Button) findViewById(R.id.bt_1);

bt_2= (Button) findViewById(R.id.bt_2);

bt_3= (Button) findViewById(R.id.bt_3);

bt_4= (Button) findViewById(R.id.bt_4);

bt_5= (Button) findViewById(R.id.bt_5);

bt_6= (Button) findViewById(R.id.bt_6);

bt_7= (Button) findViewById(R.id.bt_7);

bt_8= (Button) findViewById(R.id.bt_8);

bt_9= (Button) findViewById(R.id.bt_9);

bt_pt= (Button) findViewById(R.id.bt_pt);

bt_add= (Button) findViewById(R.id.bt_add);

bt_sub= (Button) findViewById(R.id.bt_sub);

bt_mul= (Button) findViewById(R.id.bt_mul);

bt_div= (Button) findViewById(R.id.bt_div);

bt_clr= (Button) findViewById(R.id.bt_clr);

bt_del= (Button) findViewById(R.id.bt_del);

bt_eq= (Button) findViewById(R.id.bt_eq);

et_input= (EditText) findViewById(R.id.et_input);

//设置按钮的点击事件

bt_0.setOnClickListener(this);

bt_1.setOnClickListener(this);

bt_2.setOnClickListener(this);

bt_3.setOnClickListener(this);

bt_4.setOnClickListener(this);

bt_5.setOnClickListener(this);

bt_6.setOnClickListener(this);

bt_7.setOnClickListener(this);

bt_8.setOnClickListener(this);

bt_9.setOnClickListener(this);

bt_pt.setOnClickListener(this);

bt_add.setOnClickListener(this);

bt_sub.setOnClickListener(this);

bt_mul.setOnClickListener(this);

bt_div.setOnClickListener(this);

bt_clr.setOnClickListener(this);

bt_del.setOnClickListener(this);

bt_eq.setOnClickListener(this);

}

@Override

public void onClick(View v) {

String str=et_input.getText().toString();

switch (v.getId()){

case R.id.bt_0:

case R.id.bt_1:

case R.id.bt_2:

case R.id.bt_3:

case R.id.bt_4:

case R.id.bt_5:

case R.id.bt_6:

case R.id.bt_7:

case R.id.bt_8:

case R.id.bt_9:

case R.id.bt_pt:

if(clr_flag){

clr_flag=false;

str=“”;

et_input.setText(“”);

}

et_input.setText(str+((Button)v).getText());

break;

case R.id.bt_add:

case R.id.bt_sub:

case R.id.bt_mul:

case R.id.bt_div:

if(clr_flag){

clr_flag=false;

str=“”;

et_input.setText(“”);

}

if(str.contains(“+”)||str.contains(“-”)||str.contains(“×”)||str.contains(“÷”)) {

str=str.substring(0,str.indexOf(" "));

}

et_input.setText(str+" “+((Button)v).getText()+” ");

break;

case R.id.bt_clr:

if(clr_flag)

clr_flag=false;

str=“”;

et_input.setText(“”);

break;

case R.id.bt_del: //判断是否为空,然后在进行删除

if(clr_flag){

clr_flag=false;

str=“”;

et_input.setText(“”);

}

else if(str!=null&&!str.equals(“”)){

et_input.setText(str.substring(0,str.length()-1));

}

break;

case R.id.bt_eq: //单独运算最后结果

getResult();

break;

}

}

private void getResult(){

String exp=et_input.getText().toString();

if(exp==null||exp.equals(“”)) return ;

//因为没有运算符所以不用运算

if(!exp.contains(" ")){

return ;

}

if(clr_flag){

clr_flag=false;

return;

}

clr_flag=true;

//截取运算符前面的字符串

最后

总而言之,成功是留给准备好的人的。无论是参加什么面试,都要做好充足的准备,注意好面试的礼仪和穿着,向面试官表现出自己的热忱与真诚就好。即使最后没有过关,也要做好经验的总结,为下一次面试做好充足准备。

这里我为大家准备了一些我在面试后整理的面试专题资料,除了面试题,还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料分享给大家,希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

毕竟不管遇到什么样的面试官,去面试首先最主要的就是自己的实力,只要实力够硬,技术够强,就不怕面试拿不到offer!

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料分享给大家,希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。**

毕竟不管遇到什么样的面试官,去面试首先最主要的就是自己的实力,只要实力够硬,技术够强,就不怕面试拿不到offer!

[外链图片转存中…(img-Og1a7J3o-1714291574442)]

[外链图片转存中…(img-DWLQ1BrN-1714291574443)]

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

[外链图片转存中…(img-HD2yudT4-1714291574443)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 18
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是制作简易计算器的步骤: 1. 打开Android Studio,创建一个新项目。 2. 在布局文件中添加一个EditText用于显示计算结果和多个Button用于输入数字和运算符。 3. 在MainActivity中获取EditText和Button的引用,并设置Button的点击事件。 4. 在点击事件中,根据按钮的标识符进行不同的计算,并将结果显示到EditText中。 5. 完成以上步骤后,就可以运行程序并测试计算器的功能了。 下面是一个简单的例子代码,供参考: activity_main.xml: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity"> <EditText android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginTop="16dp" android:hint="0" android:padding="16dp" android:textAlignment="textEnd" android:textSize="24sp" /> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:rowCount="4" android:columnCount="4" android:layout_below="@id/result"> <Button android:id="@+id/button_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" android:textSize="24sp" /> <Button android:id="@+id/button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" android:textSize="24sp" /> <Button android:id="@+id/button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" android:textSize="24sp" /> <Button android:id="@+id/button_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="+" android:textSize="24sp" /> <Button android:id="@+id/button_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" android:textSize="24sp" /> <Button android:id="@+id/button_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" android:textSize="24sp" /> <Button android:id="@+id/button_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" android:textSize="24sp" /> <Button android:id="@+id/button_subtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="-" android:textSize="24sp" /> <Button android:id="@+id/button_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" android:textSize="24sp" /> <Button android:id="@+id/button_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" android:textSize="24sp" /> <Button android:id="@+id/button_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" android:textSize="24sp" /> <Button android:id="@+id/button_multiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" android:textSize="24sp" /> <Button android:id="@+id/button_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill" android:text="C" android:textSize="24sp" /> <Button android:id="@+id/button_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" android:textSize="24sp" /> <Button android:id="@+id/button_equal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="=" android:textSize="24sp" /> <Button android:id="@+id/button_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="/" android:textSize="24sp" /> </GridLayout> </RelativeLayout> ``` MainActivity.java: ``` public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText result; private Button button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9; private Button button_add, button_subtract, button_multiply, button_divide, button_clear, button_equal; private boolean isAddition, isSubtraction, isMultiplication, isDivision; private float value1, value2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = findViewById(R.id.result); button_0 = findViewById(R.id.button_0); button_1 = findViewById(R.id.button_1); button_2 = findViewById(R.id.button_2); button_3 = findViewById(R.id.button_3); button_4 = findViewById(R.id.button_4); button_5 = findViewById(R.id.button_5); button_6 = findViewById(R.id.button_6); button_7 = findViewById(R.id.button_7); button_8 = findViewById(R.id.button_8); button_9 = findViewById(R.id.button_9); button_add = findViewById(R.id.button_add); button_subtract = findViewById(R.id.button_subtract); button_multiply = findViewById(R.id.button_multiply); button_divide = findViewById(R.id.button_divide); button_clear = findViewById(R.id.button_clear); button_equal = findViewById(R.id.button_equal); button_0.setOnClickListener(this); button_1.setOnClickListener(this); button_2.setOnClickListener(this); button_3.setOnClickListener(this); button_4.setOnClickListener(this); button_5.setOnClickListener(this); button_6.setOnClickListener(this); button_7.setOnClickListener(this); button_8.setOnClickListener(this); button_9.setOnClickListener(this); button_add.setOnClickListener(this); button_subtract.setOnClickListener(this); button_multiply.setOnClickListener(this); button_divide.setOnClickListener(this); button_clear.setOnClickListener(this); button_equal.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_0: result.setText(result.getText() + "0"); break; case R.id.button_1: result.setText(result.getText() + "1"); break; case R.id.button_2: result.setText(result.getText() + "2"); break; case R.id.button_3: result.setText(result.getText() + "3"); break; case R.id.button_4: result.setText(result.getText() + "4"); break; case R.id.button_5: result.setText(result.getText() + "5"); break; case R.id.button_6: result.setText(result.getText() + "6"); break; case R.id.button_7: result.setText(result.getText() + "7"); break; case R.id.button_8: result.setText(result.getText() + "8"); break; case R.id.button_9: result.setText(result.getText() + "9"); break; case R.id.button_add: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isAddition = true; result.setText(null); } break; case R.id.button_subtract: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isSubtraction = true; result.setText(null); } break; case R.id.button_multiply: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isMultiplication = true; result.setText(null); } break; case R.id.button_divide: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isDivision = true; result.setText(null); } break; case R.id.button_clear: result.setText(""); break; case R.id.button_equal: value2 = Float.parseFloat(result.getText() + ""); if (isAddition) { result.setText(value1 + value2 + ""); isAddition = false; } if (isSubtraction) { result.setText(value1 - value2 + ""); isSubtraction = false; } if (isMultiplication) { result.setText(value1 * value2 + ""); isMultiplication = false; } if (isDivision) { result.setText(value1 / value2 + ""); isDivision = false; } break; } } } ``` 这样,一个简单的计算器就完成了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值