安卓开发的自制计算器源代码

安卓开发的自制计算器源代码
安卓开发的自制计算器源代码

这是本人自己用安卓编写的计算器,代码的优化可能有所问题,不过大家都在学习阶段,欢迎大家批评指正,还是上源码吧。

先来运行图

package com.example.nanchen.exam_1_1;

import android.content.DialogInterface;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Button btn0;

private Button btn1;

private Button btn2;

private Button btn3;

private Button btn4;

private Button btn5;

private Button btn6;

private Button btn7;

private Button btn8;

private Button btn9;

private Button btnAdd;

private Button btnSub;

private Button btnMul;

private Button btnDiv;

private Button btnDec;

private Button btnEqual;

private Button btnClear;

private Button btnBackSpace;

private TextView textView_result;

double num1 = 0,num2 = 0,result = 0;//定义存储结果和两个操作数boolean isCheckEqu = false;//检测是否点击了等于

int op = 0;//定义运算符

boolean hasDec = false;

boolean hasOp = false;

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(https://www.360docs.net/doc/4a3328178.html,yout.activity_main);

textView_result = (TextView) findViewById(R.id.result);

btn0 = (Button) findViewById(R.id.btn0);

btn1 = (Button) findViewById(R.id.btn1);

btn2 = (Button) findViewById(R.id.btn2);

btn3 = (Button) findViewById(R.id.btn3);

btn4 = (Button) findViewById(R.id.btn4);

btn5 = (Button) findViewById(R.id.btn5);

btn6 = (Button) findViewById(R.id.btn6);

btn7 = (Button) findViewById(R.id.btn7);

btn8 = (Button) findViewById(R.id.btn8);

btn9 = (Button) findViewById(R.id.btn9);

btnAdd = (Button) findViewById(R.id.btn10);

btnSub = (Button) findViewById(R.id.btn11);

btnMul = (Button) findViewById(R.id.btn12);

btnDiv = (Button) findViewById(R.id.btn13);

btnDec = (Button) findViewById(R.id.btn14);

btnEqual = (Button) findViewById(R.id.btn15);

btnClear = (Button) findViewById(R.id.clear); btnBackSpace = (Button) findViewById(R.id.backSpace); btn0.setOnClickListener(this);

btn1.setOnClickListener(this);

btn2.setOnClickListener(this);

btn3.setOnClickListener(this);

btn4.setOnClickListener(this);

btn5.setOnClickListener(this);

btn6.setOnClickListener(this);

btn7.setOnClickListener(this);

btn8.setOnClickListener(this);

btn9.setOnClickListener(this);

btnAdd.setOnClickListener(this);

btnSub.setOnClickListener(this);

btnDec.setOnClickListener(this);

btnDiv.setOnClickListener(this);

btnEqual.setOnClickListener(this);

btnMul.setOnClickListener(this);

btnClear.setOnClickListener(this);

btnBackSpace.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.btn0:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString0 = textView_result.getText().toString(); if (myString0.equals("0")){

myString0 = "";

}

myString0 += "0";

textView_result.setText(myString0);

break;

case R.id.btn1:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString1 = textView_result.getText().toString(); if (myString1.equals("0")){

myString1 = "";

}

myString1 += "1";

textView_result.setText(myString1);

break;

case R.id.btn2:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString2 = textView_result.getText().toString(); if (myString2.equals("0")){

myString2 = "";

}

myString2 += "2";

textView_result.setText(myString2);

break;

case R.id.btn3:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString3 = textView_result.getText().toString(); if (myString3.equals("0")){

myString3 = "";

}

myString3 += "3";

textView_result.setText(myString3);

break;

case R.id.btn4:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString4 = textView_result.getText().toString(); if (myString4.equals("0")){

myString4 = "";

}

myString4 += "4";

textView_result.setText(myString4);

break;

case R.id.btn5:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString5 = textView_result.getText().toString(); if (myString5.equals("0")){

myString5 = "";

}

myString5 += "5";

textView_result.setText(myString5);

break;

case R.id.btn6:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString6 = textView_result.getText().toString(); if (myString6.equals("0")){

myString6 = "";

}

myString6 += "6";

textView_result.setText(myString6);

break;

case R.id.btn7:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString7 = textView_result.getText().toString(); if (myString7.equals("0")){

myString7 = "";

}

myString7 += "7";

textView_result.setText(myString7);

break;

case R.id.btn8:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString8 = textView_result.getText().toString(); if (myString8.equals("0")){

myString8 = "";

}

myString8 += "8";

textView_result.setText(myString8);

break;

case R.id.btn9:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

String myString9 = textView_result.getText().toString(); if (myString9.equals("0")){

myString9 = "";

}

myString9 += "9";

textView_result.setText(myString9);

break;

case R.id.btn10:

if (hasOp){

doEqual();//显示出来当前的答案

}

String myStringAdd = textView_result.getText().toString(); if(myStringAdd.equals(null)){

return;

}

num1 = Double.valueOf(myStringAdd);

textView_result.setText(null);// 这里被清除了

op = 1;

isCheckEqu = false;

hasDec = false;

hasOp = true;

break;

case R.id.btn11:

if (hasOp){

doEqual();

}

String myStringSub = textView_result.getText().toString(); if(myStringSub.equals(null)){

return;

}

num1 = Double.valueOf(myStringSub);

textView_result.setText(null);

op = 2;

isCheckEqu = false;

hasDec = false;

hasOp = true;

break;

case R.id.btn12:

if (hasOp){

doEqual();

}

String myStringMul = textView_result.getText().toString(); if(myStringMul.equals(null)){

return;

}

num1 = Double.valueOf(myStringMul);

textView_result.setText(null);

op = 3;

isCheckEqu = false;

hasDec = false;

hasOp = true;

break;

case R.id.btn13:

if (hasOp){

doEqual();

}

String myStringDiv = textView_result.getText().toString(); if(myStringDiv.equals(null)){

return;

}

num1 = Double.valueOf(myStringDiv);

textView_result.setText(null);

op = 4;

hasDec = false;

isCheckEqu = false;

hasOp = true;

break;

case R.id.btn14:

if(isCheckEqu){

textView_result.setText(null);

isCheckEqu=false;

}

if(hasDec){//如果已经有小数点了,则弹出一个对话框

AlertDialog.Builder builder = new

AlertDialog.Builder(MainActivity.this);

builder.setTitle("警告");

builder.setMessage("输入错误,一个数字不能有两个小数点!");

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

});

