android计算器加减乘除,【03-21求助】写一个简易计算器的安卓app,一按加减乘除就退出...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package com.example.ag;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity

{

private Button bt_1;

private Button bt_2;

private Button bt_3;

private Button bt_4;

private Button bt_5;

private Button bt_6;

private Button bt_7;

private Button bt_8;

private Button bt_9;

private Button bt_0;

private Button bt_add;

private Button bt_sub; // 减

private Button bt_multiply; // 乘

private Button bt_divide; // 除

private Button bt_back;

private Button bt_equal; // 等于

private Button bt_point; // 点

private Button bt_clear; // 清除

private Button bt_exit;

private EditText et_play; // 显示

private int op= 0; // 运算符

private StringBuffer str_display = new StringBuffer();// 显示

private int i = 0;

private double num1=0;

private boolean flag = true; // 小数点个数开关控制;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

bt_0 = (Button) findViewById(R.id.bt0);

bt_1 = (Button) findViewById(R.id.bt1);

bt_2 = (Button) findViewById(R.id.bt2);

bt_3 = (Button) findViewById(R.id.bt3);

bt_4 = (Button) findViewById(R.id.bt4);

bt_5 = (Button) findViewById(R.id.bt5);

bt_6 = (Button) findViewById(R.id.bt6);

bt_7 = (Button) findViewById(R.id.bt7);

bt_8 = (Button) findViewById(R.id.bt8);

bt_9 = (Button) findViewById(R.id.bt9);

bt_add = (Button) findViewById(R.id.btjia);

bt_sub = (Button) findViewById(R.id.btjian);

bt_multiply = (Button) findViewById(R.id.btcheng);

bt_divide = (Button) findViewById(R.id.btchu);

bt_back = (Button) findViewById(R.id.btdel);

bt_equal = (Button) findViewById(R.id.btdeng);

bt_point = (Button) findViewById(R.id.btdian);

bt_clear = (Button) findViewById(R.id.btce);

bt_exit = (Button) findViewById(R.id.btexit);

et_play = (EditText) findViewById(R.id.et);

et_play.setText("0");

}

public void btexitClick(View view){

System.exit(0);

}

public void bt0Click(View v)

{

str_display.append("0");

et_play.setText(str_display.toString());

}

public void bt1Click(View v)

{

str_display.append("1");

et_play.setText(str_display.toString());

}

public void bt2Click(View v)

{

str_display.append("2");

et_play.setText(str_display.toString());

}

public void bt3Click(View v)

{

str_display.append("3");

et_play.setText(str_display.toString());

}

public void bt4Click(View v)

{

str_display.append("4");

et_play.setText(str_display.toString());

}

public void bt5Click(View v)

{

str_display.append("5");

et_play.setText(str_display.toString());

}

public void bt6Click(View v)

{

str_display.append("6");

et_play.setText(str_display.toString());

}

public void bt7Click(View v)

{

str_display.append("7");

et_play.setText(str_display.toString());

}

public void bt8Click(View v)

{

str_display.append("8");

et_play.setText(str_display.toString());

}

public void bt9Click(View v)

{

str_display.append("9");

et_play.setText(str_display.toString());

}

public void btdianClick(View v)

{

if (flag)

{

str_display.append(".");

flag = false;

}

}

public void btdelClick(View v)

{

if (str_display.length() != 0)

{

str_display.deleteCharAt(str_display.length() - 1);

et_play.setText(str_display.toString());

}

}

public void btjiaClick(View v)

{

if (i==0){

num1 = Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op=1;

i++;

}

if (i!=0){

switch (op){

case 1:num1=num1 + Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 1;

break;

case 2:num1=num1 - Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 1;

break;

case 3:num1=num1 * Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 1;

break;

case 4:if(Double.parseDouble(str_display.toString())==0){

Toast.makeText(MainActivity.this,

"除数不能为0!", Toast.LENGTH_LONG).show();

str_display = new StringBuffer("");

op = 0;

num1=0;

flag = true;

}

else{

num1=num1 / Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 1;

}

break;

}

}

et_play.setText(String.valueOf(num1));

flag = true;

}

public void btjianClick(View v)

{

if (i==0){

num1 = Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op=2;

i++;

}

if (i!=0){

switch (op){

case 1:num1=num1 + Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 2;

break;

case 2:num1=num1 - Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 2;

break;

case 3:num1=num1 * Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 2;

break;

case 4:if(Double.parseDouble(str_display.toString())==0){

Toast.makeText(MainActivity.this,

"除数不能为0!", Toast.LENGTH_LONG).show();

str_display = new StringBuffer("");

op = 0;

num1=0;

flag = true;

}

else{

num1=num1 / Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 2;

}

break;

}

}

et_play.setText(String.valueOf(num1));

flag = true;

}

public void btchengClick(View v)

