计算器中的等于按钮什么都不做(Android 开发者工具)
Equals button in calculator doing nothing (Android Developer Tools)
我真的想不通,我是编程初学者,做了大约 2 个月。我为 Android 制作了计算器,但问题是等于按钮什么都不做。
设置不同的变量:
Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;
除了等号之外的所有按钮都只是将各自的文本附加到变量结果。
这里是唯一的等号按钮的代码(创建只是为了测试,但失败了):
equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String expression = String.valueOf(result);
if(expression.contains("+")){
String[] numbers = expression.split("\+");
String no1 = numbers[0];
String no2 = numbers[1];
Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;
theresult = String.valueOf(added);
result.setText(theresult);
}
}
});
我这里用了if,这样我就可以用else if来让其他操作生效了。稍后我也会使用 Try-Catch 块。感谢您的宝贵时间!
MainActivity.java
的完整代码
package org.example.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
one = (Button) findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("1");
}
});
two = (Button) findViewById(R.id.two);
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("2");
}
});
three = (Button) findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("3");
}
});
four = (Button) findViewById(R.id.four);
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("4");
}
});
five = (Button) findViewById(R.id.five);
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("5");
}
});
six = (Button) findViewById(R.id.six);
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("6");
}
});
seven = (Button) findViewById(R.id.seven);
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("7");
}
});
eight = (Button) findViewById(R.id.eight);
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("8");
}
});
nine = (Button) findViewById(R.id.nine);
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("9");
}
});
zero = (Button) findViewById(R.id.zero);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("0");
}
});
dot = (Button) findViewById(R.id.dot);
dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append(".");
}
});
clear = (ImageButton) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("");
}
});
plus = (Button) findViewById(R.id.plus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("+");
}
});
minus = (Button) findViewById(R.id.minus);
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("-");
}
});
divide = (Button) findViewById(R.id.divide);
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("/");
}
});
multiply = (Button) findViewById(R.id.multiply);
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("x");
}
});
equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.getText();
String expression = String.valueOf(result);
if(expression.contains("+")){
String[] numbers = expression.split("\+");
String no1 = numbers[0];
String no2 = numbers[1];
Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;
theresult = String.valueOf(added);
result.setText(theresult);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当您按下等号按钮时,您必须通过 result.getText() 获取文本视图的值,这样您才能获取文本视图的值并将其分配到某个字符串中。
字符串表达式 = String.valueOf(结果);
此处您只是使用 String.valueOf() 中的结果。通过调试或某种打印语句检查您是否获得了正确的值。
另外 post 您的代码 completely/reasonable 避免更多问题的代码,以便您得到答案。
就这样做:
@Override
public void onClick(View v) {
String expression = result.getText().toString();
// Log.d("LOG", expression);
if(expression.contains("+")){
(...)
也许工作。无需使用 String.valueOf()
我真的想不通,我是编程初学者,做了大约 2 个月。我为 Android 制作了计算器,但问题是等于按钮什么都不做。
设置不同的变量:
Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;
除了等号之外的所有按钮都只是将各自的文本附加到变量结果。
这里是唯一的等号按钮的代码(创建只是为了测试,但失败了):
equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String expression = String.valueOf(result);
if(expression.contains("+")){
String[] numbers = expression.split("\+");
String no1 = numbers[0];
String no2 = numbers[1];
Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;
theresult = String.valueOf(added);
result.setText(theresult);
}
}
});
我这里用了if,这样我就可以用else if来让其他操作生效了。稍后我也会使用 Try-Catch 块。感谢您的宝贵时间!
MainActivity.java
的完整代码package org.example.calc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
one = (Button) findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("1");
}
});
two = (Button) findViewById(R.id.two);
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("2");
}
});
three = (Button) findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("3");
}
});
four = (Button) findViewById(R.id.four);
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("4");
}
});
five = (Button) findViewById(R.id.five);
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("5");
}
});
six = (Button) findViewById(R.id.six);
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("6");
}
});
seven = (Button) findViewById(R.id.seven);
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("7");
}
});
eight = (Button) findViewById(R.id.eight);
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("8");
}
});
nine = (Button) findViewById(R.id.nine);
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("9");
}
});
zero = (Button) findViewById(R.id.zero);
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("0");
}
});
dot = (Button) findViewById(R.id.dot);
dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append(".");
}
});
clear = (ImageButton) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("");
}
});
plus = (Button) findViewById(R.id.plus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("+");
}
});
minus = (Button) findViewById(R.id.minus);
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("-");
}
});
divide = (Button) findViewById(R.id.divide);
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("/");
}
});
multiply = (Button) findViewById(R.id.multiply);
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.append("x");
}
});
equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.getText();
String expression = String.valueOf(result);
if(expression.contains("+")){
String[] numbers = expression.split("\+");
String no1 = numbers[0];
String no2 = numbers[1];
Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;
theresult = String.valueOf(added);
result.setText(theresult);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当您按下等号按钮时,您必须通过 result.getText() 获取文本视图的值,这样您才能获取文本视图的值并将其分配到某个字符串中。 字符串表达式 = String.valueOf(结果);
此处您只是使用 String.valueOf() 中的结果。通过调试或某种打印语句检查您是否获得了正确的值。
另外 post 您的代码 completely/reasonable 避免更多问题的代码,以便您得到答案。
就这样做:
@Override
public void onClick(View v) {
String expression = result.getText().toString();
// Log.d("LOG", expression);
if(expression.contains("+")){
(...)
也许工作。无需使用 String.valueOf()