第一个安卓app——计算器

时间:2021-07-19 17:57:34

 

几天前,我花了一天时间,结合这段时间所学知识开发出了一个简单的计算器,它由两个TextView和23个Button组成,代码会放在文章结尾。

第一个安卓app——计算器

 

 

TextView

TextView:上面一个TextView的功能是记录运算过程,其中运算符的显示顺序是输入顺序也是运算顺序/*即1 2×3为(1 2)×3*/,

下面一个TextView功能是显示运算结果和显示输入的运算数据。

 

Button

AC键:功能是清除运算记录,将所有变量重置。

 第一个安卓app——计算器

 

 

BC键:功能是返回上一步运算结果,在每一步运算结束后,会将这一步的运算结果存储在ArrayList中,BC之后会将Arraylist中最近一次的运算记录也就是返回的这步运算记录删除,以实现随意回调的效果。

 第一个安卓app——计算器第一个安卓app——计算器第一个安卓app——计算器

 

 

/-键:功能是将现在输入框中的数据转为它的相反数

 

DEL键:功能是退格,在输入数据不是小数时,退格的实现方式是输入框中数据除十得到结果,在输入数据是小数时退格的实现方式是获取输入框中的字符串减去末尾数字字符得到结果。

 

0~9数字键:进行数字键输入操作时,会对当前是否正在输入小数进行判断,如果是,则这样将

double num = 1;
pointnum ;//小数点后输入的位数
for (int i = 0; i < pointnum; i )
    num /= 10;
init = key* num;//当前key的值*位数

的确是繁琐!

推荐第二种做法直接将key拼接到输入框字符串的尾部即可!

    

四则运算键:在四则运算前都会对前一个输入是否为其他四则运算符进行判断。如果是,则将前一个四则运算符替换为当前的。进行每一个运算符在被点击之后都会储存起来,在下一个运算符或等于键被点击时(即下一个需要运算的数据输入完毕时)进行运算,并将此时被点击的运算符存储起来以供下次运算使用。

 

%键:进行求余运算,与四则运算类似。

 

x²键:对当前输入框中的数据进行平方操作。

 

√x键:对当前输入框中的数据进行开平方操作。

 第一个安卓app——计算器

 

 

小数点键:将当前输入状态设置为小数输入。

 第一个安卓app——计算器

 

 

关于这个程序的反省:

*在这个程序中我对输入框中的输入等操作大多是以double数据的运算实现的,在写完之后我突然意识到其实输入等很多功能的实现用字符串操作会省很多功夫,像DEL退格的实现以及小数点的等等操作用字符串进行操作确实会容易很多,不需要做那么多运算,在用的时候直接把字符串转换为所需数据类型即可!

 

*关于界面布局的看法:

在刚开始写这个程序布局的时候我只用了一个RelativeLayout就写了下来,在虚拟机上看起来布局很好,但放到其他型号的手机里布局就不行了,今天我对它的布局做了优化,用的是一个大的LinearLayout里面框住两个文本框和一个小的LinearLayout框住五个更小的LinearLayout,然后为他们各自设置match_parent和layout_weight,五个更小的LinearLayout每个各占match_parent/5,在更小的的LinearLayout里为里面的键设置layoutweight,避免了直接使用dp等数字,保证它可以占满整个屏幕或者确定的那一部分,使之在其他机型上也能美观地展示。

 

