Android计算器源码分享

今天给大家带来自己用Android开发的计算器小程序,话不多说先把代码奉上。

布局效果。。。
有点丑,别见怪~~
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:layout_marginBottom="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="40dp"
    android:background="@drawable/bj"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:id="@+id/gongjulan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gongjulan_shape" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:layout_marginLeft="7dp"
            android:background="@null"
            android:text="查看(V)"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:layout_marginLeft="25dp"
            android:layout_toRightOf="@+id/btn1"
            android:background="@null"
            android:text="编辑(E)"
            android:textSize="12sp" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:layout_marginLeft="25dp"
            android:layout_toRightOf="@+id/btn2"
            android:background="@null"
            android:text="帮助(H)"
            android:textSize="12sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/gongjulan"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/textview_shape"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tx1"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginLeft="9dp"
            android:layout_marginRight="9dp"
            android:layout_marginTop="10dp"
            android:gravity="right" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="9dp"
            android:layout_marginRight="9dp"
            android:background="@drawable/text1_shape" />

        <TextView
            android:id="@+id/tx2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="9dp"
            android:layout_marginRight="9dp"
            android:gravity="right|center_vertical"
            android:text="0"
            android:textSize="50sp" />
    </LinearLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginTop="180dp"
        android:background="@drawable/anjianbeijing_shape"
        android:columnCount="5"
        android:paddingLeft="7dp"
        android:paddingRight="9dp"
        android:rowCount="6" >

        <Button
            android:id="@+id/houtui"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="←"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/guiling"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="CE"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="C"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="±"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="√"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b7"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="7"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b8"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="8"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b9"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="9"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/chu"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="/"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="%"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b4"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="4"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b5"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="5"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b6"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="6"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/cheng"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="*"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="1/x"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b1"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="1"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b2"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="2"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b3"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="3"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/jian"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="-"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/deng"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:layout_gravity="fill_vertical"
            android:layout_rowSpan="2"
            android:background="@drawable/btn_selector"
            android:text="="
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/b0"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:layout_columnSpan="2"
            android:layout_gravity="fill_horizontal"
            android:background="@drawable/btn_selector"
            android:text="0"
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/dian"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="."
            android:textColor="#ffffff"
            android:textSize="25sp" />

        <Button
            android:id="@+id/jia"
            android:layout_width="63dp"
            android:layout_height="63dp"
            android:background="@drawable/btn_selector"
            android:text="+"
            android:textColor="#ffffff"
            android:textSize="25sp" />
    </GridLayout>

</RelativeLayout>

java代码。。。

