我需要在Android中创建一个计算器。我需要创建一个plus
函数来添加到数字(整数)。这是我的代码。我还需要创建其他函数,比如减法。
public class MainActivity extends AppCompatActivity {
Button one,two,three,four,five,six,seven,eight,nine,plus,minus,mul,divide,equal;
TextView textView;
String currentDisplayedInput="";
private String inputToBeParsed = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
plus = (Button) findViewById(R.id.plus);
minus = (Button) findViewById(R.id.minus);
mul = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
equal=(Button)findViewById(R.id.equal);
textView = (TextView) findViewById(R.id.textView);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentDisplayedInput += "1";
//inputToBeParsed += "1";
textView.setText(currentDisplayedInput);
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentDisplayedInput += "2";
//inputToBeParsed += "1";
textView.setText(currentDisplayedInput);
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
我需要做的就是当我点击1 + 1
时,TextView
应该显示结果2
。