activity_calculator.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:tools="http://schemas.android.com/tools"
  4     android:layout_width="match_parent"
  5     android:layout_height="match_parent"
  6     tools:context="com.Appalication_Trial.Calculator">
  7 
  8     <TextView
  9         android:id="@ id/tv_cord"
 10         android:layout_width="match_parent"
 11         android:layout_height="99dp"
 12         android:background="@drawable/riple_btn_pink"
 13         android:gravity="right"
 14         android:textSize="30dp"></TextView>
 15 
 16     <TextView
 17         android:id="@ id/tv_calcul"
 18         android:layout_width="match_parent"
 19         android:layout_height="99dp"
 20         android:layout_below="@ id/tv_cord"
 21         android:background="@drawable/riple_btn_pink"
 22         android:gravity="right"
 23         android:textSize="30dp"></TextView>
 24     <LinearLayout
 25         android:layout_width="match_parent"
 26         android:layout_height="match_parent"
 27         android:layout_below="@id/tv_calcul"
 28         android:orientation="vertical"
 29         android:gravity="center">
 30         <LinearLayout
 31             android:layout_width="match_parent"
 32             android:layout_height="0dp"
 33             android:layout_weight="1"
 34             android:orientation="horizontal"
 35             ><Button
 36             android:id="@ id/calcul_btn_ac"
 37             android:layout_width="70dp"
 38             android:layout_height="match_parent"
 39             android:layout_marginTop="0dp"
 40             android:background="@drawable/riple_btn_nocorner"
 41             android:fontFamily="宋体"
 42             android:text="AC"
 43             android:textSize="40dp"
 44             android:layout_weight="1"
 45             android:textColor="#169FDF"
 46             ></Button>
 47             <Button
 48                 android:id="@ id/calcul_btn_back"
 49                 android:layout_width="70dp"
 50                 android:layout_height="match_parent"
 51                 android:layout_marginTop="0dp"
 52                 android:layout_toRightOf="@id/calcul_btn_ac"
 53                 android:background="@drawable/riple_btn_nocorner"
 54                 android:fontFamily="宋体"
 55                 android:text="bc"
 56                 android:textColor="#169FDF"
 57                 android:layout_weight="1"
 58                 android:textSize="40dp"></Button>
 59             <Button
 60                 android:id="@ id/calcul_btn_neg"
 61                 android:layout_width="70dp"
 62                 android:layout_height="match_parent"
 63                 android:layout_marginTop="0dp"
 64                 android:background="@drawable/riple_btn_nocorner"
 65                 android:text=" /-"
 66                 android:textSize="40dp"
 67                 android:textColor="#169FDF"
 68                 android:layout_weight="1"
 69                 android:fontFamily="宋体"></Button>
 70             <Button
 71                 android:id="@ id/calcul_btn_del"
 72                 android:layout_width="140dp"
 73                 android:layout_height="match_parent"
 74                 android:layout_marginTop="0dp"
 75                 android:background="@drawable/riple_btn_nocorner"
 76                 android:text="del"
 77                 android:textSize="30dp"
 78                 android:textColor="#169FDF"
 79                 android:layout_weight="2"
 80                 android:fontFamily="宋体"></Button>
 81 
 82         </LinearLayout>
 83         <LinearLayout
 84             android:layout_width="match_parent"
 85             android:layout_weight="1"
 86             android:layout_height="0dp"
 87             android:orientation="horizontal"
 88             >
 89             <Button
 90                 android:id="@ id/calcul_btn_1"
 91                 android:layout_width="70dp"
 92                 android:layout_height="match_parent"
 93                 android:layout_marginTop="0dp"
 94                 android:background="@drawable/riple_btn_nocorner"
 95                 android:fontFamily="宋体"
 96                 android:text="1"
 97                 android:layout_weight="1"
 98                 android:textColor="#169FDF"
 99                 android:textSize="40dp"></Button>
