我想显示我的应用程序的完整输入和输出。
例如:
2+2=4
需要在计算器中点击“=”后显示。
我的问题是应用程序意外关闭。
package com.scientific.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
public class Calculator extends Activity{
EditText display;
Button btn,btnop,add,sub,mul,div;
ToggleButton shift;
String operator,result;
float value=0;
Character op = 'q';
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
display = (EditText)findViewById(R.id.etDisplay);
shift = (ToggleButton)findViewById(R.id.tbShift);
}
public void set_number(View v) {
// TODO Auto-generated method stub
btn = (Button)findViewById(v.getId());
result = display.getText().toString();
display.setText(result+btn.getText().toString());
}
public void btnplusclicked(View v){
perform(v);
op = '+';
}
public void btnminusclicked(View v){
perform(v);
op = '-';
}
public void btndivideclicked(View v){
perform(v);
op = '÷';
}
public void btnmulticlicked(View v){
perform(v);
op = '×';
}
public void btnequalClicked(View v){
evaluate();
}
public void perform(View v) {
// TODO Auto-generated method stub
btnop=(Button)findViewById(v.getId());
result=display.getText().toString();
operator=btnop.getText().toString();
display.setText(result+operator);
}
private void evaluate() {
// TODO Auto-generated method stub
value=Float.valueOf(display.getText().toString());
if(op == '+')
value=Float.valueOf(display.getText().toString())+value;
else if(op == '-')
value=Float.valueOf(display.getText().toString())-value;
else if(op == '/')
value=Float.valueOf(display.getText().toString())/value;
else if(op == '*')
value=Float.valueOf(display.getText().toString())*value;
display.setText(String.valueOf(value));
}
}