Android入门 简易计算器

第一个上手的安卓小项目,包含两个界面:login和calculator。
功能比较简单粗糙,目的主要是熟悉android基本结构与操作。

环境

  • Android Studio + Genymotion

演示

login

calculator

详解

布局layout文件之login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="com.example.mycalculator.Login">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="30dp"
        android:paddingTop="20dp"
        android:background="#99404348"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Welcome"
            android:textColor="#FFFFFF"
            android:textSize="18sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:background="#99000000">
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_gravity="center"
            android:layout_marginRight="50dp"
            android:layout_marginLeft="50dp"
            android:textColor="#9F9FA0"
            android:textColorHint="#9F9FA0"
            android:hint="your username"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:background="#83738F"
            android:layout_marginRight="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"></View>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_gravity="center"
            android:inputType="textPassword"
            android:hint="your password"
            android:textColor="#9F9FA0"
            android:textColorHint="#9F9FA0"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp"
        android:paddingTop="10dp"
        android:background="#99404348"
        android:gravity="center"
        android:orientation="horizontal">
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="@drawable/buttonstyle"
            android:text="Login"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="40dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/buttonstyle"
            android:layout_weight="1"
            android:text="Cancel"/>
    </LinearLayout>

</LinearLayout>

</LinearLayout>

布局layout文件之activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:background="#000" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="89dp"
        android:layout_gravity="top"
        android:layout_weight="1.2">

        <TextView
            android:id="@+id/the_first_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp" />

        <TextView
            android:id="@+id/the_option"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp" />

        <TextView
            android:id="@+id/the_second_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp" />

        <TextView
            android:id="@+id/the_equal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textSize="50dp" />

        <TextView
            android:id="@+id/the_answer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50dp" />

    </GridLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="3px"
        android:background="@color/back"></View>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/surplus"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:text="%"
            android:textColor="#99404348"
            android:textSize="30sp" />

        <Button
            android:background="@drawable/circle"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:textColor="#99404348"
            android:layout_marginBottom="5dp"
            android:id="@+id/root"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="√"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/clear"
            android:background="@drawable/circle"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="C"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/delete"
            android:background="@drawable/circle"
            android:layout_marginLeft="5dp"
            android:textColor="#99404348"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="DEL"
            android:textSize="30sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/seven"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="7"
            android:background="@drawable/circle"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/eight"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#99404348"
            android:text="8"
            android:background="@drawable/circle"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/nine"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/circle"
            android:layout_height="match_parent"
            android:textColor="#99404348"
            android:layout_weight="1"
            android:text="9"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/substraction"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/circle"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="-"
            android:textSize="30sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/four"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:textColor="#99404348"
            android:background="@drawable/circle"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            android:textSize="30sp"/>

        <Button
            android:id="@+id/five"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:text="5"
            android:textSize="30sp" />
        <Button
            android:id="@+id/six"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6"
            android:background="@drawable/circle"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="+"
            android:background="@drawable/circle"
            android:textSize="30sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/circle"
            android:text="1"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/circle"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:background="@drawable/circle"
            android:layout_weight="1"
            android:text="3"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/division"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="÷"
            android:background="@drawable/circle"
            android:textSize="30sp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:id="@+id/zero"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="0"
            android:background="@drawable/circle"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/point"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:textColor="#99404348"
            android:layout_weight="1"
            android:text="."
            android:background="@drawable/circle"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/equal"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:textColor="#99404348"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="="
            android:background="@drawable/circle"
            android:textSize="30sp"/>
        <Button
            android:id="@+id/mulitipliction"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#99404348"
            android:text="×"
            android:background="@drawable/circle"
            android:textSize="30sp"/>

    </LinearLayout>

</LinearLayout>

drawable文件中buttonstyle.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape>
            <!-- 色值 -->
            <solid android:color="#585EE0" />
            <!-- 圆角 -->
            <corners android:radius="10dp" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <!-- 色值 -->
            <solid android:color="#289BE5" />
            <!-- 圆角 -->
            <corners android:radius="10dp" />
        </shape>
    </item>
</selector>

drawable文件中circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#CEFE97"/>
    <corners android:radius="360dip"/>
    <padding
        android:left="20dp"
        android:top="10dp"
        android:right="20dp"
        android:bottom="10dp"
        />
</shape>

