Android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器
本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
需求分析及概要設(shè)計(jì)
目的
開發(fā)一個(gè)簡(jiǎn)單的計(jì)算器App,使之能夠完成加減乘除混合運(yùn)算
工具及環(huán)境
使用java語(yǔ)言,在Android studio平臺(tái)上進(jìn)行開發(fā)
功能設(shè)計(jì)
- “+”:實(shí)現(xiàn)兩數(shù)相加
- “-”:實(shí)現(xiàn)兩數(shù)相減
- “*”:實(shí)現(xiàn)兩數(shù)相乘
- “/”:實(shí)現(xiàn)兩數(shù)相除
- “=”:計(jì)算并得出正確結(jié)果
- “C”:清屏
- “Backspace”:倒退
設(shè)計(jì)思路
1、首先設(shè)計(jì)一個(gè)可視化的界面,供用戶輸入數(shù)據(jù)并查看結(jié)果。
2、用戶可通過(guò)點(diǎn)擊相應(yīng)按鈕輸入正確的表達(dá)式(注意:這里只實(shí)現(xiàn)對(duì)正確表達(dá)式的計(jì)算處理),最后按"="得出正確結(jié)果。在計(jì)算過(guò)程中可以通過(guò)點(diǎn)擊倒退鍵修改輸入內(nèi)容,在進(jìn)行下一次的運(yùn)算之前必須先進(jìn)行清零操作。
3、設(shè)計(jì)好的計(jì)算器應(yīng)可以進(jìn)行加減乘除混合四則運(yùn)算,且可以進(jìn)行小數(shù)和整數(shù)運(yùn)算
詳細(xì)設(shè)計(jì)
當(dāng)用戶點(diǎn)擊按鈕時(shí),用SringBuilder變量記錄其輸入的運(yùn)算式,并顯示到文本區(qū)中。
當(dāng)用戶點(diǎn)擊"="時(shí),把文本區(qū)的運(yùn)算式拿出來(lái),首先將它內(nèi)部的一個(gè)一個(gè)字節(jié)拼接成獨(dú)立的運(yùn)算數(shù)和運(yùn)算符,然后存儲(chǔ)在一個(gè)ArrayList數(shù)組中,接著再新建兩個(gè)ArrayList數(shù)組,用來(lái)分別存放運(yùn)算數(shù)和運(yùn)算符,然后遍歷存儲(chǔ)運(yùn)算式的ArrayList數(shù)組,把其中的運(yùn)算數(shù)和運(yùn)算符分別放進(jìn)不同的ArrayList中,每一次放置運(yùn)算符時(shí),都要先和已存在的運(yùn)算符進(jìn)行比較,若要放進(jìn)的運(yùn)算符優(yōu)先級(jí)低于或等于運(yùn)算符數(shù)組中的運(yùn)算符,則彈出一個(gè)運(yùn)算符,并從運(yùn)算數(shù)數(shù)組中彈出兩個(gè)運(yùn)算數(shù),然后進(jìn)行運(yùn)算,并把結(jié)果送入運(yùn)算數(shù)數(shù)組中,直到遇到比自己優(yōu)先級(jí)低的運(yùn)算符或運(yùn)算符數(shù)組為空時(shí),則送入該運(yùn)算符。當(dāng)遍歷到運(yùn)算式末尾時(shí),依次彈出運(yùn)算符中的運(yùn)算符,并對(duì)應(yīng)彈出運(yùn)算數(shù)進(jìn)行運(yùn)算直到運(yùn)算符數(shù)組為空,此時(shí)運(yùn)算數(shù)數(shù)組中只有一個(gè)數(shù)據(jù)就是最終的結(jié)果
代碼
MainAcivity.java
package com.example.qw.calculator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity{ private StringBuilder show_equation=new StringBuilder();//顯示運(yùn)算式 private ArrayList calculate_equation;//計(jì)算式 private int signal=0;//為0 時(shí)表示剛輸入狀態(tài);為1 時(shí)表示當(dāng)前在輸出結(jié)果上繼續(xù)輸入 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 show_equation=new StringBuilder(); calculate_equation=new ArrayList<>(); Button zero=(Button)findViewById(R.id.zero); Button one=(Button)findViewById(R.id.one); Button two=(Button)findViewById(R.id.two); Button three=(Button)findViewById(R.id.three); Button four=(Button)findViewById(R.id.four); Button five=(Button)findViewById(R.id.five); Button six=(Button)findViewById(R.id.six); Button seven=(Button)findViewById(R.id.seven); Button eight=(Button)findViewById(R.id.eight); Button nine=(Button)findViewById(R.id.nine); Button cls=(Button)findViewById(R.id.cls); Button div=(Button)findViewById(R.id.div); Button mul=(Button)findViewById(R.id.mul); Button backspace=(Button)findViewById(R.id.Backspace); Button sub=(Button)findViewById(R.id.sub); Button add=(Button)findViewById(R.id.add); final Button equal=(Button)findViewById(R.id.equal); final Button point=(Button)findViewById(R.id.spot); final EditText result=(EditText)findViewById(R.id.result); result.setCursorVisible(true); disableShowInput(result); //點(diǎn)擊文本框時(shí)光標(biāo)始終在文本末尾 result.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { result.setSelection(result.getText().length()); } }); zero.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ if(!(show_equation.toString().equals("0"))){ if(signal==0){ show_equation.append("0"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("0"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } } }); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("1"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("1"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("2"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("2"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("3"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("3"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); four.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("4"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("4"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); five.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("5"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("5"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); six.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("6"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("6"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); seven.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("7"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("7"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); eight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("8"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("8"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); nine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ show_equation.append("9"); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); show_equation.append("9"); result.setText(show_equation); result.setSelection(result.getText().length()); signal=0; } } }); cls.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { show_equation.delete(0,show_equation.length()); calculate_equation.clear(); signal=0; result.setText(""); } }); backspace.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!(show_equation.toString().equals(""))) { if(signal==0){ show_equation.deleteCharAt(show_equation.length() - 1); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ show_equation.delete(0,show_equation.length()); result.setText(""); signal=0; } } } }); point.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(signal==0){ String a=show_equation.toString(); if(a.equals("")){ show_equation.append("."); result.setText(show_equation); result.setSelection(result.getText().length()); }else{ int i; char t='0'; for(i=a.length();i>0;i--){ t=a.charAt(i-1); if(t=='.'||t=='+'||t=='-'||t=='*'||t=='/') break; } if(i==0){ show_equation.append("."); result.setText(show_equation); result.setSelection(result.getText().length()); }else if(t=='+'||t=='-'||t=='*'||t=='/'){ show_equation.append("."); result.setText(show_equation); result.setSelection(result.getText().length()); } } }else{ show_equation.delete(0,show_equation.length()); show_equation.append("."); result.setText("."); result.setSelection(result.getText().length()); signal=0; } } }); equal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判斷用戶是否輸入了內(nèi)容 if(!show_equation.toString().equals("")){ signal=1; char temp=show_equation.charAt(show_equation.length()-1); if(show_equation.charAt(0)=='-') show_equation.insert(0,"0"); if(temp=='+'||temp=='-') show_equation.append("0"); if(temp=='*'||temp=='/') show_equation.append("1"); StringBuilder temp1=new StringBuilder(); for(int i=0;i<show_equation.length();i++){ if(show_equation.charAt(i)>='0'&&show_equation.charAt(i)<='9'||show_equation.charAt(i)=='.'){ temp1.append(String.valueOf(show_equation.charAt(i))); }else if(show_equation.charAt(i)=='N'){ calculate_equation.add("NaN"); //跳過(guò)2個(gè)字符 i=i+2; }else if(show_equation.charAt(i)=='∞'){ calculate_equation.add("∞"); } else { if(temp1.length()!=0){ calculate_equation.add(temp1.toString()); temp1.delete(0,temp1.length()); } calculate_equation.add(String.valueOf(show_equation.charAt(i))); } } if(temp1.length()!=0){ calculate_equation.add(temp1.toString()); } calculate_equation.add("#"); String temp8=calculate(calculate_equation); result.setText(temp8); result.setSelection(result.getText().length()); show_equation.delete(0,show_equation.length()); calculate_equation.clear(); show_equation.append(temp8); } } }); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判斷用戶是否輸入了內(nèi)容 if(!(show_equation.toString().equals(""))) { signal=0; char temp=show_equation.charAt(show_equation.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show_equation.deleteCharAt(show_equation.length()-1); show_equation.append("+"); } else show_equation.append("+"); result.setText(show_equation); result.setSelection(result.getText().length()); } } }); sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判斷用戶是否輸入了內(nèi)容 if(!(show_equation.toString().equals(""))) { signal=0; char temp=show_equation.charAt(show_equation.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show_equation.deleteCharAt(show_equation.length()-1); show_equation.append("-"); } else show_equation.append("-"); result.setText(show_equation); result.setSelection(result.getText().length()); } } }); mul.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判斷用戶是否輸入了內(nèi)容 if(!(show_equation.toString().equals(""))) { signal=0; char temp=show_equation.charAt(show_equation.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show_equation.deleteCharAt(show_equation.length()-1); show_equation.append("*"); } else show_equation.append("*"); result.setText(show_equation); result.setSelection(result.getText().length()); } } }); div.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //判斷用戶是否輸入了內(nèi)容 if(!(show_equation.toString().equals(""))) { signal=0; char temp=show_equation.charAt(show_equation.length()-1); if(temp=='+'||temp=='-'||temp=='*'||temp=='/') { show_equation.deleteCharAt(show_equation.length()-1); show_equation.append("/"); } else show_equation.append("/"); result.setText(show_equation); result.setSelection(result.getText().length()); } } }); } protected boolean operatorPriorityCompare(char operator1,char operator2) { int o1=0; int o2=0; switch (operator1){ case '+':{o1=0;break;} case '-':{o1=0;break;} case '*':{o1=1;break;} case '/':{o1=1;break;} } switch (operator2){ case '+':{o2=0;break;} case '-':{o2=0;break;} case '*':{o2=1;break;} case '/':{o2=1;break;} } if(o1<=o2) { return false; } else return true; } //相加 public static Double Add(Double d1,Double d2) { if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1+d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1+d2; } BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); return b1.add(b2).doubleValue(); } //相減 public static Double Sub(Double d1,Double d2){ if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1-d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1-d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1*d2; } BigDecimal b1=new BigDecimal(Double.toString(d1)); BigDecimal b2=new BigDecimal(Double.toString(d2)); return b1.subtract(b2).doubleValue(); } //相乘 public static Double Mul(Double d1,Double d2){ if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1*d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1*d2; } BigDecimal b1=new BigDecimal(Double.toString(d1)); BigDecimal b2=new BigDecimal(Double.toString(d2)); return b1.multiply(b2).setScale(8).doubleValue(); } //相除 public static Double Div(Double d1,Double d2){ if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){ return d1/d2; } if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){ return d1/d2; } if(d1==0.0&&d2==0.0){ return Double.NaN; } if(d2==0.0){ return d1/d2; } BigDecimal b1=new BigDecimal(Double.toString(d1)); BigDecimal b2=new BigDecimal(Double.toString(d2)); return b1.divide(b2,8,BigDecimal.ROUND_HALF_UP).doubleValue(); } protected String calculate(ArrayList equation){ Double temp2; Double temp3; Double result; List operator=new ArrayList(); List<Double> operand=new ArrayList(); for(int i=0;i<equation.size();i++) { String temp4=(String) equation.get(i); if(temp4.equals("+")||temp4.equals("-")||temp4.equals("*")||temp4.equals("/")) { if(operator.size()>0) { String temp5=operator.get(operator.size()-1).toString(); while(!(operatorPriorityCompare(temp4.charAt(0),temp5.charAt(0)))&&operator.size()>0) { operator.remove(operator.size()-1); temp3=operand.get(operand.size()-1); operand.remove(operand.size()-1); temp2=operand.get(operand.size()-1); operand.remove(operand.size()-1); switch (temp5.charAt(0)){ case '+':{result=Add(temp2,temp3);operand.add(result);break;} case '-':{result=Sub(temp2,temp3);operand.add(result);break;} case '*':{result=Mul(temp2,temp3);operand.add(result);break;} case '/':{result=Div(temp2,temp3);operand.add(result);break;} } if(operator.size()>0) { temp5=operator.get(operator.size()-1).toString(); } else break; } operator.add(temp4); } else operator.add(temp4); } else if(temp4.equals("#")) { while(operator.size()>0) { String temp6=(String)operator.get(operator.size()-1); operator.remove(operator.size()-1); temp3=operand.get(operand.size()-1); operand.remove(operand.size()-1); temp2=operand.get(operand.size()-1); operand.remove(operand.size()-1); switch (temp6.charAt(0)){ case '+':{result=Add(temp2,temp3);operand.add(result);break;} case '-':{result=Sub(temp2,temp3);operand.add(result);break;} case '*':{result=Mul(temp2,temp3);operand.add(result);break;} case '/':{result=Div(temp2,temp3);operand.add(result);break;} } } } else { if(temp4.equals("NaN")){ operand.add(Double.NaN); }else if(temp4.equals("∞")){ operand.add(Double.POSITIVE_INFINITY); }else{ operand.add(Double.parseDouble(temp4)); } } } if(operand.get(0)==Double.NEGATIVE_INFINITY) return "-∞"; if(operand.get(0)==Double.POSITIVE_INFINITY) return "∞"; return operand.get(0).toString(); } //當(dāng)API最低版小于21時(shí)使用這個(gè)函數(shù)實(shí)現(xiàn)點(diǎn)擊文本框不彈出鍵盤 public void disableShowInput(EditText et) { Class<EditText> cls = EditText.class; Method method; try { method = cls.getMethod("setShowSoftInputOnFocus", boolean.class); method.setAccessible(true); method.invoke(et, false); } catch (Exception e) { e.printStackTrace(); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40sp" android:enabled="false"/> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/cls" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="C" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/div" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="/" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/mul" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="*" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/Backspace" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="Backspace" android:textAllCaps="false" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/seven" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="7" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/eight" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="8" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/nine" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="9" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/sub" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="-" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/four" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="4" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/five" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="5" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/six" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="6" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="+" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/one" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="1" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/two" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="20sp" android:text="2" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="1"> <Button android:id="@+id/zero" android:layout_width="match_parent" android:layout_height="match_parent" android:text="0" android:textSize="20sp" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <Button android:id="@+id/three" android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="1" android:text="3" android:textSize="20sp" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> <Button android:id="@+id/spot" android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="1" android:text="." android:textSize="20sp" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <Button android:id="@+id/equal" android:layout_width="match_parent" android:layout_height="match_parent" android:text="=" android:textSize="20sp" android:background="@drawable/buttonstytle" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>
buttonstytle.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 主體背景顏色值 --> <solid android:color="#666666" /> <!-- 連框?qū)挾群皖伾?--> <stroke android:width="0.01dp" android:color="#FFFFFF" /> </shape>
####結(jié)果分析
啟動(dòng)計(jì)算器并輸入運(yùn)算式“59.0-8/46+2”如下圖:
結(jié)果如下圖:
總結(jié)
這次做計(jì)算器收獲很大,首先我對(duì)Android studio中的布局有了更深刻的認(rèn)識(shí),其次在這次編程中熟悉了怎么設(shè)置斷點(diǎn)調(diào)試以快速的找出問(wèn)題所在。當(dāng)然,這次的作品也是不夠成熟的,因?yàn)闆](méi)有做有關(guān)錯(cuò)誤表達(dá)式的相應(yīng)處理,因?yàn)闀r(shí)間和精力有限,這次只能先做這么多了
注: 今天沒(méi)事又看了一下這個(gè)代碼,發(fā)現(xiàn)問(wèn)題很多,簡(jiǎn)直是慘不忍睹(希望沒(méi)坑到你們)。比如直接按加、減、乘、除和等號(hào)鍵及后退鍵會(huì)閃退,剛開始一直按 “0” 可以一直輸入0,同一個(gè)數(shù)中可以輸入多個(gè)小數(shù)點(diǎn)等等,我感到很慚愧哈,本人能力有限,不過(guò)還是抽時(shí)間又改了一下,修復(fù)了這些bug,另外也優(yōu)化了一些東西,上面貼的代碼我已經(jīng)更新了,GitHub上的源碼我很快也會(huì)更新的,哪里做的不好也希望大家不吝賜教哈 -2018/11/5
鏈接:源代碼下載地址
更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- android studio實(shí)現(xiàn)計(jì)算器
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)
- Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
- Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器APP
- Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)
- android?studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
相關(guān)文章
使用Android studio創(chuàng)建的AIDL編譯時(shí)找不到自定義類的解決辦法
這篇文章主要介紹了使用Android studio創(chuàng)建的AIDL編譯時(shí)找不到自定義類的解決辦法的相關(guān)資料,需要的朋友可以參考下2016-02-02Flutter禁止手機(jī)橫屏的簡(jiǎn)單實(shí)現(xiàn)方法
app默認(rèn)是可以橫屏的,如果需要禁止橫屏話可以參考這篇文章,本文主要給大家介紹了關(guān)于Flutter禁止手機(jī)橫屏的簡(jiǎn)單實(shí)現(xiàn)方法,需要的朋友可以參考下2021-07-07Flutter仿微信通訊錄實(shí)現(xiàn)自定義導(dǎo)航條的示例代碼
某些頁(yè)面比如我們?cè)谶x擇聯(lián)系人或者某個(gè)城市的時(shí)候需要快速定位到我們需要的選項(xiàng),一般都會(huì)需要像微信通訊錄右邊有一個(gè)導(dǎo)航條一樣的功能,本文將利用Flutter實(shí)現(xiàn)這一效果,需要的可以參考一下2022-04-04剖析Android Activity側(cè)滑返回的實(shí)現(xiàn)原理
在很多的App中,都會(huì)發(fā)現(xiàn)利用手指滑動(dòng)事件,進(jìn)行高效且人性化的交互非常有必要,那么它是怎么實(shí)現(xiàn)的呢,本文給大家解析實(shí)現(xiàn)原理,對(duì)Activity側(cè)滑返回實(shí)現(xiàn)代碼感興趣的朋友一起看看吧2021-06-06Android 二維碼 生成和識(shí)別二維碼 附源碼下載
這篇文章主要介紹了Android 生成和識(shí)別二維碼的方法,提供源碼下載,需要的朋友可以參考下。2016-06-06Android 復(fù)制文本內(nèi)容到系統(tǒng)剪貼板的最簡(jiǎn)單實(shí)例(分享)
下面小編就為大家?guī)?lái)一篇Android 復(fù)制文本內(nèi)容到系統(tǒng)剪貼板的最簡(jiǎn)單實(shí)例(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04