package com.example.demo;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText et;;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn7;
Button btn8;
Button btn9;
Button btn0;
Button btn_plus;
Button btn_minus;
Button btn_multiply;
Button btn_devide;
Button btn_equal;
Button btn_point;
Button btn_clear;
Button btn_del;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.editText1);
btn1=(Button) findViewById(R.id.button1);
btn2=(Button) findViewById(R.id.button2);
btn3=(Button) findViewById(R.id.button3);
btn4=(Button) findViewById(R.id.button4);
btn5=(Button) findViewById(R.id.button5);
btn6=(Button) findViewById(R.id.button6);
btn7=(Button) findViewById(R.id.button7);
btn8=(Button) findViewById(R.id.button8);
btn9=(Button) findViewById(R.id.button9);
btn0=(Button) findViewById(R.id.button0);
btn_point=(Button) findViewById(R.id.button_point);
btn_clear=(Button) findViewById(R.id.button_c);
btn_del=(Button) findViewById(R.id.button_del);
btn_devide=(Button) findViewById(R.id.button_devide);
btn_equal=(Button) findViewById(R.id.button_equal);
btn_minus=(Button) findViewById(R.id.button_minus);
btn_multiply=(Button) findViewById(R.id.button_multiply);
btn_plus=(Button) findViewById(R.id.button_plus);
boolean clear_flag=false;
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);
btn_plus.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_devide.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str=et.getText().toString();
switch(v.getId()) {
case R.id.button0:
case R.id.button1:
case R.id.button2:
case R.id.button3:
case R.id.button4:
case R.id.button5:
case R.id.button6:
case R.id.button7:
case R.id.button8:
case R.id.button9:
case R.id.button_point:
et.setText(str+((Button)v).getText());
break;
case R.id.button_plus:
case R.id.button_minus:
case R.id.button_multiply:
case R.id.button_devide:
et.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.button_del:
if(et.getText().toString()!=null&&et.getText().toString()!=""){
et.setText(str.substring(0, str.length()-1));
}
break;
case R.id.button_c:
et.setText("");
break;
case R.id.button_equal:
getResult();
break;
}
}
private void getResult(){
double result=0;
String edt=et.getText().toString();
if(edt==null||edt==""){
return;
}
if(!edt.contains(" ")){
return;
}
String s1=edt.substring(0,edt.indexOf(" "));
String op=edt.substring(edt.indexOf(" ")+1,edt.indexOf(" ")+2);
String s2=edt.substring(edt.indexOf(" ")+3);
if(s1!=""&&s2!=""){
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d1+d2;
}
else if(op.equals("-")){
result=d1-d2;
}
else if(op.equals("×")){
result=d1*d2;
}
else if(op.equals("÷")){
if(d2==0){
result=0;
}else {
result=d1/d2;
}
}
et.setText(result+"");
}
else if(s1!=""&&s2==""){
et.setText(edt);
}
else if(s1==""&&s2!=""){
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d2;
}
else if(op.equals("-")){
result=0-d2;
}
else if(op.equals("×")){
result=0;
}
else if(op.equals("÷")){
result=0;
}
et.setText(result+"");
}
}
}
估計是substring問題,范圍大了,截取不到