一、首先编写layout界面框架
整体采用LinearLayout布局;Button实现按键功能,为其设置id,以便进行触发事件的控制,设置文本大小/宽高以及颜色;
例如为“1”按钮进行布局:
Ps:Input输入框采用Text view文本框
二、Java程序编写
为每个功能按钮绑定触发事件,以按钮“1”为例
btn_1 = (Button) findViewById(R.id.btn_1);
btn_1.setOnClickListener(this);
运算逻辑的程序实现:
public void onClick(View v) {
int last = 0;
if (pending.length() != 0) {
last = pending.codePointAt(pending.length() - 1);
}
switch (v.getId()) {
case R.id.btn_0:
pending = pending.append("0");
et_input.setText(pending);
break;
case R.id.btn_1:
pending = pending.append("1");
et_input.setText(pending);
break;
case R.id.btn_2:
pending = pending.append("2");
et_input.setText(pending);
break;
case R.id.btn_3:
pending = pending.append("3");
et_input.setText(pending);
break;
case R.id.btn_4:
pending = pending.append("4");
et_input.setText(pending);
break;
case R.id.btn_5:
pending = pending.append("5");
et_input.setText(pending);
break;
case R.id.btn_6:
pending = pending.append("6");
et_input.setText(pending);
break;
case R.id.btn_7:
pending = pending.append("7");
et_input.setText(pending);
break;
case R.id.btn_8:
pending = pending.append("8");
et_input.setText(pending);
break;
case R.id.btn_9:
pending = pending.append("9");
et_input.setText(pending);
break;
case R.id.btn_plus:
//if (last >= '0' && last <= '9' ) {
pending = pending.append("+");
// }
et_input.setText(pending);
break;
case R.id.btn_minus:
//if (last >= '0' && last <= '9') {
pending = pending.append("-");
// }
et_input.setText(pending);
break;
case R.id.btn_multiply:
// if (last >= '0' && last <= '9' ) {
pending = pending.append("*");
// }
et_input.setText(pending);
break;
case R.id.btn_divide:
// if (last >= '0' && last <= '9' ) {
pending = pending.append("/");
// }
et_input.setText(pending);
break;
case R.id.btn_point:
if (judje1()) {
pending = pending.append(".");
et_input.setText(pending);
}
break;
case R.id.btn_clear: //清空
pending = pending.delete(0, pending.length());
et_input.setText(pending);
break;
case R.id.btn_equal: // =等于
if ((pending.length() > 1)) {
InfixInToDuffix inf = new InfixInToDuffix();
String jieguo;
try {
String a = inf.toSuffix(pending);
jieguo = inf.dealEquation(a);
} catch (Exception ex) {
jieguo = "出错";
}
et_input.setText(pending + "=" + jieguo);
pending = pending.delete(0, pending.length());
if (Character.isDigit(jieguo.charAt(0))) {
pending = pending.append(jieguo);
}
}
break;
default:
break;
}
}
private boolean judje1() {
String a = "+-*/.";
int[] b = new int[a.length()];
int max;
for (int i = 0; i < a.length(); i++) {
String c = "" + a.charAt(i);
b[i] = pending.lastIndexOf(c);
}
Arrays.sort(b);
if (b[a.length() - 1] == -1) {
max = 0;
} else {
max = b[a.length() - 1];
}
if (pending.indexOf(".", max) == -1) {
return true;
} else {
return false;
}
}
private int judje2() {
int a = 0, b = 0;
for (int i = 0; i < pending.length(); i++) {
if (pending.charAt(i) == '(') {
a++;
}
if (pending.charAt(i) == ')') {
b++;
}
}
if (a == b)
return 0;
if (a > b)
return 1;
return 2;
}
PS:运算逻辑程序实现比较复杂,因此参考了网上的代码实现;增加多了一个JAVA文件 InfixInToDuffix 以维持正常运算;
三、最后运行界面样例
心得:对布局有了更深层次的理解和运用。熟悉了Layout代码和Androidstudio软件的运行;