日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

Android實現(xiàn)簡易計算器(可以實現(xiàn)連續(xù)計算)

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

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

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

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

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

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

效果圖就是這樣滴:

Android實現(xiàn)簡易計算器(可以實現(xiàn)連續(xù)計算)

有幾個小bug帶修復:

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

由于最近要期中考試了,所以這幾個小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; //小數(shù)點 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中,就執(zhí)行相應的運算 case R.id.button_plus: result2.setNumber(editText2.getText().toString()); //設置操作數(shù) 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()); //設置操作數(shù) 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()); //設置操作數(shù) 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()); //設置操作數(shù) 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中字符串不發(fā)生變化 edit_input.setText(str.substring(0, str.length() - 2)); } else { //如果最后一個字符是數(shù)字 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 文件:

這里我用的是線性布局,同樣也可以用網(wǎng)格布局

<?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.b3g6.com/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.b3g6.com/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.b3g6.com/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; //第一位操作數(shù) private double doubleA1; //實際參與運算 private String a2; //第二位操作數(shù) private double doubleA2; //實際參與運算 private int operation; //運算符 double result; //結果 //構造函數(shù) 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; } //設置操作數(shù),同時將字符串轉換成數(shù)字,如果帶小數(shù)點,轉換成浮點數(shù),否則轉換成整數(shù) 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; } //進行運算,得到結果,同時將結果賦值給第一位操作數(shù) 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; }}

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

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

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

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
精品视频网站| 日本麻豆一区二区三区视频| 中文在线а√天堂| 在线看片日韩| 国产精品原创| 日本成人在线不卡视频| 久久久水蜜桃av免费网站| 日韩国产一区二| 在线综合视频| 日韩电影免费在线观看| 国产精品入口久久| 亚洲精品少妇| 亚洲欧美日韩精品一区二区| 成人国产精品一区二区免费麻豆| 日本欧美一区二区| 亚洲专区一区| 99免费精品| 日韩国产一区| 国产精品久久观看| 蜜臀av国产精品久久久久| 久久国产亚洲精品| 欧美日韩一区二区综合 | 美女毛片一区二区三区四区| 精品国产中文字幕第一页| 国产日本亚洲| 国产日韩一区| 欧美一级二区| 欧美日本三区| 欧美亚洲免费| 国产精品欧美三级在线观看 | 三级欧美韩日大片在线看| 久久人人99| 久久亚洲成人| 偷拍欧美精品| 一区二区91| 先锋亚洲精品| 日韩黄色在线观看| 国产精品久久国产愉拍| 国产午夜一区| 久久精品国产亚洲aⅴ| 国产精品传媒麻豆hd| 精品一区视频| 成人va天堂| 日韩国产欧美一区二区| 亚洲网站视频| 亚洲精品自拍| 国产精品精品| 亚洲精品.com| 视频一区免费在线观看| 日韩av电影一区| 国产一区二区精品久| 午夜精品成人av| 亚洲精选久久| 日韩欧美二区| 69堂免费精品视频在线播放| 成人午夜国产| 久久香蕉网站| 国产一区二区三区精品在线观看| 国产精品网站在线看| 黄色网一区二区| 欧美在线资源| 国产欧美久久一区二区三区| 电影91久久久| 国产农村妇女精品一二区| 人人精品久久| 亚洲一级少妇| 亚洲精品日本| 亚洲性色av| 亚洲毛片视频| 97精品一区| 综合激情视频| 丰满少妇一区| 视频一区中文字幕| 欧美日韩夜夜| 欧美午夜精彩| 国产精品久久久久久久免费软件| 色老板在线视频一区二区| 日韩欧美激情| 国产精品av久久久久久麻豆网| 日本aⅴ免费视频一区二区三区| 91亚洲一区| 日韩国产在线不卡视频| 激情五月综合| 色偷偷偷在线视频播放| 青青草91久久久久久久久| 一区免费视频| 日韩精品免费一区二区三区| 国产精品一级| 视频一区二区三区在线| 日韩精品一区二区三区免费观影 | 亚洲精品人人| 一区二区视频欧美| 久久影院午夜精品| 国产成人精品999在线观看| 国产亚洲一区| 亚洲另类视频| 亚洲精品裸体| 狠狠色综合网| 精品91久久久久| 日本在线精品| 久久久久免费| 激情中国色综合| 亚洲专区视频| 你懂的国产精品| 亚洲久久在线| 久久亚洲不卡| 蜜桃视频一区二区| 蜜桃久久久久久久| 国产精品99免费看| 日本精品影院| 日韩大片在线播放| 国产麻豆精品久久| 国产欧美亚洲精品a| 日本视频在线一区| 国产欧美日韩精品高清二区综合区 | 久久国产电影| 女人天堂亚洲aⅴ在线观看| 久久久精品久久久久久96 | 手机精品视频在线观看| 久久亚洲图片| 日韩精品五月天| 91国内精品| 麻豆久久一区二区| 91欧美国产| 日韩在线短视频| 国产精品伦理久久久久久| 91欧美日韩| av一区二区高清| 亚洲欧洲美洲国产香蕉| 亚洲精品高潮| 91大神在线观看线路一区| 美女在线视频一区| 国产伦久视频在线观看| 久久在线电影| 亚洲精品在线二区| 国产日韩一区二区三区在线 | 黄色aa久久| 亚洲黄色免费av| 激情91久久| 亚洲精品美女| 色婷婷综合网| 模特精品在线| 婷婷精品在线观看| 国产传媒在线| 日本亚洲最大的色成网站www| 久久爱www成人| 99xxxx成人网| 欧美激情亚洲| 欧美日韩精品一本二本三本| 久久国内精品视频| 国产视频亚洲| 欧美sss在线视频| 青青草视频一区| 婷婷激情图片久久| 久久国产免费看| 国产农村妇女精品一二区| 久久精品五月| 中文字幕免费精品| 四虎国产精品免费观看| 三级欧美在线一区| 在线亚洲人成| 国内揄拍国内精品久久| 色婷婷成人网| re久久精品视频| 久久69成人| 国产日韩视频在线| 在线精品亚洲| 蜜桃成人av| 国产精品成人a在线观看| 欧美不卡高清| 久久久久久自在自线| 日韩高清欧美激情| 蜜桃视频免费观看一区| 99久久精品网| 免费污视频在线一区| 精品国产aⅴ| 国产精品成人国产| 日韩国产一二三区| 日韩欧美中文字幕在线视频| 国产高清久久| 国产精品女主播一区二区三区| 欧美手机在线| 999久久久亚洲| 国产综合精品| 999国产精品| 久久久久午夜电影| 欧美69视频| 日韩精品一二三| 91欧美极品| 欧美1区2区3| 成人亚洲精品| 999国产精品| 亚洲精品黄色| 福利在线一区| 欧美91精品| 亚洲精品韩国| а√天堂8资源在线| 久久天堂精品| 中文不卡在线| 久久福利在线|