我真的不知道,我是编程的初学者,做了大约2个月。我已经为Android制作了计算器,但问题是equals按钮什么都不做。
设置不同的变量:
Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;
除了equals之外的所有按钮只是将相应的文本附加到变量结果。
这是仅相等按钮的代码(仅为测试而创建,但失败了):
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);
}
}
});
我已经在这里使用过,所以我可以使用else来进行其他操作。稍后我也将使用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);
}
}
答案 0 :(得分:1)
这样做:
@Override
public void onClick(View v) {
String expression = result.getText().toString();
// Log.d("LOG", expression);
if(expression.contains("+")){
(...)
也许工作。无需使用String.valueOf()
答案 1 :(得分:0)
当您按下等号按钮时,您必须通过result.getText()获取textview的值,以便获取文本视图的值并将其分配给某个字符串。 String expression = String.valueOf(result);
这里只是使用String.valueOf()中的结果。通过调试或某种打印状态检查您是否获得了正确的值。
还要完整/合理地发布您的代码以避免更多问题,以便您得到答案。