我已经通过在评估等于按钮的情况下调用它们来单独测试这些方法。他们工作正常。我的问题是确定是否按下了乘法或加法运算符,然后调用适当的方法。
我在网上搜索并在案例中使用if语句来评估乘法:
case R.id.buttonequals:
if (v.equals(btnmult)) {
Multiplication();
}
else if (v.equals(btnplus)) {
Addition();
}
break;
这不起作用。这似乎合乎逻辑。如果按下相等按钮,则根据推送的内容,将调用正确的方法。我的完整代码如下:
public class MainActivity extends Activity {
// variables declared to hold buttons and text field.
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnc,
btnplus, btnd, btnpnt, btnequal, btnmult;
TextView text;
int num1, num2;
String input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// reference and assign the resources for the view elements
btn0 = (Button) findViewById(R.id.button0);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
btn5 = (Button) findViewById(R.id.button5);
btn6 = (Button) findViewById(R.id.button6);
btn7 = (Button) findViewById(R.id.button7);
btn8 = (Button) findViewById(R.id.button8);
btn9 = (Button) findViewById(R.id.button9);
btnc = (Button) findViewById(R.id.buttonc);
text = (TextView) findViewById(R.id.textView1);
btnd = (Button) findViewById(R.id.buttondel);
btnpnt = (Button) findViewById(R.id.buttonpnt);
btnplus = (Button) findViewById(R.id.buttonplus);
btnequal = (Button) findViewById(R.id.buttonequal);
btnmult = (Button) findViewById(R.id.buttonmult);
// add the click listeners
btn0.setOnClickListener(onClickListener);
btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);
btn3.setOnClickListener(onClickListener);
btn4.setOnClickListener(onClickListener);
btn5.setOnClickListener(onClickListener);
btn6.setOnClickListener(onClickListener);
btn7.setOnClickListener(onClickListener);
btn8.setOnClickListener(onClickListener);
btn9.setOnClickListener(onClickListener);
btnc.setOnClickListener(onClickListener);
btnd.setOnClickListener(onClickListener);
btnpnt.setOnClickListener(onClickListener);
btnplus.setOnClickListener(onClickListener);
btnequal.setOnClickListener(onClickListener);
btnmult.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button0:
text.setText(text.getText().toString()
+ btn0.getText().toString());
break;
case R.id.button1:
text.setText(text.getText().toString()
+ btn1.getText().toString());
break;
case R.id.button2:
text.setText(text.getText().toString()
+ btn2.getText().toString());
break;
case R.id.button3:
text.setText(text.getText().toString()
+ btn3.getText().toString());
break;
case R.id.button4:
text.setText(text.getText().toString()
+ btn4.getText().toString());
break;
case R.id.button5:
text.setText(text.getText().toString()
+ btn5.getText().toString());
break;
case R.id.button6:
text.setText(text.getText().toString()
+ btn6.getText().toString());
break;
case R.id.button7:
text.setText(text.getText().toString()
+ btn7.getText().toString());
break;
case R.id.button8:
text.setText(text.getText().toString()
+ btn8.getText().toString());
break;
case R.id.button9:
text.setText(text.getText().toString()
+ btn9.getText().toString());
break;
case R.id.buttondel:
String contents = text.getText().toString();
if (contents.length() > 1) {
text.setText(contents.substring(0, contents.length() - 1));
} else {
text.setText("");
}
break;
case R.id.buttonc:
text.setText("");
break;
case R.id.button8:
String dot = ".";
String concat = text.getText() + dot;
text.setText(concat);
break;
case R.id.buttonplus:
num1 = Integer.parseInt(text.getText().toString());
num2 = Integer.parseInt(text.getText().toString());
text.setText(text.getText() + "+");
break;
case R.id.buttonmult:
num1 = Integer.parseInt(text.getText().toString());
num2 = Integer.parseInt(text.getText().toString());
text.setText(text.getText() + "x");
break;
case R.id.buttonequals:
if (v.equals(btnmult)) {
Multiplication();
}
else if (v.equals(btnplus)) {
Addition();
}
break;
}
}
};
public void Addition() {
text.setText("");
int total = num1 + num2;
text.setText(Integer.toString(total));
}
public void Multiplication() {
text.setText("");
int total = num1 * num2;
text.setText(Integer.toString(total));
}
@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;
}
非常感谢帮助,如果解决方案明显存在,我深表歉意。谢谢大家的时间。
答案 0 :(得分:1)
为每个按钮添加一个onClickListener(乘法,除法等)。
还为每个添加标志,
boolean multiply = false; //same for rest
,点击它们时必须设置为true,
case R.id.multiply:
multiply = true;
然后当你查看=按钮
的onClick事件时 public void onClick(View v){
..
case R.id.buttonequals:
if (multiply) {
Multiplication();
}
else if (other flags) {
Addition();
}
break;
}
答案 1 :(得分:0)
为你的所有按钮设置一个相同的android:onClick,例如:“click”
然后像这样使用它:
public void clicking(View v) {
Button button = (Button) v;
switch (button.getId()) {
case R.id.button0:
text.setText(text.getText().toString()
+ btn0.getText().toString());
break;
case R.id.button1:
text.setText(text.getText().toString()
+ btn1.getText().toString());
break;
and so on...