源代码下载:https://gitee.com/zha-yingying/calculator.git
1.建立一个新的Layout,我这里使用的是GridLayout(网格布局),提取屏幕宽度(方便后面设置子控件的宽度)GridLayout的特点是自定义网格布局有几行几列,我们可以将自控件自定义放在第几行第几列。
2.建立一个新的textview(文本框)作为我们计算器输入数字运算符号的界面,将textview放在第一行第一列,设置宽度为屏幕的宽度,这边我们需要提取一下屏幕的宽度。
3.添加子控件 添加按钮到布局上(数字+运算符号+等于符号)分别设置它们的位置,我这里的GridLayout设置的是八行四列,宽度为屏幕宽度的四分之一。
4.给每个控件都加上点击事件,给按钮设置监听器。在MainaActivity中使用enum(枚举)方法,分为四种情况(1)无按钮操作(2)上一次按钮为数字(3)上一次按钮为运算符号(4)上一次按钮为等于,分别写出其操作内容,需要在点击事件中添加状态。
提取屏幕宽度:
WindowManager wm = (WindowManager) this.getSystemService(this.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
添加按钮:
(1)添加数字按钮(依次添加0-9和"."按钮)
Button button1 = new Button(this); layout1.addView(button1); GridLayout.LayoutParams bt1 = (GridLayout.LayoutParams) button1.getLayoutParams(); bt1.rowSpec = GridLayout.spec(6,1,GridLayout.CENTER,1); bt1.columnSpec = GridLayout.spec(0,1,GridLayout.CENTER,1); bt1.width = width/4; button1.setText("1"); button1.setLayoutParams(bt1);
(2)添加运算符号按钮(加减乘除这一类)
减号为例:
Button buttonminus = new Button(this); layout1.addView(buttonminus); buttonminus.setText("-"); GridLayout.LayoutParams btjian = (GridLayout.LayoutParams) buttonminus.getLayoutParams(); btjian.rowSpec = GridLayout.spec(6, 1, GridLayout.CENTER, 1); btjian.columnSpec = GridLayout.spec(3, 1, GridLayout.CENTER, 1); btjian.width = width / 4; buttonminus.setLayoutParams(btjian);
(3)添加等号按钮
Button buttonequal = new Button(this); layout1.addView(buttonequal); buttonequal.setText("="); GridLayout.LayoutParams btdy = (GridLayout.LayoutParams) buttonequal.getLayoutParams(); btdy.rowSpec = GridLayout.spec(7, 1, GridLayout.CENTER, 1); btdy.columnSpec = GridLayout.spec(3, 1, GridLayout.CENTER, 1); btdy.width = width / 4; buttonequal.setLayoutParams(btdy);
给按钮添加点击事件,设置状态:
(1)数字按钮
button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NumberClick("1"); } });
(2)运算符号按钮(减号为例子)
buttonminus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { OperationClick("-"); operationType = OperationType.Minus; } });
(3)等号按钮
buttonequal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EqualClick("="); } }); }
枚举法举例
新建两个enum的类
ClickType判断上一次按钮是数字/运算符号/等于符号
public enum ClickType { None, Number, Operation, Equqal, }
OperationType判断上一次的运算符号是加/减/乘/除
public enum OperationType { None, Plus, Minus, Multiply, Divide, }
在MainActivity里:
private ClickType clickType = ClickType.None;
private OperationType operationType = OperationType.None;
//按下数字键时
private void NumberClick(String n){ switch (clickType){ case None: textView.setText(n); break; case Number: String an = textView.getText().toString(); textView.setText(an+n); break; case Operation: textView.setText(n); break; case Equqal: textView.setText(n); break; } clickType = ClickType.Number; }
//按下操作符号时
private void OperationClick(String n1){ switch ((clickType)){ case None: textView.setText("mistake"); break; case Number:
switch ((operationType)){ case None: String an2 = textView.getText().toString(); a = Double.valueOf(an2); textView.setText(an2+n1); break; case Plus: String an3 = textView.getText().toString(); b = Double.valueOf(an3); a = a + b; textView.setText(an3+n1); textView.setText(""+a); break;
case Minus: String an4 = textView.getText().toString(); b = Double.valueOf(an4); a = a - b; textView.setText(an4+"-"); textView.setText(""+a); break; case Divide: String an5 = textView.getText().toString(); b = Double.valueOf(an5); a = a / b; textView.setText(an5+n1); textView.setText(""+a); break; case Multiply: String an6 = textView.getText().toString(); b = Double.valueOf(an6); a = a * b; textView.setText(an6+n1); textView.setText(""+a); break; } break; case Operation: textView.setText("mistake"); break;
case Equqal: String an1 = textView.getText().toString(); a = Double.valueOf(an1); break; } clickType = ClickType.Operation; }
//按下等于号时
private void EqualClick(String n2){ switch ((clickType)){ case None: textView.setText("mistake"); break; case Number: switch ((operationType)){ case None: textView.setText(""+a); case Plus: String bn = textView.getText().toString(); b = Double.valueOf(bn); b = a + b; textView.setText(""+b); a = 0; break;
case Minus: String bn1 = textView.getText().toString(); b = Double.valueOf(bn1); b = a - b; textView.setText(""+b); a = 0; break; case Multiply: String bn2 = textView.getText().toString(); b = Double.valueOf(bn2); b = a * b; textView.setText(""+b); a = 0; break; case Divide: String bn3 = textView.getText().toString(); b = Double.valueOf(bn3); b = a / b; textView.setText(""+b); a = 0; break; } break; case Operation: textView.setText("mistake"); break; case Equqal: textView.setText("mistake"); break; } clickType = ClickType.Equqal; }