100             <Button
101                 android:id="@ id/calcul_btn_2"
102                 android:layout_width="70dp"
103                 android:layout_height="match_parent"
104                 android:layout_marginTop="0dp"
105                 android:background="@drawable/riple_btn_nocorner"
106                 android:text="2"
107                 android:layout_weight="1"
108                 android:textSize="40dp"
109                 android:textColor="#169FDF"
110                 android:fontFamily="宋体"></Button>
111             <Button
112                 android:id="@ id/calcul_btn_3"
113                 android:layout_width="70dp"
114                 android:layout_height="match_parent"
115                 android:layout_marginTop="0dp"
116                 android:background="@drawable/riple_btn_nocorner"
117                 android:text="3"
118                 android:textSize="40dp"
119                 android:textColor="#169FDF"
120                 android:layout_weight="1"
121                 android:fontFamily="宋体"></Button>
122             <Button
123                 android:id="@ id/calcul_btn_pf"
124                 android:layout_width="70dp"
125                 android:layout_height="match_parent"
126                 android:layout_below="@id/calcul_btn_back"
127                 android:layout_marginTop="0dp"
128                 android:layout_toRightOf="@id/calcul_btn_9"
129                 android:background="@drawable/riple_btn_nocorner"
130                 android:text="x²"
131                 android:textAllCaps="false"
132                 android:textSize="30dp"
133                 android:textColor="#169FDF"
134                 android:layout_weight="1"
135                 android:fontFamily="宋体"></Button>
136             <Button
137                 android:id="@ id/calcul_btn_sqrt"
138                 android:layout_width="70dp"
139                 android:layout_height="match_parent"
140                 android:layout_below="@id/calcul_btn_back"
141                 android:layout_marginTop="0dp"
142                 android:layout_toRightOf="@id/calcul_btn_pf"
143                 android:background="@drawable/riple_btn_nocorner"
144                 android:text="√x"
145                 android:textAllCaps="false"
146                 android:textSize="30dp"
147                 android:textColor="#169FDF"
148                 android:layout_weight="1"
149                 android:fontFamily="宋体"></Button>
150 
151         </LinearLayout>
152         <LinearLayout
153             android:layout_width="match_parent"
154             android:layout_weight="1"
155             android:layout_height="0dp"
156             android:orientation="horizontal"
157             >
158             <Button
159                 android:id="@ id/calcul_btn_4"
160                 android:layout_width="70dp"
161                 android:layout_height="match_parent"
162                 android:layout_marginTop="0dp"
163                 android:background="@drawable/riple_btn_nocorner"
164                 android:text="4"
165                 android:textSize="40dp"
166                 android:layout_weight="1"
167                 android:textColor="#169FDF"
168                 android:fontFamily="宋体"></Button>
169             <Button
170                 android:id="@ id/calcul_btn_5"
171                 android:layout_width="70dp"
172                 android:layout_height="match_parent"
173                 android:layout_marginTop="0dp"
174                 android:background="@drawable/riple_btn_nocorner"
175                 android:text="5"
176                 android:textSize="40dp"
177                 android:textColor="#169FDF"
178                 android:layout_weight="1"
179                 android:fontFamily="宋体"></Button>
180             <Button
181                 android:id="@ id/calcul_btn_6"
182                 android:layout_width="70dp"
183                 android:layout_height="match_parent"
184                 android:layout_marginTop="0dp"
185                 android:background="@drawable/riple_btn_nocorner"
186                 android:text="6"
187                 android:textSize="40dp"
188                 android:textColor="#169FDF"
189                 android:layout_weight="1"
190                 android:fontFamily="宋体"></Button>
191             <Button
192                 android:id="@ id/calcul_btn_add"
193                 android:layout_width="70dp"
194                 android:layout_height="match_parent"
195                 android:layout_marginTop="0dp"
196                 android:background="@drawable/riple_btn_nocorner"
197                 android:text=" "
198                 android:textSize="40dp"
199                 android:textColor="#169FDF"
200                 android:fontFamily="宋体"
201                 android:layout_weight="1"
202                 ></Button>
203             <Button
204                 android:id="@ id/calcul_btn_multiply"
205                 android:layout_width="70dp"
206                 android:layout_height="match_parent"
207                 android:layout_weight="1"
208                 android:layout_marginTop="0dp"
209                 android:background="@drawable/riple_btn_nocorner"
210                 android:text="×"
211                 android:textSize="40dp"
212                 android:textColor="#169FDF"
213                 android:fontFamily="宋体"></Button></LinearLayout>
214         <LinearLayout
215             android:layout_width="match_parent"
216             android:layout_weight="1"
217             android:layout_height="0dp"
218             android:orientation="horizontal"
219             >
220           
221             <Button
222                 android:id="@ id/calcul_btn_7"
223                 android:layout_width="70dp"
224                 android:layout_height="match_parent"
225                 android:layout_below="@id/calcul_btn_4"
226                 android:layout_marginTop="0dp"
227                 android:layout_toLeftOf="@id/calcul_btn_5"
228                 android:background="@drawable/riple_btn_nocorner"
229                 android:text="7"
230                 android:textSize="40dp"
231                 android:layout_weight="1"
232                 android:textColor="#169FDF"
233                 android:fontFamily="宋体"></Button>
234             <Button
235                 android:id="@ id/calcul_btn_8"
236                 android:layout_width="70dp"
237                 android:layout_height="match_parent"
238                 android:layout_below="@id/calcul_btn_5"
239                 android:layout_marginTop="0dp"
240                 android:layout_toRightOf="@id/calcul_btn_7"
241                 android:background="@drawable/riple_btn_nocorner"
242                 android:text="8"
243                 android:layout_weight="1"
244                 android:textSize="40dp"
245                 android:textColor="#169FDF"
246                 android:fontFamily="宋体"></Button>
247             <Button
248                 android:id="@ id/calcul_btn_9"
249                 android:layout_width="70dp"
250                 android:layout_height="match_parent"
251                 android:layout_below="@id/calcul_btn_6"
252                 android:layout_marginTop="0dp"
253                 android:layout_toRightOf="@id/calcul_btn_8"
254                 android:background="@drawable/riple_btn_nocorner"
255                 android:text="9"
256                 android:layout_weight="1"
257                 android:textColor="#169FDF"
258                 android:textSize="40dp"
259                 android:fontFamily="宋体"></Button>
260             <Button
261                 android:id="@ id/calcul_btn_sub"
262                 android:layout_width="70dp"
263                 android:layout_height="match_parent"
264                 android:layout_marginTop="0dp"
265                 android:background="@drawable/riple_btn_nocorner"
266                 android:text="-"
267                 android:textSize="40dp"
268                 android:textColor="#169FDF"
269                 android:layout_weight="1"
270                 android:fontFamily="宋体"></Button>
271             <Button
272                 android:id="@ id/calcul_btn_divide"
273                 android:layout_width="70dp"
274                 android:layout_height="match_parent"
275                 android:layout_marginTop="0dp"
276                 android:background="@drawable/riple_btn_nocorner"
277                 android:layout_weight="1"
278                 android:text="÷"
279                 android:textSize="40dp"
280                 android:textColor="#169FDF"
281                 android:fontFamily="宋体"></Button>
282         </LinearLayout>
283         <LinearLayout
284             android:layout_width="match_parent"
285             android:layout_weight="1"
286             android:layout_height="0dp"
287             android:orientation="horizontal"
288             >
289             <Button
290                 android:id="@ id/calcul_btn_rem"
291                 android:layout_width="70dp"
292                 android:layout_height="match_parent"
293                 android:layout_below="@id/calcul_btn_divide"
294                 android:layout_marginTop="0dp"
295                 android:layout_toLeftOf="@id/calcul_btn_0"
296                 android:background="@drawable/riple_btn_nocorner"
297                 android:text="%"
298                 android:layout_weight="1"
299                 android:textSize="40dp"
300                 android:textColor="#169FDF"
301                 android:fontFamily="宋体"></Button>
302             <Button
303                 android:id="@ id/calcul_btn_0"
304                 android:layout_width="70dp"
305                 android:layout_height="match_parent"
306                 android:layout_below="@id/calcul_btn_8"
307                 android:layout_marginTop="0dp"
308                 android:layout_toRightOf="@id/calcul_btn_7"
309                 android:background="@drawable/riple_btn_nocorner"
310                 android:text="0"
311                 android:textSize="40dp"
312                 android:layout_weight="1"
313                 android:fontFamily="宋体"
314                 android:textColor="#169FDF"
315                 ></Button>
316             <Button
317                 android:id="@ id/calcul_btn_point"
318                 android:layout_width="70dp"
319                 android:layout_height="match_parent"
320                 android:layout_below="@id/calcul_btn_9"
321                 android:layout_marginTop="0dp"
322                 android:layout_toRightOf="@id/calcul_btn_0"
323                 android:background="@drawable/riple_btn_nocorner"
324                 android:text="."
325                 android:textAllCaps="false"
326                 android:layout_weight="1"
327                 android:textSize="70dp"
328                 android:textColor="#169FDF"
329                 android:fontFamily="宋体"></Button>
330             <Button
331                 android:id="@ id/calcul_btn_equal"
332                 android:layout_width="140dp"
333                 android:layout_height="match_parent"
334                 android:layout_below="@id/calcul_btn_divide"
335                 android:layout_marginTop="0dp"
336                 android:layout_toRightOf="@id/calcul_btn_neg"
337                 android:background="@drawable/riple_btn_nocorner"
338                 android:text="="
339                 android:textSize="40dp"
340                 android:layout_weight="2"
341                 android:textColor="#169FDF"
342                 android:fontFamily="宋体"></Button>
343         </LinearLayout>
344     </LinearLayout>
345 </RelativeLayout>

 