values文件中colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="back">#C0C0C0</color>
    <color name="button">#C75A</color>
</resources>

values文件中strings.xml

<resources>
    <string name="app_name">mycalculator</string>
</resources>

values文件中styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

Login.java

package com.example.mycalculator;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class Login extends AppCompatActivity{

    private EditText et_username;
    private EditText et_password;
    private Button bt_log;
    private Button bt_bos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //透明状态栏          
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        //通过资源标识获得控件实例
        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);
        bt_log = (Button) findViewById(R.id.login);
        bt_bos = (Button) findViewById(R.id.cancel);

        //给登录按钮注册监听器,实现监听器接口,编写事件
        bt_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入的数据
                String strUsername = et_username.getText().toString();
                String strPassword = et_password.getText().toString();

                //判断用户名和密码是否正确(为可以进行测试,将用户名和密码都定义为admin)
                if(strUsername.equals("admin") && strPassword.equals("admin")){
                    Toast.makeText(Login.this,"用户名和密码正确!",Toast.LENGTH_SHORT).show();
                    //创建一个Intent的实例
                    Intent intent = new Intent();
                    //设置从哪个activity启动哪个activity
                    intent.setClass(Login.this, MainActivity.class);
                    //启动另一个activity
                    startActivity(intent);
                }else {
                    Toast.makeText(Login.this,"用户名或密码错误!",Toast.LENGTH_SHORT).show();
                }

            }
        });
        //给取消按钮注册监听器,实现监听器接口,编写事件
        bt_bos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

MainActivity.java

