0x00 功能
基于Java的Android四则运算器,可以进行浮点数的四则运算
计算器
0x01 开发工具
Android Studio、雷电模拟器、Android手机
0x02 基本简介
总体使用网格布局(七行四列)使用了两个TextView和十八个按钮,其中TextVIew采用滚动设计(跑马灯)解决TextView文本过长的显示问题,支持浮点数的+ - * / 以及百分比运算
0x03 踩坑经历
1.布局,由于不熟练导致的布局频频受阻
2.按钮监听,逐个按钮监听工作量有点大,故采用Button数组批量绑定并且监听,但是此举必先定义Button
3.逻辑代码,为了APP有更友好的交互,逻辑代码方面实在浪费了n多时间
0x04 核心代码
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.feng.learn3.MainActivity"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="7"
android:columnCount="4"
android:id="@+id/gridLayout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:gravity="right"
android:textColor="#292828"
android:text="0"
android:layout_width="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:textSize="60sp"
android:layout_height="@dimen/button_height"
android:layout_columnSpan="4"
android:layout_column="0"
android:layout_row="1"
android:id="@+id/textView"
android:layout_below="@+id/gridLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="@string/clear"
android:layout_column="0"
android:layout_row="2"
android:layout_columnSpan="1"
android:layout_gravity="fill"
android:layout_width="wrap_content"
android:layout_height="@dimen/button_height"
android:id="@+id/buttonC" />
<!--……以下省略-->
</GridLayout>
</RelativeLayout>
MainActivity.java文件
package com.feng.learn3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//按钮0~9
private Button buttonP;
private Button button0;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
//按钮 + - * / =
private Button buttonA;
private Button buttonS;
private Button buttonM;
private Button buttonD;
private Button buttonE;
//按钮 CE C
private Button buttonB;
private Button buttonC;
//百分号按钮
private Button buttonPer;
//TextView
private TextView textView;
private TextView hintTestView;
//左右操作数
private Double left = 0.0;
private Double right = 0.0;
//操作符
private String operator = "#";
//操作符选中标志
private byte tag = 0;
Button[] buttons = {buttonP,button0,button1,button2,button3,button4,button5,button6,button7,
button8,button9,buttonA,buttonS,buttonM,buttonD,buttonE,buttonB,buttonC,buttonPer};
Integer[] ids = {R.id.buttonP,R.id.button0,R.id.button1,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
R.id.button6,R.id.button7,R.id.button8,R.id.button9,R.id.buttonA,R.id.buttonS,
R.id.buttonM,R.id.buttonD,R.id.buttonE,R.id.buttonB,R.id.buttonC,R.id.buttonPer};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定Textiews
textView = (TextView) findViewById(R.id.textView);
hintTestView = (TextView) findViewById(R.id.textView1);
textView.setSelected(true);
hintTestView.setSelected(true);
//绑定Buttons并监听之
for (int i = 0; i < 19; i++){
buttons[i] = (Button) findViewById(ids[i]);
buttons[i].setOnClickListener(this);
}
}
@Override
public void onClick(View view) {
//逻辑代码
String op = (String) ((Button)view).getText();
switch (op){
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
for (int i = 11; i < 17; i++){
buttons[i].setEnabled(true);
}
if (tag == 1){
tag = 0;
textView.setText("");
}
if (textView.getText().toString().startsWith("0") && !textView.getText().toString().startsWith("0.")){
textView.setText(op);
} else {
textView.setText(textView.getText().toString().concat(op));
}
break;
case ".":
if (tag == 1){
tag = 0;
textView.setText("0.");
} else {
textView.setText(textView.getText().toString().concat(op));
}
buttons[0].setEnabled(false);//"."点按钮在每个操作数仅可使用一次
break;
case "%":
Double temp = 0.0;
temp = temp.valueOf(textView.getText().toString());
temp /= 100;
textView.setText(temp.toString());
break;
case "+":
case "-":
case "*":
case "/":
setOperator(op);break;
case "=":
setOperator(op);
textView.setText(left.toString());
hintTestView.setText("");
for (int i = 11; i < 17; i++){
buttons[i].setEnabled(false);
}
left = 0.0;
right = 0.0;
break;
case "CE"://后退一位
textView.setText(textView.getText().subSequence(0,textView.getText().length()-1));
break;
case "C"://清零
textView.setText("0");
hintTestView.setText("");
buttons[0].setEnabled(true);
for (int i = 11; i < 17; i++){
buttons[i].setEnabled(true);
}
left = 0.0;
right = 0.0;
tag = 0;
break;
}
}
//选中对应操作符计算左右操作数结果
public void setOperator(String op){
if (tag == 1){
//临时更换操作符
hintTestView.setText(hintTestView.getText().subSequence(0,hintTestView.getText().length()-2) + op + " ");
} else {
if (hintTestView.getText().toString().equals("")){
hintTestView.setText(textView.getText() + " " + op + " ");
} else {
hintTestView.setText(hintTestView.getText().toString() + textView.getText() + " " + op + " ");
}
right = right.valueOf(textView.getText().toString());
left = new Cal().cal(operator,left,right);
textView.setText(left.toString());
tag = 1;
}
buttons[0].setEnabled(true);
operator = op;
}
}
Cal.java文件
package com.feng.learn3;
/**
* Created by 枫 on 2019/2/10.
*/
public class Cal {
public double cal (String op,double left,double right){
double ans = 0.0;
switch (op){
case "+":ans = add(left,right);break;
case "-":ans = sub(left, right);break;
case "*":ans = mul(left, right);break;
case "/":ans = div(left, right);break;
}
return ans;
}
public double add (double left,double right){
return left + right;
}
public double sub (double left,double right){
return left - right;
}
public double mul (double left,double right){
return left * right;
}
public double div (double left,double right){
if (right != 0){
return left/right;
} else {
return 0;
}
}
}
网友评论