安卓计算器,如何代码实现在文本框也把运算符显示出来?

hello2013year 2013-01-20 02:12:25
我越来越笨了,想实现比如:输入数字的时候,再按下运算符按钮,把运算符也显示到 EditText里。如:3+2,然后按 =号按钮出结果。
package com.qinguo.myfirstapp;

import java.text.BreakIterator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class BusOne extends Activity {
private Button[] bnNum = new Button[11];
private Button[] bnCommand = new Button[5];
private EditText editText = null;
private Button bnClear = null;
private String lastCommand;
private boolean clearFlag;
private boolean firstFlag;
private double result;

public BusOne() {
// 初始化各项值
result = 0; // x的值
firstFlag = true; // 是首次运算
clearFlag = false; // 不需要清空
lastCommand = "="; // 运算符
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bnCommand[0] = (Button) findViewById(R.id.add);
bnCommand[1] = (Button) findViewById(R.id.subtract);
bnCommand[2] = (Button) findViewById(R.id.divide);
bnCommand[3] = (Button) findViewById(R.id.equal);
bnCommand[4] = (Button) findViewById(R.id.multiply);

bnNum[0] = (Button) findViewById(R.id.num0);
bnNum[1] = (Button) findViewById(R.id.num1);
bnNum[2] = (Button) findViewById(R.id.num2);
bnNum[3] = (Button) findViewById(R.id.num3);
bnNum[4] = (Button) findViewById(R.id.num4);
bnNum[5] = (Button) findViewById(R.id.num5);
bnNum[6] = (Button) findViewById(R.id.num6);
bnNum[7] = (Button) findViewById(R.id.num7);
bnNum[8] = (Button) findViewById(R.id.num8);
bnNum[9] = (Button) findViewById(R.id.num9);
bnNum[10] = (Button) findViewById(R.id.point);
editText = (EditText) findViewById(R.id.result);
editText.setText("");
NumberAction na = new NumberAction();
CommandAction ca = new CommandAction();
for (Button bc : bnCommand) {
bc.setOnClickListener(ca);
}
for (Button bc : bnNum) {
bc.setOnClickListener(na);
}
bnClear = (Button) findViewById(R.id.clear);
bnClear.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
editText.setText("");
result = 0;
firstFlag = true;
clearFlag = false;
lastCommand = "=";
}
});
}

private class NumberAction implements OnClickListener {

@Override
public void onClick(View view) {
Button bn = (Button) view;
String input = bn.getText().toString();
if (firstFlag) {
if (input.equals(".")) {
return;

}
if (editText.getText().toString().equals("0.0")) {
editText.setText("");
}
firstFlag = false;
} else {
String editTextStr = editText.getText().toString();
if (editTextStr.indexOf(".") != -1 && input.equals(".")) {
return;
}
if (editTextStr.equals("-") && input.equals(".")) {
return;
}
/*if (editTextStr.equals("0") && !input.equals(".")) {
return;
}*/

}
if (clearFlag) {
editText.setText("");
clearFlag = false;
}
editText.setText(editText.getText().toString() + input);
}

}

private class CommandAction implements OnClickListener {
public void onClick(View view) {
Button bn = (Button) view;
String inputCommand = (String) bn.getText();
if (firstFlag) {
if (inputCommand.equals("-")) {
editText.setText("-");
firstFlag = true;

}
}else {
if(!clearFlag){
calculate(Double.parseDouble(editText.getText().toString()));
}
lastCommand = inputCommand;
clearFlag = true;
}

}
}


private void calculate (double x ){
if(lastCommand.equals("+")){
result += x;
}else if (lastCommand.equals("-")){
result -= x;
}else if (lastCommand.equals("*")){
result *= x;
}else if (lastCommand.equals("/")){
result /= x;
}else if (lastCommand.equals("=")){
result = x;
}
editText.setText(""+result);
}
}
...全文
336 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
微笑面具 2013-06-29
  • 打赏
  • 举报
回复
这是完善后的代码么?

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