builder.show();

return;

}

hasDec = true;

String myString14 = textView_result.getText().toString();

myString14 += ".";

textView_result.setText(myString14);

break;

case R.id.btn15:

doEqual();

hasOp = false;

break;

case R.id.backSpace:

String myString = textView_result.getText().toString();

try {

textView_result.setText(myString.substring(0,myString.length() -1));

}catch (Exception e){

textView_result.setText("0");

}

break;

case R.id.clear:

textView_result.setText("0");

break;

default:

break;

}

}

/**

* 按下等于

*/

private void doEqual(){

String myStringEqu=textView_result.getText().toString(); if(myStringEqu.equals(null))

{

return;

}

num2=Double.valueOf(myStringEqu);

textView_result.setText(null);

switch (op) {

case 0:

result=num2;

break;

case 1:

result=num1+num2;

break;

case 2:

result=num1-num2;

break;

case 3:

result=num1*num2;

break;

case 4:

result=num1/num2;

break;

default:

result=0;

break;

}

textView_result.setText(String.valueOf(result)); isCheckEqu=true;

}

}

xmlns:android="https://www.360docs.net/doc/4a3328178.html,/apk/res/android" xmlns:tools="https://www.360docs.net/doc/4a3328178.html,/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.nanchen.exam_1_1.MainActivity">

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

android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/result"

android:gravity="end"

android:textSize="40sp"

android:text="@string/zero"/>

android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1">

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content" android:text="@string/clear" android:id="@+id/clear"

android:textAllCaps="false" android:gravity="center"

android:textSize="25sp"/>

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content" android:text="@string/backspace" android:id="@+id/backSpace" android:textAllCaps="false" android:gravity="center"

android:textSize="25sp"

/>

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

android:layout_width="0dp"

android:text="@string/seven" android:id="@+id/btn7"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/eight" android:id="@+id/btn8"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/nine" android:id="@+id/btn9"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/del"

android:id="@+id/btn13"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="match_parent"

android:layout_weight="1">

android:layout_width="0dp"

android:text="@string/four" android:id="@+id/btn4"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/five" android:id="@+id/btn5"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/six"

android:id="@+id/btn6"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/mul"

android:id="@+id/btn12"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

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

android:text="@string/one"

android:textSize="30sp"

android:id="@+id/btn1"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/two"

android:id="@+id/btn2"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/three" android:id="@+id/btn3"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/sub"

android:id="@+id/btn11"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

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

android:layout_width="0dp"

android:text="@string/zero1" android:id="@+id/btn0"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/dec"

android:id="@+id/btn14"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/equal" android:id="@+id/btn15"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_width="0dp"

android:text="@string/add"

android:id="@+id/btn10"

android:textSize="30sp"

android:gravity="center"

android:layout_height="wrap_content" android:layout_weight="1"/>

android:layout_weight="4"

android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp">

android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"

android:text="@string/string1"/>

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/string2" android:textSize="20sp"/>

相关主题
相关文档
最新文档