我在计算器应用程序中遇到了很多问题,但这两个是我的主要问题:
我的计算器在除减法之外的所有算术运算中都能正常工作。每当我执行.7-.5
之类的操作时,它都会显示.199999
我需要使用删除单个数字而不是清除整个编辑文本。
package jarvis.calc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
float ValueOne, ValueTwo;
Boolean add=false, sub=false, div=false, multi=false ,deci = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.one);
Button two = (Button) findViewById(R.id.two);
Button three = (Button) findViewById(R.id.three);
Button four = (Button) findViewById(R.id.four);
Button five = (Button) findViewById(R.id.five);
Button six = (Button) findViewById(R.id.six);
Button seven = (Button) findViewById(R.id.seven);
Button eight = (Button) findViewById(R.id.eight);
Button nine = (Button) findViewById(R.id.nine);
Button zero = (Button) findViewById(R.id.zero);
final Button decimal = (Button) findViewById(R.id.decimal);
Button plus = (Button) findViewById(R.id.plus);
Button minus = (Button) findViewById(R.id.minus);
Button multiply = (Button) findViewById(R.id.multiply);
Button divide = (Button) findViewById(R.id.divide);
Button del = (Button) findViewById(R.id.del);
Button eq = (Button) findViewById(R.id.eq);
final EditText calculation = (EditText) findViewById(R.id.calculation);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculation.setText(calculation.getText() + "0");
}
});
decimal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(calculation.getText().toString().trim().length()== 0 && deci == false) {
calculation.setText(calculation.getText() + ".");
deci = true;
}
else if (calculation.getText().toString().trim().length() > 0 && deci == false){
calculation.setText(calculation.getText() + ".");
deci = true;
}
}
});
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation.getText().toString().trim().length() > 0){
ValueOne = Float.parseFloat(calculation.getText() +"");
deci=false;
add = true;
calculation.setText(null);
}
else if(calculation == null){
calculation.setText("");
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
} else if (calculation.getText().toString().trim().length() > 0){
ValueOne = Float.parseFloat(calculation.getText() + "");
deci=false;
sub = true;
calculation.setText("");
}
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
} else if (calculation.getText().toString().trim().length() > 0){
ValueOne = Float.parseFloat(calculation.getText() + "");
deci=false;
multi = true;
calculation.setText("");
}
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation == null) {
calculation.setText("");
} else if (calculation.getText().toString().trim().length() > 0){
ValueOne = Float.parseFloat(calculation.getText() + "");
deci=false;
div = true;
calculation.setText("");
}
}
});
eq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (calculation.getText().toString().trim().length() > 0) {
ValueTwo = Float.parseFloat(calculation.getText() + "");
if (add == true) {
calculation.setText(ValueOne + ValueTwo + "");
add=false;
}
if(sub == true){
calculation.setText(ValueOne - ValueTwo + "");
sub=false;
}
if (multi == true){
calculation.setText(ValueOne*ValueTwo + "");
multi = false;
}
if (div == true){
calculation.setText(ValueOne/ValueTwo + "");
div = false;
}}
else if (calculation == null){
calculation.setText("");
}
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deci=false;
calculation.setText("");
}
});
}
@Override
public void onClick(View view) {
}
}
`