android studio 计算器代码,Android Studio实现简易计算器

如果是制作简易计算器的话是基本没有难点的,供大家参考,具体内容如下

步骤是先写好界面布局,将按钮的布局、字号颜色啥的做好,再就是设置监听器。

使用了NoTitleBar的主题

代码如下:

activity_main里关于界面的代码:

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"

android:orientation="vertical"

tools:context=".MainActivity"

>

android:id="@+id/et_input"

android:layout_width="fill_parent"

android:layout_height="90dp"

android:background="@drawable/white_bg"

android:editable="false"

android:gravity="right|bottom"

android:paddingBottom="20dp"

android:paddingRight="20dp"

android:textSize="50sp" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:orientation="horizontal"

android:gravity="center_horizontal"

>

android:id="@+id/bt_clr"

android:layout_width="80dp"

android:layout_height="80dp"

android:text="C"

android:gravity="right|bottom"

android:textSize="30sp"

android:background="@drawable/white_selector"

android:paddingRight="15sp"

android:paddingBottom="15sp"

/>

android:id="@+id/bt_del"

android:layout_width="80dp"

android:layout_height="80dp"

android:text="DEL"

android:gravity="right|bottom"

android:textSize="30sp"

android:layout_marginLeft="10dp"

android:background="@drawable/white_selector"

android:paddingRight="15sp"

android:paddingBottom="15sp"

/>

android:id="@+id/bt_div"

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"

/>

android:id="@+id/bt_mul"

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"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:orientation="horizontal"

android:gravity="center_horizontal"

>

android:id="@+id/bt_7"

android:layout_width="80dp"

android:layout_height="80dp"

android:text="7"

android:gravity="right|bottom"

android:textSize="30sp"

android:background="@drawable/white_selector"

android:paddingRight="15sp"

android:paddingBottom="15sp"

/>

android:id="@+id/bt_8"

android:layout_width="80dp"

android:layout_height="80dp"

android:text="8"

android:gravity="right|bottom"

android:textSize="30sp"

android:layout_marginLeft="10dp"

android:background="@drawable/white_selector"

android:paddingRight="15sp"

android:paddingBottom="15sp"

/>

android:id="@+id/bt_9"

android:layout_width="80dp"

android:layout_height="80dp"

android:text="9"

android:textSize="30sp"

android:gravity="right|bottom"

android:layout_marginLeft="10dp"

android:background="@drawable/white_selector"

android:paddingRight="15sp"

android:paddingBottom="15sp"

/>

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"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:orientation="horizontal"

android:gravity="center_horizontal"

>

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"

/>

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"

/>

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"

/>

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"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginTop="10dp"

android:gravity="center_horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

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"

/>

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"

/>

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"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginTop="10dp">

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"

/>

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"

/>

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;

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

String s1=exp.substring(0,exp.indexOf(" "));

//截取的运算符

String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);

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

String s2=exp.substring(exp.indexOf(" ")+3);

double cnt=0;

if(!s1.equals("")&&!s2.equals("")){

double d1=Double.parseDouble(s1);

double d2=Double.parseDouble(s2);

if(op.equals("+")){

cnt=d1+d2;

}

if(op.equals("-")){

cnt=d1-d2;

}

if(op.equals("×")){

cnt=d1*d2;

}

if(op.equals("÷")){

if(d2==0) cnt=0;

else cnt=d1/d2;

}

if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {

int res = (int) cnt;

et_input.setText(res+"");

}else {

et_input.setText(cnt+"");}

}

//s1不为空但s2为空

else if(!s1.equals("")&&s2.equals("")){

double d1=Double.parseDouble(s1);

if(op.equals("+")){

cnt=d1;

}

if(op.equals("-")){

cnt=d1;

}

if(op.equals("×")){

cnt=0;

}

if(op.equals("÷")){

cnt=0;

}

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

int res = (int) cnt;

et_input.setText(res+"");

}else {

et_input.setText(cnt+"");}

}

//s1是空但s2不是空

else if(s1.equals("")&&!s2.equals("")){

double d2=Double.parseDouble(s2);

if(op.equals("+")){

cnt=d2;

}

if(op.equals("-")){

cnt=0-d2;

}

if(op.equals("×")){

cnt=0;

}

if(op.equals("÷")){

cnt=0;

}

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

int res = (int) cnt;

et_input.setText(res+"");

}else {

et_input.setText(cnt+"");}

}

else {

et_input.setText("");

}

}

}

关于计算器的精彩文章请查看《计算器专题》 ,更多精彩等你来发现!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio是一款用于开发Android应用程序的集成开发环境(IDE)。下面是一个简单的Android Studio计算器代码的示例: 1. 首先,在Android Studio中创建一个新的项目,并设置项目名称和其他相关信息。 2. 在项目的布局文件(activity_main.xml)中,添加一个TextView用于显示计算结果,以及一组Button用于输入数字和操作符。 3. 在MainActivity.java文件中,编写以下代码: ```java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView resultTextView; private String currentNumber = ""; private double result = 0; private String operator = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resultTextView = findViewById(R.id.resultTextView); Button button0 = findViewById(R.id.button0); Button button1 = findViewById(R.id.button1); // 添加其他数字按钮的引用 Button buttonPlus = findViewById(R.id.buttonPlus); Button buttonMinus = findViewById(R.id.buttonMinus); // 添加其他操作符按钮的引用 Button buttonEquals = findViewById(R.id.buttonEquals); Button buttonClear = findViewById(R.id.buttonClear); button0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentNumber += "0"; resultTextView.setText(currentNumber); } }); // 添加其他数字按钮的点击事件监听器 buttonPlus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { performOperation("+"); } }); // 添加其他操作符按钮的点击事件监听器 buttonEquals.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { calculateResult(); } }); buttonClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearCalculator(); } }); } private void performOperation(String op) { if (!currentNumber.equals("")) { double number = Double.parseDouble(currentNumber); if (result == 0) { result = number; } else { if (operator.equals("+")) { result += number; } else if (operator.equals("-")) { result -= number; } // 添加其他操作符的逻辑 } operator = op; currentNumber = ""; } } private void calculateResult() { if (!currentNumber.equals("")) { double number = Double.parseDouble(currentNumber); if (operator.equals("+")) { result += number; } else if (operator.equals("-")) { result -= number; } // 添加其他操作符的逻辑 resultTextView.setText(String.valueOf(result)); currentNumber = String.valueOf(result); result = 0; } } private void clearCalculator() { currentNumber = ""; result = 0; operator = ""; resultTextView.setText(""); } } ``` 这是一个简单的计算器代码示例,它可以处理加法和减法操作。你可以根据需要添加其他操作符和相应的逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值