Android Studio 制作简易计算器,Android面试题中高级

文章详细描述了一个Android应用中按钮和EditText的布局设置,以及如何通过onClick事件处理用户输入。展示了如何为数字按钮和运算符按钮设置背景、边距和文本属性。
摘要由CSDN通过智能技术生成

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;

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

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

写在最后

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从哪里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家

我的【Github】会分享一些关于Android进阶方面的知识,也会分享一下最新的面试题~

如果你熟练掌握GitHub中列出的知识点,相信将会大大增加你通过前两轮技术面试的几率!这些内容都供大家参考,互相学习。

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以在我的【Github】阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

外链图片转存中…(img-EPO3xZIy-1710498915137)]

写在最后

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从哪里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家

我的【Github】会分享一些关于Android进阶方面的知识,也会分享一下最新的面试题~

如果你熟练掌握GitHub中列出的知识点,相信将会大大增加你通过前两轮技术面试的几率!这些内容都供大家参考,互相学习。

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以在我的【Github】阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

[外链图片转存中…(img-fq5yCvor-1710498915137)]

[外链图片转存中…(img-Qy5aozlE-1710498915138)]

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是最新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); } }); } } ``` 希望能对您有所帮助!现在您有什么需要我帮忙回答的问题吗?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值