package com.ao.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


    private int fuhao;             //记录输入的符号
    private TextView tv1;          //屏幕上一层的文本框
    private TextView tv2;          //屏幕下一层的文本框
    private StringBuilder sb;      //存储按钮输入的数据
    private String string;         //数据中转变量
    private boolean flag = true;   //检测小数点输入是否合法

    //屏幕上一层的文本处理。------------------------------------->
    private String text1Chuli() {

        if(tv1.getText().length()>0 && tv2.getText().length()>0 &&      tv2.getText()!="0"){
            string = dateChuLi(new StringBuilder(MainActivity.this.dengYu()+""));
        }else if(tv1.getText().length() == 0){
            string = tv2.getText().toString();
        }
        return string;

    }

    //文本缩放,超过屏幕自动缩小字体。----------------------------->
    private void suoFang(String string) {

        if(string.length() > 14){
            tv2.setTextSize(25.0f);
        }
        if(string.length() <= 14){
            tv2.setTextSize(50.0f);
        }
    }

    // 数据处理中心   将以“.0”结尾的 浮点数 转换为整数。---------------->
    private String dateChuLi(StringBuilder sbBuilder) {

        String str = sbBuilder.toString().substring(sbBuilder.length()-2,sbBuilder.length());
        if(str.equals(".0")){
            sbBuilder.delete(sbBuilder.length()-2, sbBuilder.length());
        }
        return sbBuilder.toString();
    }

    //运算中心。--------------------------------------------->
    private double yunSuan(double a , double b) {
        double sum = 0;
        switch (fuhao) {
        case 1:
            sum = a+b;
            break;
        case 2:
            sum = a-b;
            break;
        case 3:
            sum = a*b;
            break;
        case 4:
            sum = a/b;
            break;
        }
        return sum;
    }

    //数据源、结果处理中心。------------------------------------>
    private double dengYu() {

        StringBuilder s = new StringBuilder(tv1.getText().toString());
        s.delete(s.length()-3, s.length());
        double a = Double.parseDouble(s.toString());
        double b = Double.parseDouble(tv2.getText().toString());
        return this.yunSuan(a, b);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sb = new StringBuilder();

        //找出控件的对象。
        tv2 = (TextView)findViewById(R.id.tx2);
        tv1 = (TextView)findViewById(R.id.tx1);
        Button b1  = (Button)findViewById(R.id.b1);
        Button b2  = (Button)findViewById(R.id.b2);
        Button b3  = (Button)findViewById(R.id.b3);
        Button b4  = (Button)findViewById(R.id.b4);
        Button b5  = (Button)findViewById(R.id.b5);
        Button b6  = (Button)findViewById(R.id.b6);
        Button b7  = (Button)findViewById(R.id.b7);
        Button b8  = (Button)findViewById(R.id.b8);
        Button b9  = (Button)findViewById(R.id.b9);
        Button b0  = (Button)findViewById(R.id.b0);
        Button b_jia = (Button)findViewById(R.id.jia);
        Button b_jian = (Button)findViewById(R.id.jian);
        Button b_cheng = (Button)findViewById(R.id.cheng);
        Button b_chu = (Button)findViewById(R.id.chu);
        Button b_deng = (Button)findViewById(R.id.deng);
        Button b_houtui = (Button)findViewById(R.id.houtui);
        Button b_guiling = (Button)findViewById(R.id.guiling);
        Button b_dian = (Button)findViewById(R.id.dian);

        //创建对应的监听事件。
        NumBtnOnClick numBtnOnClick = new NumBtnOnClick();
        FuhaoOnClick fuhaoOnClick = new FuhaoOnClick();
        DengYuOnClick dengYuOnClick = new DengYuOnClick();
        QiTaOnClick qiTaOnClick = new QiTaOnClick();

        //为控件绑定对应的监听事件。
        b1.setOnClickListener(numBtnOnClick);
        b2.setOnClickListener(numBtnOnClick);
        b3.setOnClickListener(numBtnOnClick);
        b4.setOnClickListener(numBtnOnClick);
        b5.setOnClickListener(numBtnOnClick);
        b6.setOnClickListener(numBtnOnClick);
        b7.setOnClickListener(numBtnOnClick);
        b8.setOnClickListener(numBtnOnClick);
        b9.setOnClickListener(numBtnOnClick);
        b0.setOnClickListener(numBtnOnClick);
        b_dian.setOnClickListener(numBtnOnClick);
        b_cheng.setOnClickListener(fuhaoOnClick);
        b_chu.setOnClickListener(fuhaoOnClick);
        b_jia.setOnClickListener(fuhaoOnClick);
        b_jian.setOnClickListener(fuhaoOnClick);
        b_deng.setOnClickListener(dengYuOnClick);
        b_houtui.setOnClickListener(qiTaOnClick);
        b_guiling.setOnClickListener(qiTaOnClick);

    }

    //通过数字按钮的Text获取数字,通过StringBuilder接收数字。------->
    class NumBtnOnClick implements OnClickListener{

        @Override
        public void onClick(View v) {

            Button button = (Button)v;
            if(fuhao != 5  && !( button.getText().toString().equals(".") && sb.length() == 0 ) ){
                if(flag || !(button.getText().equals("."))){
                    sb.append(button.getText());
                    MainActivity.this.suoFang(sb.toString());
                    tv2.setText(sb);
                    string = tv2.getText().toString();
                }
                if(button.getText().equals(".")){
                    flag = false;
                }
            }
        }
    }

    //等于号的监听事件。--------------------------------------->
    class DengYuOnClick implements OnClickListener{
        @Override
        public void onClick(View v) {   

            if(!tv1.getText().toString().equals("") && !tv2.getText().toString().equals("")){
                String string = dateChuLi(new StringBuilder(MainActivity.this.dengYu()+""));
                MainActivity.this.suoFang(string);
                tv2.setText(string);
                tv1.setText("");
                string = tv2.getText().toString();
                fuhao = 5;
            }
        }
    }

    //(+,-,*,/)的监听事件,支持长算式,只支持从左到右的计算规则。---->
    class FuhaoOnClick implements OnClickListener{

        @Override
        public void onClick(View v) {

            if(string != null){
                Button button = (Button)v;
                switch (button.getText().toString()) {
                case "+":   
                    tv1.setText(MainActivity.this.text1Chuli()+"+  ");  
                    fuhao = 1;
                    break;
                case "-":
                    tv1.setText(MainActivity.this.text1Chuli()+"-  ");
                    fuhao = 2;
                    break;
                case "*":
                    tv1.setText(MainActivity.this.text1Chuli()+"*  ");      
                    fuhao = 3;
                    break;
                case "/":
                    tv1.setText(MainActivity.this.text1Chuli()+"/  ");  
                    fuhao = 4;
                    break;              
                }
                sb.delete(0, sb.length());
                tv2.setText("0");
                flag = true;

            }
        }
    }

    //其他按键处理。------------------------------------------>
    class QiTaOnClick implements OnClickListener{

        @Override
        public void onClick(View v) {

            Button button = (Button)v;
            switch (button.getText().toString()) {
            case "CE":
                string = null;
                tv1.setText("");
                sb.delete(0, sb.length());
                MainActivity.this.suoFang(sb.toString());
                tv2.setText("0");
                fuhao = 6;
                flag = true;
                break;
            case "←":
                if(fuhao == 5 || sb.length() == 0)
                    break;
                sb.delete(sb.length()-1, sb.length());
                if(!(sb.toString().contains(".")))
                    flag =true;
                MainActivity.this.suoFang(sb.toString());
                tv2.setText(sb);
                if(sb.length() == 0)
                    tv2.setText("0");
            }
        }
    }   
}

