亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android實現簡易計算器(可以實現連續計算)

瀏覽:39日期:2022-09-25 09:54:16

發一個庫存程序,好像是幾個禮拜之前寫的吧,是一個用安卓實現的簡易的計算器,寫這個小程序之前,看了很多人寫的計算器,覺得使用一個 EditText,并將它設置為不可編寫,是比較好的解決方案。

設計思路主要是: 根據用戶的點擊,在一個 EditText 中顯示用戶輸入的運算步驟,例如 1 * 5 + 8 - 5 , 這個運算步驟首先是字符串類型的,然后在經過系列步驟將字符串解析成為相應的實數計算,最終得出結果

我是用了兩個 EditText ,第一個顯示運算步驟(字符串類型),第二個專門用了保存要參與運算的數字,并實時對這個數字進行更新;

對于: “操作數 操作運算符 操作數”,可以定義一個數組來保存這兩操作數,進行運算之后,將結果存儲到數組的第一個元素,方便進行連續運算,然后下一個操作數存儲到數組的第二個元素,‘’‘’ 這樣就實現了連續運算

在實現的過程當中,多處用到了類型轉換,從字符串轉換成浮點數,從浮點數轉換成字符串等,進行類型轉換是要特別小心,我就是因為類型轉換是寫錯了,查bug查了老半天

效果圖就是這樣滴:

Android實現簡易計算器(可以實現連續計算)

有幾個小bug帶修復:

1.運算沒有優先級,完全是按用戶輸入的步驟來進行運算2.連續按兩次運算操作符會閃退,剛開始是按操作符也會閃退3.其中的正負數轉換按鈕還沒實行

由于最近要期中考試了,所以這幾個小bug過一段時間再來修復,到時再更新

下面是代碼:

MainActivity.java 文件

package com.example.calculator;import android.content.Intent;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageButton;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ getResult2 result2 = new getResult2(); Button button0; Button button1; Button button2; Button button3; Button button4; Button button5; Button button6; Button button7; Button button8; Button button9; Button button_point; //小數點 Button button_clear; //清空 //2個imageButton Button button_plus; Button button_minus; Button button_mutiply; Button button_divide; ImageButton button_equal; //等于 ImageButton button_delete; //刪除(退格) EditText edit_input; //輸入框 EditText editText2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.hide(); } ImageButton imageButton1 = (ImageButton) findViewById(R.id.title_imageButton1); imageButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SetActivity.class); startActivity(intent); } }); //實例化按鈕 button0 = (Button) findViewById(R.id.button0); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); button5 = (Button) findViewById(R.id.button5); button6 = (Button) findViewById(R.id.button6); button7 = (Button) findViewById(R.id.button7); button8 = (Button) findViewById(R.id.button8); button9 = (Button) findViewById(R.id.button9); button_point = (Button) findViewById(R.id.button_point); button_clear = (Button) findViewById(R.id.button_clear); button_plus = (Button) findViewById(R.id.button_plus); button_minus = (Button) findViewById(R.id.button_minus); button_mutiply = (Button) findViewById(R.id.button_mutiply); button_divide = (Button) findViewById(R.id.button_divide); button_equal = (ImageButton) findViewById(R.id.button_equal); button_delete = (ImageButton) findViewById(R.id.button_delete); edit_input = (EditText) findViewById(R.id.main_ediText); editText2 = (EditText) findViewById(R.id.edtiText2); //設置點擊事件 button0.setOnClickListener((View.OnClickListener) this); button1.setOnClickListener((View.OnClickListener) this); button2.setOnClickListener((View.OnClickListener) this); button3.setOnClickListener((View.OnClickListener) this); button4.setOnClickListener((View.OnClickListener) this); button5.setOnClickListener((View.OnClickListener) this); button6.setOnClickListener((View.OnClickListener) this); button7.setOnClickListener((View.OnClickListener) this); button8.setOnClickListener((View.OnClickListener) this); button9.setOnClickListener((View.OnClickListener) this); button_point.setOnClickListener((View.OnClickListener) this); button_clear.setOnClickListener((View.OnClickListener) this); button_plus.setOnClickListener((View.OnClickListener) this); button_minus.setOnClickListener((View.OnClickListener) this); button_mutiply.setOnClickListener((View.OnClickListener) this); button_divide.setOnClickListener((View.OnClickListener) this); button_equal.setOnClickListener((View.OnClickListener) this); button_delete.setOnClickListener((View.OnClickListener) this); button_clear.setOnClickListener((View.OnClickListener) this); } @Override public void onClick(View v) { //str用來保存第一個EditText中的字符串 String str = edit_input.getText().toString(); //str2用來保存第二個EditText中的字符串 String str2 = editText2.getText().toString(); switch (v.getId()) { case R.id.button0: case R.id.button1: case R.id.button2: case R.id.button3: case R.id.button4: case R.id.button5: case R.id.button6: case R.id.button7: case R.id.button8: case R.id.button9: case R.id.button_point: edit_input.setText(str + ((Button) v).getText()); editText2.setText(str2 + ((Button) v).getText()); break; // + - * / 對應的值依次為 1 2 3 4,將值傳入setOperation中,就執行相應的運算 case R.id.button_plus: result2.setNumber(editText2.getText().toString()); //設置操作數 result2.getResult(); result2.setOperation(1); edit_input.setText(str + ' ' + ((Button) v).getText() + ' '); //加上空格更美觀 str2 = ''; editText2.setText(str2); //清空textView break; case R.id.button_minus: result2.setNumber(editText2.getText().toString()); //設置操作數 result2.getResult(); result2.setOperation(2); edit_input.setText(str + ' ' + ((Button) v).getText() + ' '); //加上空格更美觀 str2 = ''; editText2.setText(str2); //清空textView break; case R.id.button_mutiply: result2.setNumber(editText2.getText().toString()); //設置操作數 result2.getResult(); result2.setOperation(3); //設置操作符 edit_input.setText(str + ' ' + ((Button) v).getText() + ' '); //加上空格更美觀 str2 = ''; editText2.setText(str2); //清空textView break; case R.id.button_divide: result2.setNumber(editText2.getText().toString()); //設置操作數 result2.getResult(); result2.setOperation(4); edit_input.setText(str + ' ' + ((Button) v).getText() + ' '); //加上空格更美觀 str2 = ''; editText2.setText(str2); //清空textView break; case R.id.button_delete: if (str != null && !str.equals('')) { //substring用來截取字符串的長度 if (str.substring(str.length() - 1, str.length()) == ' ') { //如果最后一個字符是空格,則刪除最后兩個字符,且eidtText2中字符串不發生變化 edit_input.setText(str.substring(0, str.length() - 2)); } else { //如果最后一個字符是數字 edit_input.setText(str.substring(0, str.length() - 1)); //將EditText2中的字符取出,去掉最后一個字符之后再存入 String c2 = editText2.getText().toString(); String c3 = c2.substring(0, c2.length() - 1); editText2.setText(c3); } } break; case R.id.button_clear: result2.setDoubleA1(0); result2.setDoubleA2(0); result2.setA1(' '); result2.setA2(' '); edit_input.setText(''); editText2.setText(''); break; case R.id.button_equal: result2.setNumber(editText2.getText().toString()); double r = result2.getResult(); String r2 = String.valueOf(r); editText2.setText(r2); result2.setA1(' '); result2.setA2(' '); str2 = ''; break; } }

