久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(可以實(shí)現(xiàn)連續(xù)計(jì)算)

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

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

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

我是用了兩個(gè) EditText ,第一個(gè)顯示運(yùn)算步驟(字符串類(lèi)型),第二個(gè)專(zhuān)門(mén)用了保存要參與運(yùn)算的數(shù)字,并實(shí)時(shí)對(duì)這個(gè)數(shù)字進(jìn)行更新;

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

在實(shí)現(xiàn)的過(guò)程當(dāng)中,多處用到了類(lèi)型轉(zhuǎn)換,從字符串轉(zhuǎn)換成浮點(diǎn)數(shù),從浮點(diǎn)數(shù)轉(zhuǎn)換成字符串等,進(jìn)行類(lèi)型轉(zhuǎn)換是要特別小心,我就是因?yàn)轭?lèi)型轉(zhuǎn)換是寫(xiě)錯(cuò)了,查bug查了老半天

效果圖就是這樣滴:

Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(可以實(shí)現(xiàn)連續(xù)計(jì)算)

有幾個(gè)小bug帶修復(fù):

1.運(yùn)算沒(méi)有優(yōu)先級(jí),完全是按用戶(hù)輸入的步驟來(lái)進(jìn)行運(yùn)算2.連續(xù)按兩次運(yùn)算操作符會(huì)閃退,剛開(kāi)始是按操作符也會(huì)閃退3.其中的正負(fù)數(shù)轉(zhuǎn)換按鈕還沒(méi)實(shí)行

由于最近要期中考試了,所以這幾個(gè)小bug過(guò)一段時(shí)間再來(lái)修復(fù),到時(shí)再更新

下面是代碼:

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ù)點(diǎn) Button button_clear; //清空 //2個(gè)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); } }); //實(shí)例化按鈕 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); //設(shè)置點(diǎn)擊事件 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用來(lái)保存第一個(gè)EditText中的字符串 String str = edit_input.getText().toString(); //str2用來(lái)保存第二個(gè)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; // + - * / 對(duì)應(yīng)的值依次為 1 2 3 4,將值傳入setOperation中,就執(zhí)行相應(yīng)的運(yùn)算 case R.id.button_plus: result2.setNumber(editText2.getText().toString()); //設(shè)置操作數(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è)置操作數(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è)置操作數(shù) result2.getResult(); result2.setOperation(3); //設(shè)置操作符 edit_input.setText(str + ' ' + ((Button) v).getText() + ' '); //加上空格更美觀 str2 = ''; editText2.setText(str2); //清空textView break; case R.id.button_divide: result2.setNumber(editText2.getText().toString()); //設(shè)置操作數(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用來(lái)截取字符串的長(zhǎng)度 if (str.substring(str.length() - 1, str.length()) == ' ') { //如果最后一個(gè)字符是空格,則刪除最后兩個(gè)字符,且eidtText2中字符串不發(fā)生變化 edit_input.setText(str.substring(0, str.length() - 2)); } else { //如果最后一個(gè)字符是數(shù)字 edit_input.setText(str.substring(0, str.length() - 1)); //將EditText2中的字符取出,去掉最后一個(gè)字符之后再存入 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 文件:

這里我用的是線(xiàn)性布局,同樣也可以用網(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.baoyu77737.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.baoyu77737.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.baoyu77737.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 文件

還有一個(gè)getResult2 類(lèi),用來(lái)獲得運(yùn)算之后的結(jié)果

package com.example.calculator;public class getResult2 { private String a1; //第一位操作數(shù) private double doubleA1; //實(shí)際參與運(yùn)算 private String a2; //第二位操作數(shù) private double doubleA2; //實(shí)際參與運(yùn)算 private int operation; //運(yùn)算符 double result; //結(jié)果 //構(gòu)造函數(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í)將字符串轉(zhuǎn)換成數(shù)字,如果帶小數(shù)點(diǎn),轉(zhuǎn)換成浮點(diǎn)數(shù),否則轉(zhuǎn)換成整數(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; } //進(jìn)行運(yùn)算,得到結(jié)果,同時(shí)將結(jié)果賦值給第一位操作數(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; }}

更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專(zhuān)題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)

關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專(zhuān)題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 上思县| 天津市| 深泽县| 崇文区| 鹤山市| 陇川县| 岚皋县| 雷山县| 湘潭市| 衢州市| 台东县| 陕西省| 沂南县| 临安市| 湖南省| 屯留县| 漯河市| 格尔木市| 金坛市| 九寨沟县| 汤原县| 柘城县| 平邑县| 田东县| 蒙山县| 彭山县| 秀山| 额济纳旗| 金门县| 白山市| 崇阳县| 青冈县| 平罗县| 习水县| 邵阳县| 虞城县| 黔西| 东源县| 如皋市| 蒲江县| 赣榆县|