{

if (i==0){

num1 = Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op=3;

i++;

}

if (i!=0){

switch (op){

case 1:num1=num1 + Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 3;

break;

case 2:num1=num1 - Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 3;

break;

case 3:num1=num1 * Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 3;

break;

case 4:if(Double.parseDouble(str_display.toString())==0){

Toast.makeText(MainActivity.this,

"除数不能为0!", Toast.LENGTH_LONG).show();

str_display = new StringBuffer("");

op = 0;

num1=0;

flag = true;

}

else{

num1=num1 / Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 3;

}

break;

}

}

et_play.setText(String.valueOf(num1));

flag = true;

}

public void btchuClick(View v)

{

if (i == 0){

num1 = Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op=4;

i++;

}

if (i!=0){

switch (op){

case 1:num1=num1 + Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 4;

break;

case 2:num1=num1 - Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 4;

break;

case 3:num1=num1 * Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 4;

break;

case 4:if(Double.parseDouble(str_display.toString())==0){

Toast.makeText(MainActivity.this,

"除数不能为0!", Toast.LENGTH_LONG).show();

str_display = new StringBuffer("");

op = 0;

num1=0;

flag = true;

}

else{

num1=num1 / Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op = 4;

}

break;

}

}

et_play.setText(String.valueOf(num1));

flag = true;

}

public void btceClick(View v)

{

str_display = new StringBuffer("");

op=0;

num1 = 0;

i = 0;

flag = true;

}

public void btdengClick(View v)

{

if(op==0)

{

return;

}

if (i==0){

num1 = Double.parseDouble(str_display.toString());

str_display = new StringBuffer("");

op=0;

et_play.setText(String.valueOf(num1));

}

if (i!=0){

switch (op){

case 1:num1=num1 + Double.parseDouble(str_display.toString());

et_play.setText(String.valueOf(num1));

num1 = 0;

i= 0 ;

str_display = new StringBuffer("");

op = 0;

flag = true;

break;

case 2:num1=num1 - Double.parseDouble(str_display.toString());

et_play.setText(String.valueOf(num1));

num1 = 0;

i= 0 ;

str_display = new StringBuffer("");

op = 0;

flag = true;

break;

case 3:num1=num1 * Double.parseDouble(str_display.toString());

et_play.setText(String.valueOf(num1));

num1 = 0;

i= 0 ;

str_display = new StringBuffer("");

op = 0;

flag = true;

break;

case 4:if(Double.parseDouble(str_display.toString())==0){

Toast.makeText(MainActivity.this,

"除数不能为0!", Toast.LENGTH_LONG).show();

}

else{

num1=num1 / Double.parseDouble(str_display.toString());

}

et_play.setText(String.valueOf(num1));

num1 = 0;

i= 0 ;

str_display = new StringBuffer("");

op = 0;

flag = true;

break;

}

}

}

}

布局文件

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#000000">

android:id="@+id/et"

android:layout_height="200px"

android:singleLine="true"

android:focusable="false"

android:gravity="right|center"

android:text=""

android:textSize="50px"

android:textColor="#FFFFFF"

/>

android:onClick="btceClick"

android:id="@+id/btce"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="40px"

android:padding="10px"

android:text="CE"

android:textColor="#FFFFFF"

android:layout_weight="1.6"

/>

android:onClick="btdelClick"

android:id="@+id/btdel"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="40px"

android:padding="10px"

android:text="Del"

android:textColor="#FFFFFF"

android:layout_weight="1.5"

/>

android:onClick="btexitClick"

android:id="@+id/btexit"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="40px"

android:padding="10px"

android:text="exit"

android:textColor="#FFFFFF"

android:layout_weight="0.75"

/>

android:onClick="bt7Click"

android:id="@+id/bt7"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="7"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt8Click"

android:id="@+id/bt8"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="8"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt9Click"

android:id="@+id/bt9"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="9"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="btjiaClick"

android:id="@+id/btjia"

android:layout_width="match_parent"

android:layout_height="150px"

android:textSize="50px"

android:text="+"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt4Click"

android:id="@+id/bt4"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="4"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt5Click"

android:id="@+id/bt5"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="5"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt6Click"

android:id="@+id/bt6"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="6"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="btjianClick"

android:id="@+id/btjian"

android:layout_width="match_parent"

android:layout_height="150px"

android:textSize="50px"

android:text="-"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt1Click"

android:id="@+id/bt1"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="1"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt2Click"

android:id="@+id/bt2"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="2"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt3Click"

android:id="@+id/bt3"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="3"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="btchengClick"

android:id="@+id/btcheng"

android:layout_width="match_parent"

android:layout_height="150px"

android:textSize="50px"

android:text="×"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="bt0Click"

android:id="@+id/bt0"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="0"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="btdianClick"

android:id="@+id/btdian"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="."

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="btdengClick"

android:id="@+id/btdeng"

android:layout_width="wrap_content"

android:layout_height="150px"

android:textSize="50px"

android:padding="10px"

android:text="="

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

android:onClick="btchuClick"

android:id="@+id/btchu"

android:layout_width="match_parent"

android:layout_height="150px"

android:textSize="50px"

android:text="÷"

android:textColor="#FFFFFF"

android:layout_weight="1"

/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值