我的基本数字是0-9,而我的代码是
public class MainActivity extends AppCompatActivity {
Button button0 , button1 , button2 , button3 , button4 , button5 , button6 ,
button7 , button8 , button9 , buttonAdd , buttonSub , buttonDivision,
buttonMul , button10 , buttonC , buttonEqual ;
TextView txtView ;
float num1 = 0;
float num2 = 0;
float sum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
buttonAdd = (Button) findViewById(R.id.buttonadd);
buttonSub = (Button) findViewById(R.id.buttonsub);
buttonMul = (Button) findViewById(R.id.buttonmul);
buttonDivision = (Button) findViewById(R.id.buttondiv);
buttonC = (Button) findViewById(R.id.buttonC);
buttonEqual = (Button) findViewById(R.id.buttoneql);
txtView = (TextView) findViewById(R.id.txtView);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt.setText(txt.getText()+"1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(txtView.getText()+"9");
}
});
然而,当我尝试执行添加功能时,应用程序崩溃了。这似乎很简单,但我见过的其他例子更复杂。我的附加代码是......
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Float.parseFloat(txtView.getText().toString());
num2 = Float.parseFloat(txtView.getText().toString());
sum = num1 + num2;
}
});
然后当按下相等按钮时,我的代码如下。这是对我不起作用的部分。
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtView.setText(Float.toString(sum));
}
});
这导致应用程序崩溃。有帮助吗?感谢
答案 0 :(得分:0)
我认为你的buttonAdd
功能是错误的。
num1 = Float.parseFloat(txtView.getText().toString());
num2 = Float.parseFloat(txtView.getText().toString());
执行代码时,num1
和num2
相同。
答案 1 :(得分:0)
我不明白你的错误。 但没有问题,只需要改变btnAdd和btnEqual 你能试试吗?
base-4.5