package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
public float result; // 计算结果
TextView showResult;//显示计算的结果
Button[] number;//数字数字包含点
Button[] command;//符号数组
Button ClearButton;//操作按钮
public String lastCommand; // 用于保存运算符
public boolean clearFlag; // 用于判断是否清空显示区域的值,true需要,false不需要
public boolean firstFlag; // 用于判断是否是首次输入,true首次,false不是首次
public MainActivity(){//初始化变量
result=0;
clearFlag=false;
firstFlag=true;
lastCommand="=";}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showResult=(TextView)findViewById(R.id.result);//实例化显示计算结果TextView
number=new Button[11];
number[0]=(Button)findViewById(R.id.ling);
number[1]=(Button)findViewById(R.id.yi);
number[2]=(Button)findViewById(R.id.er);
number[3]=(Button)findViewById(R.id.san);
number[4]=(Button)findViewById(R.id.si);
number[5]=(Button)findViewById(R.id.wu);
number[6]=(Button)findViewById(R.id.liu);
number[7]=(Button)findViewById(R.id.qi);
number[8]=(Button)findViewById(R.id.ba);
number[9]=(Button)findViewById(R.id.jiu);
number[10]=(Button)findViewById(R.id.dian);
//运算符数组
command=new Button[5];
command[0]=(Button)findViewById(R.id.jia);
command[1]=(Button)findViewById(R.id.jian);
command[2]=(Button)findViewById(R.id.cheng);
command[3]=(Button)findViewById(R.id.chu);
command[4]=(Button)findViewById(R.id.deng);
//为每个数字按钮注册按钮单击事件
NumberAction na=new NumberAction();
for(Button bc:number){
bc.setOnClickListener(na);}
//为每个操作符号按钮注册按钮单击事件
CommandAction ca=new CommandAction();
for(Button bc:command){
bc.setOnClickListener(ca);}
//实例化按钮
ClearButton=(Button)findViewById(R.id.clear);
//为按钮注册单击事件
ClearButton.setOnClickListener(this);
}
public void calculate(double x){ //计算函数
if(lastCommand.equals("+"))
{
result+=x;
}
else if(lastCommand.equals("-"))
{
result-=x;
}
else if(lastCommand.equals("*"))
{
result*=x;
}
else if(lastCommand.equals("/"))
{
result/=x;
}
showResult.setText(""+result);
}
public void onClick(View v) {//按钮单击处理
switch(v.getId()){
case R.id.clear://当单击了Clear按钮后
showResult.setText("0.0");
result=0;
clearFlag=false;
firstFlag=true;
lastCommand="=";
break;
}
}
private class NumberAction implements OnClickListener{//数字事件注册类
public void onClick(View v) {
Button btn=(Button)v;
String input=btn.getText().toString();//取得单击该按钮的值
if(firstFlag){//如果是第一次输入
if(input.equals(".")){//判断如果第一次输入是".",则不做任何事
return;
}
if(showResult.getText().toString().equals("0.0")){
showResult.setText("");
}
result=Float.parseFloat(input);
firstFlag=false;
}
else{//如果不是第一次输入
String text=showResult.getText().toString();//取得先前的内容
//判断内容中是否有“.”,如果有,下一步输入的又是".",则不做什么
if(text.indexOf(".")!=-1 && input.equals(".")){
return;
}
//判断内容是不是等于"=",如果是,并且即将输入的是".",则不允许
if(text.equals("-") && input.equals(".")){
return;
}
//判断内容是不是等于"=",如果是,并且即将输入的不是".",则把输入的值赋给result,比如result=-6;
if(text.equals("-") && !input.equals(".")){
result=Float.parseFloat("-"+input);
}
//判断内容是不是等于"0",如果是,并且即将输入的不是".",则不允许
if(text.equals("0") && !input.equals(".")){
return;
}
}
if(clearFlag){
showResult.setText("");
clearFlag=false;
}
showResult.setText(showResult.getText().toString()+input);//显示内容
}
}
private class CommandAction implements OnClickListener{//符号操作等按钮的注册事件
public void onClick(View v) {
Button btn=(Button)v;
String inputCommand=(String)btn.getText();//取得按钮上的值
if(firstFlag){//判断是否是第一次输入
if(inputCommand.equals("-")){//如果是,如果输入的是"-"
showResult.setText("-");//则把showResult内容设置为"-"
firstFlag=false;
}
}
else{//如果不是第一次输入
if(!clearFlag){
calculate(Double.parseDouble(showResult.getText().toString()));
}
lastCommand=inputCommand;
clearFlag=true;
}
}
}
}
每次第一次输入一个两位数以上的数字进行计算,计算机都只识别第一位输入的数字
请问这段短代码应该怎么修改好?