我正在尝试开发一个计算器应用程序。
当按下1按钮时,文本视图显示“1”,当我按下2时,文本视图通过替换“1”显示“2”。
我的问题是如何在依次按下 1 和 2 时显示“12”。
我目前的代码是:
package com.calculator.kartheek;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textview=(TextView)findViewById(R.id.editText);
Button one=(Button)findViewById(R.id.one);
Button two=(Button)findViewById(R.id.two);
Button three=(Button)findViewById(R.id.three);
Button four=(Button)findViewById(R.id.four);
Button five=(Button)findViewById(R.id.five);
Button six=(Button)findViewById(R.id.six);
Button seven=(Button)findViewById(R.id.seven);
Button eight=(Button)findViewById(R.id.eight);
Button nine=(Button)findViewById(R.id.nine);
Button zero=(Button)findViewById(R.id.zero);
Button equals=(Button)findViewById(R.id.equals);
Button add=(Button)findViewById(R.id.plus);
Button sub=(Button)findViewById(R.id.sub);
Button mul=(Button)findViewById(R.id.mul);
Button div=(Button)findViewById(R.id.division);
Button dot=(Button)findViewById(R.id.dot);
Button clear=(Button)findViewById(R.id.clear);
one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("1");
textview.append(INPUT_SERVICE);
}
});
two.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("2");
}
});
three.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("3");
}
});
four.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("4");
}
});
five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("5");
}
});
six.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("6");
}
});
seven.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("7");
}
});
eight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("8");
}
});
nine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("9");
}
});
zero.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText("0");
}
});
dot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textview.setText(".");
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}