由于是初学者,我只是做了对其做了简单的处理,还有一些BUG没能处理好,当然,不影响简单运算的使用。
xml代码:
java代码:
package com.example.calculator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button one,two,three,four,five,six,seven,eight,nine,zero,multiply,minus,devide,plus,point,equal;
TextView outcome,outcome1;
String showString="",option="";
String exception="";
double showfirst=0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
zero = (Button) findViewById(R.id.zero);
devide = (Button) findViewById(R.id.devide);
multiply = (Button) findViewById(R.id.multiply);
minus = (Button) findViewById(R.id.minus);
plus = (Button) findViewById(R.id.plus);
point = (Button) findViewById(R.id.point);
equal = (Button) findViewById(R.id.equal);
outcome = (TextView) findViewById(R.id.outcome);
outcome1=(TextView) findViewById(R.id.outcome1);
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);
zero.setOnClickListener(this);
devide.setOnClickListener(this);
multiply.setOnClickListener(this);
minus.setOnClickListener(this);
plus.setOnClickListener(this);
point.setOnClickListener(this);
equal.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.add_item:
Intent intent=new Intent(MainActivity.this,second.class);
startActivity(intent);
break;
case R.id.add_remove:
Toast.makeText(this,"已经是最新版本",Toast.LENGTH_SHORT).show();
default:
}
return true;
}
@SuppressLint("ResourceType")
public void onClick(View v){
switch (v.getId()){
case R.id.zero:
showString+="0";
outcome1.setText(showString);
break;
case R.id.one:
showString+="1";
break;
case R.id.two:
showString+="2";
break;
case R.id.three:
showString+="3";
break;
case R.id.four:
showString+="4";
break;
case R.id.five:
showString+="5";
break;
case R.id.six:
showString+="6";
break;
case R.id.seven:
showString+="7";
break;
case R.id.eight:
showString+="8";
break;
case R.id.nine:
showString+="9";
break;
case R.id.plus:
if(showString.equals(""))
exception="先输入数据哦";
else{
option="+";
showfirst=Double.parseDouble(showString);
showString="";
}
break;
case R.id.minus:
if(showString.equals(""))
exception="先输入数值哦";
else{
showfirst=Double.parseDouble(showString);
showString="";
option="-";
}
break;
case R.id.multiply:
if(showString.equals(""))
exception="先输入数值哦";
else{
showfirst=Double.parseDouble(showString);
showString="";
option="*";
}
break;
case R.id.devide:
if(showString.equals(""))
exception="先输入数值哦";
else{
showfirst=Double.parseDouble(showString);
showString="";
option="/";
}
break;
case R.id.equal:
if(option.equals("+")) {
if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString = showfirst +Double.parseDouble(showString)+"";
option="";
showfirst=0;
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
else if(option.equals("-")){
if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString=showfirst-Double.parseDouble(showString)+"";
option="";
showfirst=0;
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
else if(option.equals("*")){
if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString=showfirst*Double.parseDouble(showString)+"";
option="";
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
else if(option.equals("/")){
if(showString.equals("0")){
exception="除数不能为0!";
Toast.makeText(MainActivity.this,"除数不能为0",Toast.LENGTH_SHORT).show();
}else if(showString!=""){
outcome1.setText(showfirst+option+Double.parseDouble(showString)+""+"=");
showString=showfirst/Double.parseDouble(showString)+"";
option="";
}
else{
Toast.makeText(getApplicationContext(),"请输入数字",Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.point:
showString="";
outcome.setText("");
outcome1.setText("");
break;
default:
break;
}
if(exception.equals("")) {
outcome.setText(showString);
}
else{
outcome.setText(showString);
exception="";
}
}
}
效果图