Android简易计算器项目Calculator

目标:制作一个简易的计算器
从输入框,输入要进行计算的整数
从单选按钮中选择要进行的运算法则
然后输出结果
如下图所示:

在这里插入图片描述
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingHorizontal="20dp"
    android:paddingVertical="50dp"
    tools:context=".MainActivity">
    <!--输入number1
         采用了LinerLayout线性布局
         TextView:提示信息“输入数字1”
         EditText:输入框(输入数字1)
         TextView和EditText水平排列
    -->
    <LinearLayout
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num1_name"
            android:textSize="25dp"
            />
        <EditText
            android:id="@+id/num1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint1_name"
            android:inputType="number"
            />
    </LinearLayout>
    <!--输入number2
        采用了LinerLayout线性布局
        TextView:提示信息“输入数字2”
        EditText:输入框(输入数字2)
        TextView和EditText水平排列
   -->
    <LinearLayout
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/num2_name"
            android:textSize="25dp"
            />
        <EditText
            android:id="@+id/num2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint2_name"
            android:inputType="number"
            />
    </LinearLayout>
    <!--选择运算符
            RadioGroup与RadioButton配合使用,实现RadioButton的单选功能
       -->
    <RadioGroup
        android:id="@+id/operator"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:textSize="25dp"
            android:layout_weight="1"
            />
        <RadioButton
            android:id="@+id/minus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:textSize="25dp"
            android:layout_weight="1"
            />
        <RadioButton
            android:id="@+id/mul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*"
            android:textSize="25dp"
            android:layout_weight="1"
            />
        <RadioButton
            android:id="@+id/div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:textSize="25dp"
            android:layout_weight="1"
            />
    </RadioGroup>
    <!--输出结果result
            采用了LinerLayout线性布局
            第一个TextView:提示信息“结果result”
            第二个TextView:显示结果result
            TextView和TextView水平排列
       -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/res_name"
            android:textSize="25dp"
            />
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            />
    </LinearLayout>
</LinearLayout>

strings.xml

<resources>
        <string name="app_name">Calculator</string>
        <string name="num1_name">number1:</string>
        <string name="num2_name">number2:</string>
        <string name="hint1_name">Enter number1</string>
        <string name="hint2_name">Enter number2</string>
        <string name="res_name">result:</string>
    </resources>

MainActivity.java

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText mNum1;//输入数字1
    EditText mNum2;//输入数字2
    TextView mResult;//结果
    RadioGroup mOperator;//运算符
    int value1 = 0;
    int value2 = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mOperator = (RadioGroup)findViewById(R.id.operator);
        mOperator.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            //判断选中的运算符符号,并进行相应的运算
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.add://如果与add单选按钮匹配
                        add();//进行加法
                        break;
                    case R.id.minus://如果与minus按钮匹配
                        minus();//进行减法
                        break;
                    case R.id.mul://如果与mul按钮匹配
                        mul();//进行乘法
                        break;
                    case R.id.div://如果与div按钮匹配
                        div();//进行除法
                        break;
                }
            }
            //进行加法运算
            private void add() {
                mResult = (TextView) findViewById(R.id.result);
                mNum1 = (EditText) findViewById(R.id.num1);
                mNum2 = (EditText) findViewById(R.id.num2);
                String p1 = mNum1.getText().toString();
                String p2 = mNum2.getText().toString();
                value1 = Integer.valueOf(p1);
                value2 = Integer.valueOf(p2);
                String s = Integer.toString(value1+value2);
                mResult.setText(s);
            }
            //进行减法运算
            private void minus() {
                mResult = (TextView) findViewById(R.id.result);
                mNum1 = (EditText) findViewById(R.id.num1);
                mNum2 = (EditText) findViewById(R.id.num2);
                String p1 = mNum1.getText().toString();
                String p2 = mNum2.getText().toString();
                value1 = Integer.valueOf(p1);
                value2 = Integer.valueOf(p2);
                String s = Integer.toString(value1-value2);
                mResult.setText(s);
            }
            //进行乘法运算
            private void mul() {
                mResult = (TextView) findViewById(R.id.result);
                mNum1 = (EditText) findViewById(R.id.num1);
                mNum2 = (EditText) findViewById(R.id.num2);
                String p1 = mNum1.getText().toString();
                String p2 = mNum2.getText().toString();
                value1 = Integer.valueOf(p1);
                value2 = Integer.valueOf(p2);
                String s = Integer.toString(value1*value2);
                mResult.setText(s);
            }
            //进行除法运算,注意除数不能为0
            private void div() {
                mResult = (TextView) findViewById(R.id.result);
                mNum1 = (EditText) findViewById(R.id.num1);
                mNum2 = (EditText) findViewById(R.id.num2);
                String p1 = mNum1.getText().toString();
                String p2 = mNum2.getText().toString();
                value1 = Integer.valueOf(p1);
                value2 = Integer.valueOf(p2);
                if(value1==0||value2==0){
                    Toast.makeText(MainActivity.this,"除数不能为0",Toast.LENGTH_SHORT).show();
                }
                else{
                    String s = Integer.toString(value1/value2);
                    mResult.setText(s);
                }
            }
        });
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值