我仍然是一个初学者,但试图学习所以可能会做一些非常愚蠢的事情。我的问题是关于我正在尝试制作的一个简单的 Android 计算器,但我认为这个问题涉及我认为我理解的基本 Java。我从按下的按钮中获取值,然后将其传递给显示输入的方法,然后为输入设置全局变量。如果我按“1”,则方法标题中的值为“1”,但随后会在该方法中丢失。也许我不明白如何像我想的那样处理 Java 类型——不知道。我曾经让逻辑工作,但不得不改变它的显示方式,当然,这在某个地方搞砸了我的逻辑。无论如何,我现在将发布通过的声明和接收方法。提前致谢。
public void initButtons()
{
//register buttons as listeners
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final Button button4 = (Button) findViewById(R.id.button4);
final Button button5 = (Button) findViewById(R.id.button5);
final Button button6 = (Button) findViewById(R.id.button6);
final Button button7 = (Button) findViewById(R.id.button7);
final Button button8 = (Button) findViewById(R.id.button8);
final Button button9 = (Button) findViewById(R.id.button9);
final Button button10 = (Button) findViewById(R.id.button10);
final Button addButton = (Button) findViewById(R.id.addButton);
final Button subButton = (Button) findViewById(R.id.subButton);
final Button multButton = (Button) findViewById(R.id.multButton);
final Button divButton = (Button) findViewById(R.id.divButton);
final Button calcButton = (Button) findViewById(R.id.calcButton);
final Button clearButton = (Button) findViewById(R.id.clearButton);
//when button is pressed, send num to calc function
button1.setOnClickListener
(new Button.OnClickListener()
{
public void onClick(View v)
{
inputString = button1.getText().toString();
displayCalc(inputString);
//displayCalc(1.0);
}
}
);
private void displayCalc(String curValue)
{
if (hasChanged = false)
{
//display number if reset
String display = curValue;
number1 = Double.parseDouble(display);
}
else
{
// display = display + operator + curValue;
//number2 = Double.parseDouble(curValue);
}
//showDisplay(display);
}