编写一个简单的android程序,程序一 | Android studio 实现简易计算器

一、首先编写layout界面框架

c4fcd7bfa50e11fa5efc12bb44257749.png

整体采用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 以维持正常运算;

三、最后运行界面样例

5689b618142320766ae62c1ac6d4ab2e.png

心得:对布局有了更深层次的理解和运用。熟悉了Layout代码和Androidstudio软件的运行;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值