Article directory
I. Introduction
- Description: Regarding the blog published on June 12th [Basics of Android Development] SQLite development to replicate address books, notepads, and computers , a fan asked me if the last calculator assignment has logic layer code. I will give it here. Provide specific code.
- Difficulty: Beginner
- Effect
2. Design
1. Case
For beginners or friends who are not good at algorithms, I think it is necessary to take a look at such a calculation method first.
It seems to be called function nesting method (I forgot the professional term, if you know it, you can tell everyone in the comment area)
public static void main(String[] args) {
System.out.print("输入一个数,计算1~n的数和:");
Scanner input = new Scanner(System.in);
int max = input.nextInt();
System.out.print("结果:" + add(0, 1, max));
}
private static int add(int S, int min, int max) {
if(min <= max) {
S += min;
return add(S, ++min, max);
}
return S;
}
2. Algorithm design
public class alg {
public static double parse(String formula) {
int temp = formula.indexOf("+");
if (temp != -1) {
return parse(formula.substring(0, temp)) + parse(formula.substring(temp + 1));
}
temp = formula.lastIndexOf("-");
if (temp != -1) {
return parse(formula.substring(0, temp)) - parse(formula.substring(temp + 1));
}
temp = formula.indexOf("*");
if (temp != -1) {
return parse(formula.substring(0, temp)) * parse(formula.substring(temp + 1));
}
temp = formula.lastIndexOf("/");
if (temp != -1) {
return parse(formula.substring(0, temp)) / parse(formula.substring(temp + 1));
}
return Double.parseDouble(formula);
}
}
3. Encoding
1. UI interface design
An elegant computing interface might have buttons with similar functions using the same UI style.
(1) Button style design
- Calculation symbols
For example, basic addition, subtraction, multiplication and division (+, -, *, /) and equals (=)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--设置颜色-->
<solid android:color="#A7DD4E2E"
/>
<!--设置圆角-->
<corners android:radius="90dp"/>
<size
android:width="90dp"
android:height="90dp"/>
</shape>
- number
I looked at some calculators and there are number buttons like 00, Teku La
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--设置颜色-->
<solid android:color="#A6282828"
/>
<!--设置圆角-->
<corners android:radius="90dp"/>
<size
android:width="90dp"
android:height="90dp"/>
</shape>
- special
For example, AC: clear
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--设置颜色-->
<solid android:color="#A7989595"
/>
<!--设置圆角-->
<corners android:radius="90dp"/>
<size
android:width="90dp"
android:height="90dp" />
</shape>
(2) Main interface layout design
Regarding the main interface, I further optimized it, mainly referring to the computer styles of many mobile phones, and found that this style looks very friendly to the eyes.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#6F6A6A">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/jsq_text"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#FFFFFF"
android:textSize="25dp"
android:gravity="right"
android:layout_marginTop="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center">
<Button
android:id="@+id/ac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AC"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape"/>
<Button
android:id="@+id/fang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+/-"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape"/>
<Button
android:id="@+id/yu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape" />
<Button
android:id="@+id/chu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_ceng"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei"/>
<Button
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei"/>
<Button
android:id="@+id/there"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei" />
<Button
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_ceng"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei"/>
<Button
android:id="@+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei"/>
<Button
android:id="@+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei" />
<Button
android:id="@+id/jiang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_ceng"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei"/>
<Button
android:id="@+id/event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei"/>
<Button
android:id="@+id/nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei" />
<Button
android:id="@+id/cheng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_ceng"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_margin="10dp"
android:textSize="25dp"
android:background="@drawable/shape_ling"/>
<Button
android:id="@+id/dian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_hei" />
<Button
android:id="@+id/dengyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:layout_margin="5dp"
android:textSize="25dp"
android:background="@drawable/shape_ceng"/>
</LinearLayout>
</LinearLayout>
2. Encoding
(1) Control initialization
Those who are familiar with my coding style will know that it is bound control information without looking at it.
Button zero,one,two,there,four,five,six,seven,event,nine;
Button jia,jiang,cheng,chu;
Button dian,dengyu,ac;
TextView text;
private void init() {
zero = findViewById(R.id.zero);
one = findViewById(R.id.one);
two = findViewById(R.id.two);
there = findViewById(R.id.there);
four = findViewById(R.id.four);
five = findViewById(R.id.five);
six = findViewById(R.id.six);
seven = findViewById(R.id.seven);
event = findViewById(R.id.event);
nine = findViewById(R.id.nine);
jia = findViewById(R.id.jia);
jiang = findViewById(R.id.jiang);
cheng = findViewById(R.id.cheng);
chu = findViewById(R.id.chu);
dian = findViewById(R.id.dian);
dengyu = findViewById(R.id.dengyu);
text = findViewById(R.id.jsq_text);
ac = findViewById(R.id.ac);
zero.setOnClickListener(clickListener);
one.setOnClickListener(clickListener);
two.setOnClickListener(clickListener);
there.setOnClickListener(clickListener);
four.setOnClickListener(clickListener);
five.setOnClickListener(clickListener);
six.setOnClickListener(clickListener);
seven.setOnClickListener(clickListener);
event.setOnClickListener(clickListener);
zero.setOnClickListener(clickListener);
nine.setOnClickListener(clickListener);
jia.setOnClickListener(clickListener);
jiang.setOnClickListener(clickListener);
cheng.setOnClickListener(clickListener);
chu.setOnClickListener(clickListener);
dian.setOnClickListener(clickListener);
dengyu.setOnClickListener(clickListener);
ac.setOnClickListener(clickListener);
}
(2) Event listener
Mainly responsible for data rendering and algorithm reference
public View.OnClickListener clickListener = new View.OnClickListener() {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.zero:
js += "0";
text.setText(js);
break;
case R.id.one:
js += "1";
text.setText(js);
break;
case R.id.two:
js += "2";
text.setText(js);
break;
case R.id.there:
js += "3";
text.setText(js);
break;
case R.id.four:
js += "4";
text.setText(js);
break;
case R.id.five:
js += "5";
text.setText(js);
break;
case R.id.six:
js += "6";
text.setText(js);
break;
case R.id.seven:
js += "7";
text.setText(js);
break;
case R.id.event:
js += "8";
text.setText(js);
break;
case R.id.nine:
js += "9";
text.setText(js);
break;
case R.id.jia:
js += "+";
text.setText(js);
break;
case R.id.jiang:
js += "-";
text.setText(js);
break;
case R.id.cheng:
js += "*";
text.setText(js);
break;
case R.id.chu:
js += "/";
text.setText(js);
break;
case R.id.dian:
js += ".";
text.setText(js);
break;
case R.id.dengyu:
double a = alg.parse(js);
text.setText(String.valueOf(a));
break;
case R.id.ac:
js = "";
text.setText(js);
break;
}
}
};
4. Accessories
Source code: https://download.csdn.net/download/weixin_48916759/87935078