Android实现简易计算器(页面跳转和数据传输)

一、效果图

Android实现简易计算器(页面跳转和数据传输)_第1张图片

二、代码

  1. 首页布局

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <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="第一个数:"
            android:textSize="20sp"/>
        <EditText
            android:id="@+id/num1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    LinearLayout>

    <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="第二个数:"
            android:textSize="20sp"/>
        <EditText
            android:id="@+id/num2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    LinearLayout>

    <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="计算类型:"
            android:textSize="20sp"/>
        <EditText
            android:id="@+id/calcType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@+id/selectType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择" />
    LinearLayout>

    <Button
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算"/>

LinearLayout>
  1. 首页Java
package com.lee.calc;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import java.lang.reflect.Type;

public class MainActivity extends AppCompatActivity {

    EditText num1;
    EditText num2;
    EditText calcType;
    Button selectType;
    Button result;
    int ans;
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        num1 = findViewById(R.id.num1);
        num2 = findViewById(R.id.num2);
        calcType = findViewById(R.id.calcType);
        selectType = findViewById(R.id.selectType);
        result = findViewById(R.id.result);
        selectType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TypeActivity.class);
                startActivityForResult(intent,1);
            }
        });

        result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num1 = Integer.parseInt(MainActivity.this.num1.getText().toString());
                int num2 = Integer.parseInt(MainActivity.this.num2.getText().toString());
                String type = calcType.getText().toString();
                switch(type){
                    case "加法":
                        ans = num1 + num2;
                        type = "+";
                        break;
                    case "减法":
                        ans = num1 - num2;
                        type = "-";
                        break;
                    case "乘法":
                        ans = num1 * num2;
                        type = "*";
                        break;
                    case "除法":
                        ans = num1 / num2;
                        type = "/";
                        break;
                }
                Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                intent.putExtra("num1",num1);
                intent.putExtra("num2",num2);
                intent.putExtra("ans",ans);
                intent.putExtra("type",type);
                startActivity(intent);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 1 && resultCode == 2){
            calcType.setText(data.getStringExtra("type"));
        }
    }

}

  1. 类型选择页面布局

<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"
    tools:context=".TypeActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择要计算的类型"
        android:textSize="20sp"/>

    <RadioGroup
        android:id="@+id/typeGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp"/>
        <RadioButton
            android:id="@+id/sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp"/>
        <RadioButton
            android:id="@+id/mul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp"/>
        <RadioButton
            android:id="@+id/div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp"/>
    RadioGroup>

    <Button
        android:id="@+id/confirmType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"
        android:textSize="20sp"/>

LinearLayout>
  1. 类型选择页面Java
package com.lee.calc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class TypeActivity extends AppCompatActivity {

    RadioButton add;
    RadioButton sub;
    RadioButton mul;
    RadioButton div;
    RadioGroup typeGroup;
    Button confirmType;
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type);
        typeGroup = findViewById(R.id.typeGroup);
        add = findViewById(R.id.add);
        sub = findViewById(R.id.sub);
        mul = findViewById(R.id.mul);
        div = findViewById(R.id.div);
        confirmType = findViewById(R.id.confirmType);
        confirmType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(add.isChecked()){
                    type = "加法";
                }
                if(sub.isChecked()){
                    type = "减法";
                }
                if(mul.isChecked()){
                    type = "乘法";
                }
                if(div.isChecked()){
                    type = "除法";
                }
                System.out.println(type);
                Intent intent = new Intent();
                intent.putExtra("type",type);
                setResult(2,intent);
                finish();
            }
        });

    }
}

  1. 结果页布局

<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"
    tools:context=".ResultActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/resultShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"/>

LinearLayout>
  1. 结果页Java
package com.lee.calc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;

public class ResultActivity extends AppCompatActivity {

    EditText resultShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        resultShow = findViewById(R.id.resultShow);
        Intent intent = getIntent();
        int num1 = intent.getIntExtra("num1", 0);
        int num2 = intent.getIntExtra("num2", 0);
        int ans = intent.getIntExtra("ans", 0);
        String type = intent.getStringExtra("type");
        String result = num1 + " " + type + " " + num2 + "=" + ans;
        resultShow.setText(result);
    }
}

你可能感兴趣的:(Android)