package com.example.mycalculator;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button zero;
    Button one;
    Button two;
    Button three;
    Button four;
    Button five;
    Button six;
    Button seven;
    Button eight;
    Button nine;
    Button point;
    Button clear;
    Button add;
    Button substraction;
    Button mulitipliction;
    Button division;
    Button root;
    Button surplus;
    Button equal;
    Button delete;
    TextView theFirstView;
    TextView theOptionView;
    TextView theSecondView;
    TextView theEqualView;
    TextView theAnswerView;
    String option = "";
    String str1 = "", str2 = "";
    double number1, number2;
    int answerFlag = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();  //初始化布局
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zero:
            case R.id.one:
            case R.id.two:
            case R.id.three:
            case R.id.four:
            case R.id.five:
            case R.id.six:
            case R.id.seven:
            case R.id.eight:
            case R.id.nine:
                if (answerFlag == 0) {
                    if (option == "") {
                        str1 = str1 + ((Button) view).getText().toString();
                        theFirstView.setText(str1);
                    } else {
                        str2 = str2 + ((Button) view).getText().toString();
                        theSecondView.setText(str2);
                    }
                }

                if (answerFlag == 1) {
                    clear();
                    answerFlag = 0;
                    str1 = str1 + ((Button) view).getText().toString();
                    theFirstView.setText(str1);
                }

                break;

            case R.id.point:
                if (str1 != "" && option == "") {
                    str1 = str1 + ((Button) view).getText().toString();
                    theFirstView.setText(str1);
                }
                if (option != "" && str2 != "") {
                    str2 = str2 + ((Button) view).getText().toString();
                    theSecondView.setText(str2);
                } else {
                    //什么都不执行
                }
                break;

            case R.id.add:
                option = "+";
                theOptionView.setText("+");
                break;

            case R.id.substraction:
                option = "-";
                theOptionView.setText("-");
                break;

            case R.id.mulitipliction:
                option = "×";
                theOptionView.setText("×");
                break;

            case R.id.division:
                option = "÷";
                theOptionView.setText("÷");
                break;

            case R.id.surplus:
                option = "%";
                theOptionView.setText("%");
                break;

            case R.id.root:
                if (str1 == "") {
                    option = "√";
                    theOptionView.setText("√");
                }
                break;

            case R.id.clear:
                clear();
                break;

            case R.id.delete:
                if (theAnswerView.getText().toString() == "") {
                    if ((theSecondView.getText().toString()) != "") {
                        int length = str2.length();
                        if (length == 1) {
                            theSecondView.setText("");
                        } else {
                            str2 = str2.substring(0, length - 1);
                            theSecondView.setText(str2);
                        }
                        break;
                    }
                    if ((theSecondView.getText().toString()) == "" && (theOptionView.getText().toString()) != "") {
                        theOptionView.setText("");
                        break;
                    }
                    if ((theSecondView.getText().toString()) == "" && (theAnswerView.getText().toString()) == "" && (theFirstView.getText().toString()) != "") {
                        int length = str1.length();
                        if (length == 1) {
                            theFirstView.setText("");
                        } else {
                            str1 = str1.substring(0, length - 1);
                            theFirstView.setText(str1);
                        }
                        break;
                    }
                }
                break;


            case R.id.equal:
                theEqualView.setText("=");
                answerFlag = 1;

                if (option == "+") {
                    number1 = Double.parseDouble(theFirstView.getText().toString());
                    number2 = Double.parseDouble(theSecondView.getText().toString());
                    theAnswerView.setText((number1 + number2) + "");
                }
                if (option == "-") {
                    number1 = Double.parseDouble(theFirstView.getText().toString());
                    number2 = Double.parseDouble(theSecondView.getText().toString());
                    theAnswerView.setText((number1 - number2) + "");
                }
                if (option == "×") {
                    number1 = Double.parseDouble(theFirstView.getText().toString());
                    number2 = Double.parseDouble(theSecondView.getText().toString());
                    theAnswerView.setText((number1 * number2) + "");
                }
                if (option == "÷") {
                    number1 = Double.parseDouble(theFirstView.getText().toString());
                    number2 = Double.parseDouble(theSecondView.getText().toString());
                    if (number2 == 0) {
                        theAnswerView.setText("错误");
                    } else {
                        theAnswerView.setText((number1 / number2) + "");
                    }
                }
                if (option == "√") {
                    number2 = Double.parseDouble(theSecondView.getText().toString());
                    theAnswerView.setText(Math.sqrt(number2) + "");
                }
                if (option == "%") {
                    number1 = Double.parseDouble(theFirstView.getText().toString());
                    number2 = Double.parseDouble(theSecondView.getText().toString());
                    theAnswerView.setText(number1 % number2 + "");
                }
                break;

        }
    }


    public void initView() {
        zero = (Button) findViewById(R.id.zero);
        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.two);
        three = (Button) findViewById(R.id.three);
        four = (Button) findViewById(R.id.four);
        five = (Button) findViewById(R.id.five);
        six = (Button) findViewById(R.id.six);
        seven = (Button) findViewById(R.id.seven);
        eight = (Button) findViewById(R.id.eight);
        nine = (Button) findViewById(R.id.nine);
        point = (Button) findViewById(R.id.point);
        clear = (Button) findViewById(R.id.clear);
        add = (Button) findViewById(R.id.add);
        substraction = (Button) findViewById(R.id.substraction);
        mulitipliction = (Button) findViewById(R.id.mulitipliction);
        division = (Button) findViewById(R.id.division);
        root = (Button) findViewById(R.id.root);
        surplus = (Button) findViewById(R.id.surplus);
        equal = (Button) findViewById(R.id.equal);
        delete = (Button) findViewById(R.id.delete);
        theFirstView = (TextView) findViewById(R.id.the_first_number);
        theOptionView = (TextView) findViewById(R.id.the_option);
        theSecondView = (TextView) findViewById(R.id.the_second_number);
        theEqualView = (TextView) findViewById(R.id.the_equal);
        theAnswerView = (TextView) findViewById(R.id.the_answer);

        zero.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        point.setOnClickListener(this);
        clear.setOnClickListener(this);
        add.setOnClickListener(this);
        substraction.setOnClickListener(this);
        mulitipliction.setOnClickListener(this);
        division.setOnClickListener(this);
        root.setOnClickListener(this);
        surplus.setOnClickListener(this);
        equal.setOnClickListener(this);
        delete.setOnClickListener(this);
        theFirstView.setOnClickListener(this);
        theOptionView.setOnClickListener(this);
        theEqualView.setOnClickListener(this);
        theSecondView.setOnClickListener(this);
        theAnswerView.setOnClickListener(this);
    }

    public void clear() {
        str1 = "";
        str2 = "";
        theFirstView.setText("");
        theSecondView.setText("");
        theOptionView.setText("");
        theEqualView.setText("");
        theAnswerView.setText("");
        option = "";
    }
}

Manifests文件中AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mycalculator">

    <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/Theme.AppCompat.NoActionBar">
        <activity android:name=".Login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" >
        </activity>
    </application>

</manifest>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容