activity_main.xml 文件:

這里我用的是線性布局,同樣也可以用網格布局

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android:orientation='vertical' android:layout_width='match_parent' android:layout_height='match_parent'> <include layout='@layout/title'/> <EditText android: android:editable='false' android:layout_weight='1' android:layout_width='match_parent' android:layout_height='0dp' /> <EditText android: android:editable='false' android:layout_weight='1' android:layout_width='match_parent' android:layout_height='0dp' /> <LinearLayout android: android:orientation='vertical' android:layout_width='match_parent' android:layout_height='0dp' android:layout_weight='4' android:background='#e4e4e4'> <LinearLayout android:layout_width='match_parent' android:layout_height='0dp' android:layout_weight='1' android:background='#e4e4e4' tools:ignore='Suspicious0dp'> <Button android: android:text='C' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_gravity='center' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <ImageButton android: android:scaleType='center' android:src='http://www.aoyou183.cn/bcjs/@drawable/imag1' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <ImageButton android: android:src='http://www.aoyou183.cn/bcjs/@drawable/imag2' android:textSize='24sp' android:layout_gravity='center' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='+' android:textSize='30sp' android:textColor='#fff' android:layout_gravity='center' android:gravity='center' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> </LinearLayout> <LinearLayout android:background='#e4e4e4' android:layout_weight='1' android:layout_width='match_parent' android:layout_height='0dp'> <Button android: android:text='7' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:textSize='30sp' android:textColor='#fff' android:text='8' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='9' android:textColor='#fff' android:textSize='30sp' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='-' android:textColor='#fff' android:textSize='30sp' android:gravity='center' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> </LinearLayout> <LinearLayout android:background='#e4e4e4' android:layout_weight='1' android:layout_width='match_parent' android:layout_height='0dp'> <Button android: android:text='4' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='5' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='6' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='*' android:textColor='#fff' android:textSize='30sp' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> </LinearLayout> <LinearLayout android:background='#e4e4e4' android:layout_weight='1' android:layout_width='match_parent' android:layout_height='0dp'> <Button android: android:text='1' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='2' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='4' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='/' android:textColor='#fff' android:textSize='24sp' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> </LinearLayout> <LinearLayout android:layout_weight='1' android:layout_width='match_parent' android:layout_height='0dp'> <Button android: android:text='0' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='2' android:layout_width='0dp' android:layout_height='match_parent' /> <Button android: android:text='.' android:textSize='30sp' android:textColor='#fff' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> <ImageButton android: android:src='http://www.aoyou183.cn/bcjs/@drawable/imag8' android:background='#5fe1f2' android:layout_marginTop='10dp' android:layout_marginRight='10dp' android:layout_marginLeft='10dp' android:layout_marginBottom='10dp' android:layout_weight='1' android:layout_width='0dp' android:layout_height='match_parent' /> </LinearLayout> </LinearLayout></LinearLayout>