ps:代码很low,不好的地方望指点。。。

———————————————————————————————————————–Frozen 2016-12-16

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的 Android 计算器应用程序的源代码。它包括一个布局文件,一个 Java 类和一个 Android清单文件。 activity_main.xml 布局文件: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/etNum1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Enter a number" /> <EditText android:id="@+id/etNum2" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Enter another number" /> <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Result:" android:textSize="20sp" /> <Button android:id="@+id/btnAdd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add" /> <Button android:id="@+id/btnSubtract" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Subtract" /> <Button android:id="@+id/btnMultiply" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Multiply" /> <Button android:id="@+id/btnDivide" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Divide" /> </LinearLayout> ``` MainActivity.java 类文件: ```java 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 etNum1, etNum2; TextView tvResult; Button btnAdd, btnSubtract, btnMultiply, btnDivide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etNum1 = findViewById(R.id.etNum1); etNum2 = findViewById(R.id.etNum2); tvResult = findViewById(R.id.tvResult); btnAdd = findViewById(R.id.btnAdd); btnSubtract = findViewById(R.id.btnSubtract); btnMultiply = findViewById(R.id.btnMultiply); btnDivide = findViewById(R.id.btnDivide); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double num1 = Double.parseDouble(etNum1.getText().toString()); double num2 = Double.parseDouble(etNum2.getText().toString()); double result = num1 + num2; tvResult.setText("Result: " + result); } }); btnSubtract.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double num1 = Double.parseDouble(etNum1.getText().toString()); double num2 = Double.parseDouble(etNum2.getText().toString()); double result = num1 - num2; tvResult.setText("Result: " + result); } }); btnMultiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double num1 = Double.parseDouble(etNum1.getText().toString()); double num2 = Double.parseDouble(etNum2.getText().toString()); double result = num1 * num2; tvResult.setText("Result: " + result); } }); btnDivide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double num1 = Double.parseDouble(etNum1.getText().toString()); double num2 = Double.parseDouble(etNum2.getText().toString()); double result = num1 / num2; tvResult.setText("Result: " + result); } }); } } ``` AndroidManifest.xml 清单文件: ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.calculator"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ``` 这个应用程序只是一个简单的计算器,它可以执行加、减、乘、除四种基本算术运算。当用户在 EditText 中输入两个数字,并点击相应的按钮时,应用程序将执行所选的运算并显示结果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值