我一直在尝试在android studio中构建一个简单的计算器。我是初学者,只有java的基本知识。我试图接受单个文本字段中的值而不是使用两个不同的文本字段。我使用过我的操作员按钮上的点击监听器和等于按钮上的点击监听器。但是我的应用程序一旦点击操作员按钮就关闭。我如何使用单个文本视图两个接受两个或更多的数字并应用数学运算符它们?
编辑:正如我已经说过的,我对java知之甚少。在发布这个问题之前我做了谷歌搜索。
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
n.setText("");
final float a = Float.parseFloat(n.getText().toString());
n.setText("");
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float b = Float.parseFloat(n.getText().toString());
float c = a + b;
ans.setText("" + c);
n.setText("");
}
});
}
});
答案 0 :(得分:0)
在第一行,您可以:
n.setText("");
之后才用
阅读文本n.getText().toString()
返回传递给parseFloat()的空字符串。 parseFloat()不期望一个空字符串,因此抛出异常并关闭应用程序。
因此,在实际读取所需的值之前,请不要使用setText(“”)。
更一般地说,我会考虑每次点击时保存当前状态。例如,您可以在键入数字,单击操作员按钮并键入其他数字后启用等号按钮。
答案 1 :(得分:0)
这是.java文件。由于空间不足,某些按钮代码丢失了。
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (equalcheck == true) {
textViewresult.setText("");
equalcheck = false;
} else {
textViewresult.setText(textViewresult.getText() + "1");
}
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (equalcheck == true) {
textViewresult.setText("");
equalcheck = false;
} else
textViewresult.setText(textViewresult.getText() + "2");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (equalcheck == true) {
textViewresult.setText("");
equalcheck = false;
}
textViewresult.setText(textViewresult.getText() + "7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (equalcheck == true) {
textViewresult.setText("");
equalcheck = false;
}
textViewresult.setText(textViewresult.getText() + "8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (equalcheck == true) {
textViewresult.setText("");
equalcheck = false;
}
textViewresult.setText(textViewresult.getText() + "8");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//textViewresult.setText("");
textViewresult.setText(textViewresult.getText() + "0");
}
});
dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (equalcheck == true) {
textViewresult.setText("");
equalcheck = false;
}
textViewresult.setText(textViewresult.getText() + ".");
}
});
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewresult.getText() == "") {
textViewresult.setText("");
} else {
val1 = Double.parseDouble(textViewresult.getText() + "");
add = true;
textViewresult.setText(null);
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewresult.getText() == "") {
textViewresult.setText("");
} else {
val1 = Double.parseDouble(textViewresult.getText() + "");
sub = true;
textViewresult.setText(null);
}
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewresult.getText() == "") {
textViewresult.setText("");
} else {
val1 = Double.parseDouble(textViewresult.getText() + "");
mul = true;
textViewresult.setText(null);
}
}
});
division.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewresult.getText() == "") {
textViewresult.setText("");
} else {
val1 = Double.parseDouble(textViewresult.getText() + "");
div = true;
textViewresult.setText(null);
}
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewresult.getText() == "") {
textViewresult.setText("");
} else {
equalcheck = true;
val2 = Double.parseDouble(textViewresult.getText() + "");
if (add == true) {
textViewresult.setText(val1 + val2 + "");
add = false;
}
if (sub == true) {
textViewresult.setText(val1 - val2 + "");
sub = false;
}
if (mul == true) {
textViewresult.setText(val1 * val2 + "");
mul = false;
}
if (div == true) {
textViewresult.setText(val1 / val2 + "");
div = false;
}
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textViewresult.setText("");
}
});
答案 2 :(得分:0)
class Api::ApplicationController < ActionController::API
include ActionController::Cookies
...
答案 3 :(得分:0)
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||
我在这里仅使用两个数字8(n8)和9(n9)并添加(+),clear(clr),delete(dlt)按钮。添加只会使用 double type variable (双精度类型变量)添加数字,而 clear 会删除用户输入的最后一个数字并删除将删除整个字符串(全部数字或任何特殊符号)(我正在使用textView的屏幕上)。
答案 4 :(得分:0)
**
> ***SIMPLE CALCULATOR CODE***
**
public class MainActivity extends AppCompatActivity {
EditText edit;
Button add, subtract, multiply, divide, equal,clr;
TextView result;
String operator;
float temp1, temp2, sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.addition);
subtract = (Button) findViewById(R.id.subtraction);
multiply = (Button) findViewById(R.id.multiplication);
divide = (Button) findViewById(R.id.division);
equal = (Button) findViewById(R.id.equal);
result = (TextView) findViewById(R.id.result);
edit = (EditText) findViewById(R.id.user_input);
clr = (Button)findViewById(R.id.clear);
try {
//Additon
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (temp1 == 0) {
temp1 = Float.parseFloat(edit.getText().toString());
operator = "+";
edit.setText("");
} else {
edit.setText("");
}
}
});
//Division
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (temp1 == 0) {
temp1 = Float.parseFloat(edit.getText().toString());
operator = "/";
edit.setText("");
} else {
edit.setText("");
}
}
});
//Subtract
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (temp1 == 0) {
temp1 = Float.parseFloat(edit.getText().toString());
operator = "-";
edit.setText("");
} else {
edit.setText("");
}
}
});
//Multiplication
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (temp1 == 0) {
temp1 = Float.parseFloat(edit.getText().toString());
operator = "*";
edit.setText("");
} else {
edit.setText("");
}
}
});
//Clear
clr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
temp1 = 0;
temp2 = 0;
edit.setText("");
result.setText("");
}
});
//Equal
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (temp2 == 0) {
temp2 = Float.parseFloat(edit.getText().toString());
switch (operator) {
case "+":
sum = temp1 + temp2;
break;
case "-":
sum = temp1 - temp2;
break;
case "*":
sum = temp1 * temp2;
break;
case "/":
sum = temp1 / temp2;
break;
}
edit.setText(Float.toString(sum));
Toast.makeText(MainActivity.this,"RESULT IS DISPLAYED IN EDIT TEXT VIEW :", Toast.LENGTH_SHORT).show();
result.setText(Float.toString(temp1)+" "+operator+" "+Float.toString(temp2));
}
temp1 = 0;
temp2 = 0;
}
});
} catch (Exception e) {
}
}
}
答案 5 :(得分:-1)
public class MainActivity extends AppCompatActivity implements OnClickListener {
private Button btnOne, btnTwo, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15;
EditText calc;
int op1;
int op2;
String optr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOne = (Button) findViewById(R.id.btnOne);
btnTwo = (Button) findViewById(R.id.btnTwo);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btn10 = (Button) findViewById(R.id.btn10);
btn11 = (Button) findViewById(R.id.btn11);
btn12 = (Button) findViewById(R.id.btn12);
btn13 = (Button) findViewById(R.id.btn13);
btn14 = (Button) findViewById(R.id.btn14);
btn15 = (Button) findViewById(R.id.btn15);
calc = (EditText) findViewById(R.id.edit);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn10.setOnClickListener(this);
btn11.setOnClickListener(this);
btn12.setOnClickListener(this);
btn13.setOnClickListener(this);
btn14.setOnClickListener(this);
btn15.setOnClickListener(this);
}
public void operation(){
if (optr.equals("+")) {
op2 = Integer.parseInt(calc.getText().toString());
calc.setText(" ");
op1 = op1 + op2;
calc.setText("Result : " + Integer.toString(op1));
} else if (optr.equals("-")) {
op2 = Integer.parseInt(calc.getText().toString());
calc.setText(" ");
op1 = op1 - op2;
calc.setText("Result : " + Integer.toString(op1));
} else if (optr.equals("*")) {
op2 = Integer.parseInt(calc.getText().toString());
calc.setText(" ");
op1 = op1 * op2;
calc.setText("Result : " + Integer.toString(op1));
} else if (optr.equals("/")) {
op2 = Integer.parseInt(calc.getText().toString());
calc.setText(" ");
op1 = op1 * op2;
calc.setText("Result : " + Integer.toString(op1));
}
}
@Override
public void onClick(View v) {
Editable str = calc.getText();
switch (v.getId()) {
case R.id.btnOne:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btnOne.getText());
calc.setText(str);
break;
case R.id.btnTwo:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btnTwo.getText());
calc.setText(str);
break;
case R.id.btn3:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn3.getText());
calc.setText(str);
break;
case R.id.btn5:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn5.getText());
calc.setText(str);
break;
case R.id.btn6:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn6.getText());
calc.setText(str);
break;
case R.id.btn7:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn7.getText());
calc.setText(str);
break;
case R.id.btn9:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn9.getText());
calc.setText(str);
break;
case R.id.btn10:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn10.getText());
calc.setText(str);
break;
case R.id.btn11:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn11.getText());
calc.setText(str);
break;
case R.id.btn13:
if (op2 != 0) {
op2 = 0;
calc.setText("");
}
str = str.append(btn13.getText());
calc.setText(str);
break;
case R.id.btn4:
optr = "+";
if(op1 == 0){
op1 = Integer.parseInt(calc.getText().toString());
calc.setText("");
}
else if(op2 != 0){
op2 = 0;
calc.setText("");
}
else{
op2 = Integer.parseInt(calc.getText().toString());
calc.setText("");
op1 = op1 + op2;
calc.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.btn8:
optr = "-";
if(op1 == 0){
op1 = Integer.parseInt(calc.getText().toString());
calc.setText("");
}
else if(op2 != 0){
op2 = 0;
calc.setText("");
}
else{
op2 = Integer.parseInt(calc.getText().toString());
calc.setText("");
op1 = op1 - op2;
calc.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.btn12:
optr = "*";
if(op1 == 0){
op1 = Integer.parseInt(calc.getText().toString());
calc.setText("");
}
else if(op2 != 0){
op2 = 0;
calc.setText("");
}
else{
op2 = Integer.parseInt(calc.getText().toString());
calc.setText("");
op1 = op1 * op2;
calc.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.btn15:
optr = "/";
if(op1 == 0){
op1 = Integer.parseInt(calc.getText().toString());
calc.setText("");
}
else if(op2 != 0){
op2 = 0;
calc.setText("");
}
else{
op2 = Integer.parseInt(calc.getText().toString());
calc.setText("");
op1 = op1 / op2;
calc.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.btn14:
if(!optr.equals(null)) {
if (op2 != 0) {
if (optr.equals("+")) {
calc.setText("");
/*op1 = op1 + op2;*/
calc.setText("Result : " + Integer.toString(op1));
} else if (optr.equals("-")) {
calc.setText("");/*
op1 = op1 - op2;*/
calc.setText("Result : " + Integer.toString(op1));
} else if (optr.equals("*")) {
calc.setText("");/*
op1 = op1 * op2;*/
calc.setText("Result : " + Integer.toString(op1));
} else if (optr.equals("/")) {
calc.setText("");/*
op1 = op1 / op2;*/
calc.setText("Result : " + Integer.toString(op1));
}
} else {
operation();
}
}
break;
}
}
}
这不起作用请告诉我错误