getResult2.java 文件

還有一個getResult2 類,用來獲得運算之后的結果

package com.example.calculator;public class getResult2 { private String a1; //第一位操作數 private double doubleA1; //實際參與運算 private String a2; //第二位操作數 private double doubleA2; //實際參與運算 private int operation; //運算符 double result; //結果 //構造函數 getResult2() { a1 = ' '; a2 = ' '; operation = 0; } void setA1(String A1) { a1 = A1; } void setA2(String A2) { a2 = A2; } void setDoubleA1(double x) { doubleA1 = x; } void setDoubleA2(double y) { doubleA2 = y; } //設置操作數,同時將字符串轉換成數字,如果帶小數點,轉換成浮點數,否則轉換成整數 public void setNumber(String x) { if (a1.equals(' ')) { a1 = x; if (a1.contains('.')) { doubleA1 = Double.parseDouble(a1); } else { doubleA1 = Integer.parseInt(a1); } } else { a2 = x; if (a2.contains('.')) { doubleA2 = Double.parseDouble(a2); } else { doubleA2 = Integer.parseInt(a2); } } } public void setOperation(int i) { operation = i; } //進行運算,得到結果,同時將結果賦值給第一位操作數 public double getResult() { if (operation == 1) { if (!a1.equals(' ') && a2.equals(' ')) { return 0; } else { result = doubleA1 + doubleA2; a1 = String.valueOf(result); doubleA1 = result; a2 = ' '; } } else if (operation == 2) { if (!a1.equals('') && a2.equals('')) { return 0; } else { result = doubleA1 - doubleA2; a1 = String.valueOf(result); doubleA1 = result; a2 = ' '; } } else if (operation == 3) { if (!a1.equals(' ') && a2.equals(' ')) { return 0; } else { result = doubleA1 * doubleA2; a1 = String.valueOf(result); doubleA1 = result; a2 = ' '; } } else if (operation == 4) { if (!a1.equals(' ') && a2.equals(' ')) { return 0; } else { result = doubleA1 / doubleA2; a1 = String.valueOf(result); doubleA1 = result; a2 = ' '; } } return result; }}

更多計算器功能實現,請點擊專題: 計算器功能匯總 進行學習

關于Android計算器功能的實現,查看專題:Android計算器 進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲欧美久久精品 | 91福利国产在线观看香蕉 | 日本免费人成黄页在线观看视频 | 国产精品爱久久久 | 麻豆精品视频在线原创 | 欧美激情视频网址 | 青青操精品 | jpnesxxx日本 | 亚洲一区二区免费在线观看 | 日本成人片网站 | 美国一级毛片免费视频观看 | 咪咪久久 | 一级特黄特色aa大片 | 国产成人精品在线观看 | 免费一级毛片在线播放 | 一级黄色录像免费 | 国产91欧美 | 欧美日韩在线一本卡 | 久久激情综合网 | 欧美岛国 | 色在线播放 | 婷婷射图 | 一级片黄 | 成年人免费网址 | 大人和孩做爰h视频在线观看 | 在线观看亚洲欧美 | 美女国内精品自产拍在线播放 | 国产精品网站在线观看 | 国产精品 视频一区 二区三区 | 欧美激情一级毛片在 | 9久9久女女免费精品视频在线观看 | 亚洲综合综合在线 | a级情欲视频免费观看 | 免费jizz在在线播放国产 | 伊人久久国产免费观看视频 | 成年日韩免费大片黄在线观看 | 久草在线免费福利资源站 | 三级黄色毛片 | 成人免费淫片95视频观看网站 | 欧美一级日韩一级 | 免费一级毛片免费播放 |