Calculater.java

  1 package com.Appalication_Trial;
  2 
  3 import androidx.appcompat.app.AppCompatActivity;
  4 
  5 import android.os.Bundle;
  6 import android.util.Log;
  7 import android.view.View;
  8 import android.widget.Button;
  9 import android.widget.TextView;
 10 import android.widget.Toast;
 11 
 12 import com.example.myapplication5.R;
 13 
 14 import java.util.ArrayList;
 15 
 16 import static java.lang.Math.pow;
 17 import static java.lang.Math.sqrt;
 18 
 19 public class Calculator extends AppCompatActivity {
 20     private Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_add,
 21             btn_del, btn_mul, btn_neg, btn_ac, btn_sub, btn_equal, btn_divide, btn_sqrt, btn_rem,
 22             btn_point, btn_pf, btn_back;
 23     private TextView tv_cord, tv_calcul;
 24     private double init = 0;//当前输入数据
 25     private char calcul = ‘#‘;//运算符
 26     private double nowsum = 0;//当前运算结果
 27     private int pointnum = 0;//小数点输入次数
 28     boolean isaheadchar = false, isnowpoint = false, isaheadequal = false, isaheadsqr = false;
 29     //isaheadchar: 是否输入过运算符
 30     //isnowpoint:小数点状态
 31     //isaheadequal:前一个运算符是否为=
 32     //isaheadsqr:前一个运算符是否为平方,主要为setText方便而设置
 33     ArrayList<Double> arr = new ArrayList<>();
 34 
 35     @Override
 36     protected void onCreate(Bundle savedInstanceState) {
 37         super.onCreate(savedInstanceState);
 38         setContentView(R.layout.activity_calculator);
 39         btn_0 = findViewById(R.id.calcul_btn_0);
 40         btn_1 = findViewById(R.id.calcul_btn_1);
 41         btn_2 = findViewById(R.id.calcul_btn_2);
 42         btn_3 = findViewById(R.id.calcul_btn_3);
 43         btn_4 = findViewById(R.id.calcul_btn_4);
 44         btn_5 = findViewById(R.id.calcul_btn_5);
 45         btn_6 = findViewById(R.id.calcul_btn_6);
 46         btn_7 = findViewById(R.id.calcul_btn_7);
 47         btn_8 = findViewById(R.id.calcul_btn_8);
 48         btn_9 = findViewById(R.id.calcul_btn_9);
 49         btn_point = findViewById(R.id.calcul_btn_point);
 50         btn_pf = findViewById(R.id.calcul_btn_pf);
 51         btn_back = findViewById(R.id.calcul_btn_back);
 52         btn_sqrt = findViewById(R.id.calcul_btn_sqrt);
 53         btn_sub = findViewById(R.id.calcul_btn_sub);
 54         btn_ac = findViewById(R.id.calcul_btn_ac);
 55         btn_add = findViewById(R.id.calcul_btn_add);
 56         btn_equal = findViewById(R.id.calcul_btn_equal);
 57         btn_del = findViewById(R.id.calcul_btn_del);
 58         btn_divide = findViewById(R.id.calcul_btn_divide);
 59         btn_mul = findViewById(R.id.calcul_btn_multiply);
 60         btn_rem = findViewById(R.id.calcul_btn_rem);
 61         btn_neg = findViewById(R.id.calcul_btn_neg);
 62         tv_calcul = findViewById(R.id.tv_calcul);
 63         tv_cord = findViewById(R.id.tv_cord);
 64         tv_calcul.setText("0");
 65         tv_cord.setText("");
 66         for (int i = 0; i <= 9; i  )
 67             btn_0.setOnClickListener(new View.OnClickListener() {
 68                 @Override
 69                 public void onClick(View v) {
 70                     if (!isnowpoint)
 71                         init = init * 10   0;
 72                     else {
 73                         double num = 1;
 74                         pointnum  ;
 75                         for (int i = 0; i < pointnum; i  )
 76                             num /= 10;
 77                         init  = 0 * num;
 78                     }
 79                     tv_calcul.setText(""   init);
 80                     isaheadequal = false;
 81                     isaheadchar = false;
 82                 }
 83             });
 84         btn_1.setOnClickListener(new View.OnClickListener() {
 85             @Override
 86             public void onClick(View v) {
 87                 if (!isnowpoint)
 88                     init = init * 10   1;
 89                 else {
 90                     double num = 1;
 91                     pointnum  ;
 92                     for (int i = 0; i < pointnum; i  )
 93                         num /= 10;
 94                     init  = 1 * num;
 95                 }
 96                 tv_calcul.setText(""   init);
 97                 isaheadequal = false;
 98                 isaheadchar = false;
 99             }
100         });
101         btn_2.setOnClickListener(new View.OnClickListener() {
102             @Override
103             public void onClick(View v) {
104                 if (!isnowpoint)
105                     init = init * 10   2;
106                 else {
107                     double num = 1;
108                     pointnum  ;
109                     for (int i = 0; i < pointnum; i  )
110                         num /= 10;
111                     init  = 2 * num;
112                 }
113                 tv_calcul.setText(""   init);
114                 isaheadequal = false;
115                 isaheadchar = false;
116             }
117         });
118         btn_3.setOnClickListener(new View.OnClickListener() {
119             @Override
120             public void onClick(View v) {
121                 if (!isnowpoint)
122                     init = init * 10   3;
123                 else {
124                     double num = 1;
125                     pointnum  ;
126                     for (int i = 0; i < pointnum; i  )
127                         num /= 10;
128                     init  = 3 * num;
129                 }
130                 tv_calcul.setText(""   init);
131                 isaheadequal = false;
132                 isaheadchar = false;
133             }
134         });
135         btn_4.setOnClickListener(new View.OnClickListener() {
136             @Override
137             public void onClick(View v) {
138                 if (!isnowpoint)
139                     init = init * 10   4;
140                 else {
141                     double num = 1;
142                     pointnum  ;
143                     for (int i = 0; i < pointnum; i  )
144                         num /= 10;
145                     init  = 4 * num;
146                 }
147                 tv_calcul.setText(""   init);
148                 isaheadequal = false;
149                 isaheadchar = false;
150             }
151         });
152         btn_5.setOnClickListener(new View.OnClickListener() {
153             @Override
154             public void onClick(View v) {
155                 if (!isnowpoint)
156                     init = init * 10   5;
157                 else {
158                     double num = 1;
159                     pointnum  ;
160                     for (int i = 0; i < pointnum; i  )
161                         num /= 10;
162                     init  = 5 * num;
163                 }
164                 tv_calcul.setText(""   init);
165                 isaheadequal = false;
166                 isaheadchar = false;
167             }
168         });
169         btn_6.setOnClickListener(new View.OnClickListener() {
170             @Override
171             public void onClick(View v) {
172                 if (!isnowpoint)
173                     init = init * 10   6;
174                 else {
175                     double num = 1;
176                     pointnum  ;
177                     for (int i = 0; i < pointnum; i  )
178                         num /= 10;
179                     init  = 6 * num;
180                 }
181                 tv_calcul.setText(""   init);
182                 isaheadequal = false;
183                 isaheadchar = false;
184             }
185         });
186         btn_7.setOnClickListener(new View.OnClickListener() {
187             @Override
188             public void onClick(View v) {
189                 if (!isnowpoint)
190                     init = init * 10   7;
191                 else {
192                     double num = 1;
193                     pointnum  ;
194                     for (int i = 0; i < pointnum; i  )
195                         num /= 10;
196                     init  = 7 * num;
197                 }
198                 tv_calcul.setText(""   init);
199                 isaheadequal = false;
200                 isaheadchar = false;
201             }
202         });
203         btn_8.setOnClickListener(new View.OnClickListener() {
204             @Override
205             public void onClick(View v) {
206                 if (!isnowpoint)
207                     init = init * 10   8;
208                 else {
209                     double num = 1;
210                     pointnum  ;
211                     for (int i = 0; i < pointnum; i  )
212                         num /= 10;
213                     init  = 8 * num;
214                 }
215                 tv_calcul.setText(""   init);
216                 isaheadequal = false;
217                 isaheadchar = false;
218             }
219         });
220         btn_9.setOnClickListener(new View.OnClickListener() {
221             @Override
222             public void onClick(View v) {
223                 if (!isnowpoint)
224                     init = init * 10   9;
225                 else {
226                     double num = 1;
227                     pointnum  ;
228                     for (int i = 0; i < pointnum; i  )
229                         num /= 10;
230                     init  = 9 * num;
231                 }
232                 tv_calcul.setText(""   init);
233                 isaheadequal = false;
234                 isaheadchar = false;
235             }
236         });
237         btn_add.setOnClickListener(new View.OnClickListener() {
238             @Override
239             public void onClick(View v) {
240 
241                 switch (calcul) {
242                     case ‘ ‘: {
243                         nowsum  = init;
244                         arr.add(nowsum);
245                         break;
246                     }
247                     case ‘-‘: {
248                         nowsum -= init;
249                         arr.add(nowsum);
250 
251                         break;
252                     }
253                     case ‘*‘: {
254                         nowsum *= init;
255                         arr.add(nowsum);
256 
257                         break;
258                     }
259                     case ‘/‘: {
260                         if (init != 0) {
261                             nowsum /= init;
262                             arr.add(nowsum);
263                         } else
264                             Toast.makeText(getApplicationContext(), "不能除0!", Toast.LENGTH_LONG).show();
265                         break;
266                     }
267                     case ‘士‘: {
268                         nowsum *= -1;
269                         arr.add(nowsum);
270                         break;
271                     }
272                     case ‘%‘: {
273                         nowsum %= init;
274                         arr.add(nowsum);
275                         break;
276                     }
277                     default: {
278                         nowsum  = init;
279                     }
280                 }
281                 if (isaheadchar) {
282                     if (!isaheadsqr)
283                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
284                     calcul = ‘ ‘;
285 
286                 }
287                 if (!isaheadequal) {
288                     if (!isaheadchar)
289                         tv_cord.setText(tv_cord.getText()   ""   init   " ");
290                     else tv_cord.setText(tv_cord.getText()   " ");
291                     tv_calcul.setText(""   nowsum);
292                 } else {
293                     tv_cord.setText(""   nowsum   " ");
294                     tv_calcul.setText(""   nowsum);
295                 }
296                 calcul = ‘ ‘;
297                 init = 0;
298                 isaheadequal = false;
299                 isaheadchar = true;
300                 isaheadsqr = false;
301                 isnowpoint = false;
302                 pointnum = 0;
303             }
304         });
305         btn_sub.setOnClickListener(new View.OnClickListener() {
306             @Override
307             public void onClick(View v) {
308                 if (isaheadequal) {
309                     tv_cord.setText("");
310                 }
311                 switch (calcul) {
312                     case ‘ ‘: {
313                         nowsum  = init;
314                         arr.add(nowsum);
315 
316                         break;
317                     }
318                     case ‘-‘: {
319                         nowsum -= init;
320                         arr.add(nowsum);
321 
322                         break;
323                     }
324                     case ‘*‘: {
325                         nowsum *= init;
326                         arr.add(nowsum);
327 
328                         break;
329                     }
330                     case ‘/‘: {
331                         if (init != 0) {
332                             nowsum /= init;
333                             arr.add(nowsum);
334                         } else
335                             Toast.makeText(getApplicationContext(), "不能除0!", Toast.LENGTH_LONG).show();
336                         break;
337                     }
338                     case ‘士‘: {
339                         nowsum *= -1;
340                         arr.add(nowsum);
341                         break;
342                     }
343                     case ‘%‘: {
344                         nowsum %= init;
345                         arr.add(nowsum);
346                         break;
347                     }
348                     default: {
349                         nowsum  = init;
350                     }
351                 }
352                 if (isaheadchar) {
353                     if (!isaheadsqr)
354                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
355                     calcul = ‘-‘;
356                 }
357                 if (!isaheadequal) {
358                     if (!isaheadchar)
359                         tv_cord.setText(tv_cord.getText()   ""   init   "-");
360                     else tv_cord.setText(tv_cord.getText()   "-");
361                     tv_calcul.setText(""   nowsum);
362                 } else {
363                     tv_cord.setText(""   nowsum   "-");
364                     tv_calcul.setText(""   nowsum);
365                 }
366                 calcul = ‘-‘;
367                 init = 0;
368                 isaheadequal = false;
369                 isaheadchar = true;
370                 isaheadsqr = false;
371                 isnowpoint = false;
372                 pointnum = 0;
373             }
374         });
375         btn_mul.setOnClickListener(new View.OnClickListener() {
376             @Override
377             public void onClick(View v) {
378                 if (isaheadequal) {
379                     tv_cord.setText("");
380                 }
381                 switch (calcul) {
382                     case ‘ ‘: {
383                         nowsum  = init;
384                         arr.add(nowsum);
385 
386                         break;
387                     }
388                     case ‘-‘: {
389                         nowsum -= init;
390                         arr.add(nowsum);
391 
392                         break;
393                     }
394                     case ‘*‘: {
395                         nowsum *= init;
396                         arr.add(nowsum);
397                         break;
398                     }
399                     case ‘/‘: {
400                         if (init != 0) {
401                             nowsum /= init;
402                             arr.add(nowsum);
403                         } else
404                             Toast.makeText(getApplicationContext(), "不能除0!", Toast.LENGTH_LONG).show();
405                         break;
406                     }
407                     case ‘士‘: {
408                         nowsum *= -1;
409                         arr.add(nowsum);
410                         break;
411                     }
412                     case ‘%‘: {
413                         nowsum %= init;
414                         arr.add(nowsum);
415                         break;
416                     }
417                     default: {
418                         nowsum  = init;
419                     }
420                 }
421                 //if ptr calcul is calcul change it
422                 if (isaheadchar) {
423                     if (!isaheadsqr)
424                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
425                     calcul = ‘*‘;
426                 }
427                 if (!isaheadequal) {
428                     if (!isaheadchar)
429                         tv_cord.setText(tv_cord.getText()   ""   init   "×");
430                     else tv_cord.setText(tv_cord.getText()   "×");
431                     tv_calcul.setText(""   nowsum);
432                 } else {
433                     tv_cord.setText(""   nowsum   "×");
434                     tv_calcul.setText(""   nowsum);
435                 }
436                 calcul = ‘*‘;
437                 init = 0;
438                 isaheadchar = true;
439                 isaheadequal = false;
440                 isaheadsqr = false;
441                 isnowpoint = false;
442                 pointnum = 0;
443             }
444         });
445         btn_divide.setOnClickListener(new View.OnClickListener() {
446             @Override
447             public void onClick(View v) {
448                 if (isaheadequal) {
449                     tv_cord.setText("");
450                 }
451                 switch (calcul) {
452                     case ‘ ‘: {
453                         nowsum  = init;
454                         arr.add(nowsum);
455 
456                         break;
457                     }
458                     case ‘-‘: {
459                         nowsum -= init;
460                         arr.add(nowsum);
461 
462                         break;
463                     }
464                     case ‘*‘: {
465                         nowsum *= init;
466                         arr.add(nowsum);
467 
468                         break;
469                     }
470                     case ‘/‘: {
471                         if (init != 0) {
472                             nowsum /= init;
473                             arr.add(nowsum);
474                         } else
475                             Toast.makeText(getApplicationContext(), "不能除0!", Toast.LENGTH_LONG).show();
476                         break;
477                     }
478                     case ‘士‘: {
479                         nowsum *= -1;
480                         arr.add(nowsum);
481                         break;
482                     }
483                     case ‘%‘: {
484                         nowsum %= init;
485                         arr.add(nowsum);
486                         break;
487                     }
488                     default: {
489                         nowsum  = init;
490                     }
491                 }
492                 if (isaheadchar) {
493                     if (!isaheadsqr)
494                         tv_cord.setText(tv_cord.getText().subSequence(0, tv_cord.getText().length() - 1));
495                     calcul = ‘/‘;
496                 }
497                 if (!isaheadequal) {
498                     if (!isaheadchar)
499                         tv_cord.setText(tv_cord.getText()   ""   init   "÷");
500                     else tv_cord.setText(tv_cord.getText()   "÷");
501                     tv_calcul.setText(""   nowsum);
502                 } else {
503                     tv_cord.setText(""   nowsum   "÷");
504                     tv_calcul.setText(""   nowsum);
505                 }
506                 calcul = ‘/‘;
507                 init = 0;
508                 isaheadequal = false;
509                 isaheadchar = true;
510                 isaheadsqr = false;
511                 isnowpoint = false;
512                 pointnum = 0;
513             }
514         });
515         btn_rem.setOnClickListener(new View.OnClickListener() {
516             @Override
517             public void onClick(View v) {
518                 if (isaheadequal) {
519                     tv_cord.setText("");
520                 }
521                 switch (calcul) {
522                     case ‘ ‘: {
523                         nowsum  = init;
524                         arr.add(nowsum);
525 
526                         break;
527                     }
528                     case ‘-‘: {
529                         nowsum -= init;
530                         arr.add(nowsum);
531 
532                         break;
533                     }
534                     case ‘*‘: {
535                         nowsum *= init;
536                         arr.add(nowsum);
537 
538                         break;
539                     }
540                     case ‘/‘: {
541                         if (init != 0) {
542                             nowsum /= init;
543                             arr.add(nowsum);
544                         } else
545                             Toast.makeText(getApplicationContext(), "不能除0!", Toast.LENGTH_LONG).show();
546                         break;
547                     }
548                     case ‘士‘: {
549                         nowsum *= -1;
550                         arr.add(nowsum);
551                         break;
552                     }
553                     case ‘%‘: {
554                         nowsum %= init;
555                         arr.add(nowsum);
556                         break;
557                     }
558                     default: {
559                     }
560                 }
561                 if (!isaheadequal) {
562                     tv_cord.setText(tv_cord.getText()   ""   init   "%");
563                     tv_calcul.setText(""   nowsum);
564                 } else {
565                     tv_cord.setText(""   nowsum   "%");
566                     tv_calcul.setText(""   nowsum);
567                     isaheadequal = false;
568                 }
569                 calcul = ‘%‘;
570                 init = 0;
571                 isnowpoint = false;
572                 pointnum = 0;
573             }
574         });
575         btn_neg.setOnClickListener(new View.OnClickListener() {
576             @Override
577             public void onClick(View v) {
578                 double nowcal = Double.parseDouble(tv_calcul.getText().toString());
579                 nowcal *= -1;
580                 init = nowcal;
581                 tv_calcul.setText(""   init);
582                 isnowpoint = false;
583                 pointnum = 0;
584             }
585         });
586         btn_point.setOnClickListener(new View.OnClickListener() {
587             @Override
588             public void onClick(View v) {
589                 isnowpoint = true;
590             }
591         });
592         btn_equal.setOnClickListener(new View.OnClickListener() {
593             @Override
594             public void onClick(View v) {
595                 if (isaheadequal) {
596                     tv_cord.setText("");
597                 }
598                 switch (calcul) {
599                     case ‘ ‘: {
600                         nowsum  = init;
601                         arr.add(nowsum);
602                         break;
603                     }
604                     case ‘-‘: {
605                         nowsum -= init;
606                         arr.add(nowsum);
607                         break;
608                     }
609                     case ‘*‘: {
610                         nowsum *= init;
611                         arr.add(nowsum);
612 
613                         break;
614                     }
615                     case ‘/‘: {
616                         if (init != 0) {
617                             nowsum /= init;
618                             arr.add(nowsum);
619                         } else
620                             Toast.makeText(getApplicationContext(), "不能除0!", Toast.LENGTH_LONG).show();
621                         break;
622                     }
623                     case ‘士‘: {
624                         nowsum *= -1;
625                         arr.add(nowsum);
626                         break;
627                     }
628                     case ‘%‘: {
629                         nowsum %= init;
630                         arr.add(nowsum);
631                         break;
632                     }
633                     default: {
634                         nowsum = init;
635                     }
636                 }
637                 if (!isaheadsqr)
638                     tv_cord.setText(tv_cord.getText()   ""   init   "=");
639                 else tv_cord.setText(tv_cord.getText()   "=");
640                 tv_calcul.setText(""   nowsum);
641                 calcul = ‘#‘;
642                 init = 0;
643                 isaheadequal = true;
644                 isaheadsqr = false;
645                 isnowpoint = false;
646                 pointnum = 0;
647             }
648         });
649         btn_ac.setOnClickListener(new View.OnClickListener() {
650             @Override
651             public void onClick(View v) {
652                 tv_calcul.setText("");
653                 tv_cord.setText("");
654                 calcul = ‘#‘;
655                 init = 0;
656                 nowsum = 0;
657                 isaheadchar = false;
658                 isnowpoint = false;
659                 isaheadequal = false;
660                 isaheadsqr = false;
661                 pointnum = 0;
662                 arr.removeAll(arr);
663             }
664         });
665         btn_del.setOnClickListener(new View.OnClickListener() {
666             @Override
667             public void onClick(View v) {
668 
669                 if (!isnowpoint)
670                     init = (int) init / 10;
671                 else {
672                     if (tv_calcul.getText().length() > 2) {
673                         tv_calcul.setText(tv_calcul.getText().subSequence(0, tv_calcul.getText().length() - 1));
674                         init = Double.parseDouble(tv_calcul.getText().toString());
675                     } else {
676                         isnowpoint = false;
677                         pointnum = 0;
678                     }
679                 }
680                 tv_calcul.setText(""   init);
681             }
682         });
683         btn_pf.setOnClickListener(new View.OnClickListener() {
684             @Override
685             public void onClick(View v) {
686                 double ptr;
687                 double nowcal = Double.parseDouble(tv_calcul.getText().toString());
688                 nowcal *= nowcal;
689                 ptr = init;
690                 init = nowcal;
691                 tv_calcul.setText(""   init);
692                 tv_cord.setText(tv_cord.getText()   ""   ptr   "²");
693                 isaheadsqr = true;
694                 isaheadchar = true;
695                 pointnum = 0;
696             }
697         });
698         btn_sqrt.setOnClickListener(new View.OnClickListener() {
699             @Override
700             public void onClick(View v) {
701                 double ptr;
702                 double nowcal = Double.parseDouble(tv_calcul.getText().toString());
703                 nowcal = sqrt(nowcal);
704                 ptr = init;
705                 init = nowcal;
706                 tv_calcul.setText(""   init);
707                 tv_cord.setText(tv_cord.getText()   "√"   ptr);
708                 isaheadsqr = true;
709                 isaheadchar = true;
710                 pointnum = 0;
711             }
712         });
713         btn_back.setOnClickListener(new View.OnClickListener() {
714             @Override
715             public void onClick(View v) {
716                 if (arr.size()>=1) {
717                     Log.d("A","111");
718                     arr.remove(arr.size()-1);
719                     nowsum = arr.get(arr.size()-1);
720                     init = 0;
721                     tv_cord.setText(""   nowsum);
722                     tv_calcul.setText(""   nowsum);
723                 }
724             }
725         });
726 
727     }
728 }