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

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

Android Studio實現簡單計算器APP

瀏覽:6日期:2022-09-25 10:04:05

一、簡介:用Android Studio 實現一個簡單的計算器APP,并在藍疊模擬器中運行。

該計算器只能實現兩位數字的四則運算。

二、代碼

activity_main.xml ---界面設計

<?xml version='1.0' encoding='utf-8'?><GridLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:layout_below='@+id/textView' android:layout_alignParentStart='true' android:rowCount='6' android:columnCount='4' > <!--設置網格為6行4列--> <!--顯示文本組件,占1行4列--> <TextView android: android:layout_width='350dp' android:layout_height='wrap_content' android:layout_columnSpan='4' android:layout_marginLeft='4px' android:gravity='left' android:textSize='50dp' /> <!--清除按鈕,占1行4列--> <Button android: android:layout_width='353dp' android:layout_height='wrap_content' android:layout_columnSpan='4' android:text='清除' android:textSize='26sp' /> <!--以下按鈕為數字按鈕和函數按鈕,每個占1行1列--> <Button android: android:text='1' android:textSize='26sp' /> <Button android: android:text='2' android:textSize='26sp' /> <Button android: android:text='3' android:textSize='26sp' /> <Button android: android:text='+' android:textSize='26sp' /> <Button android: android:text='4' android:textSize='26sp' /> <Button android: android:text='5' android:textSize='26sp' /> <Button android: android:text='6' android:textSize='26sp' /> <Button android: android:text='-' android:textSize='26sp' /> <Button android: android:text='7' android:textSize='26sp' /> <Button android: android:text='8' android:textSize='26sp' /> <Button android: android:text='9' android:textSize='26sp' /> <Button android: android:text='*' android:textSize='26sp' /> <Button android: android:text='.' android:textSize='26sp' /> <Button android: android:text='0' android:textSize='26sp' /> <Button android: android:text='=' android:textSize='26sp' /> <Button android: android:text='/' android:textSize='26sp' /> </GridLayout>

界面:

Android Studio實現簡單計算器APP

MainActivity.java---功能實現

public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9, btnClear,btnPlus,btnSubtract,btnMultiply,btnDivide,btnSum,btnPoint; TextView text; String str = ''; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn0 = (Button) findViewById(R.id.btn0); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); btn5 = (Button) findViewById(R.id.btn5); btn6 = (Button) findViewById(R.id.btn6); btn7 = (Button) findViewById(R.id.btn7); btn8 = (Button) findViewById(R.id.btn8); btn9 = (Button) findViewById(R.id.btn9); btnClear = (Button) findViewById(R.id.btnClear); btnPlus = (Button) findViewById(R.id.btnPlus); btnSubtract = (Button) findViewById(R.id.btnSubtract); btnMultiply = (Button) findViewById(R.id.btnMultiply); btnDivide = (Button) findViewById(R.id.btnDivide); btnPoint = (Button) findViewById(R.id.btnPoint); btnSum = (Button) findViewById(R.id.btnSum); text = (TextView) findViewById(R.id.text) ; btn0.setOnClickListener(this); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); btn9.setOnClickListener(this); btnClear.setOnClickListener(this); btnPlus.setOnClickListener(this); btnSubtract.setOnClickListener(this); btnMultiply.setOnClickListener(this); btnDivide.setOnClickListener(this); btnPoint.setOnClickListener(this); btnSum.setOnClickListener(new click()); //給所有按鈕注冊點擊事件 } @Override public void onClick(View v) { String input=text.getText().toString(); switch (v.getId()){ case R.id.btn0: case R.id.btn1: case R.id.btn2: case R.id.btn3: case R.id.btn4: case R.id.btn5: case R.id.btn6: case R.id.btn7: case R.id.btn8: case R.id.btn9: case R.id.btnPoint: text.setText(input+((Button)v).getText()); break; case R.id.btnPlus: case R.id.btnSubtract: case R.id.btnMultiply: case R.id.btnDivide: text.setText(input + ' ' + ((Button)v).getText() + ' '); //給運算符前后加空格,好判斷 break; case R.id.btnClear: text.setText(''); break; } } class click implements View.OnClickListener { public void onClick (View v) { getResult(); } } private void getResult () { String str1 = text.getText().toString(); if(str1 == null || str1.equals('')){ return; } if(!str1.contains(' ')){ return ; } double result = 0;// 第一個數的字符串 String s1 = str1.substring(0,str1.indexOf(' '));// 運算符 String op = str1.substring(str1.indexOf(' ')+1,str1.indexOf(' ')+2);// 第二個數的字符串 String s2 = str1.substring(str1.indexOf(' ')+3); double d1 = Double.parseDouble(s1);//將數字字符串轉為double類型 double d2 = Double.parseDouble(s2); if (op.equals('+')) { //加法運算 result = d1 + d2; } else if (op.equals('-')) { //減法運算 result = d1 - d2; } else if (op.equals('*')) { //乘法運算 result = d1 * d2; } else if (op.equals('/')) { //除法運算 if (d2 == 0) { //如果被除數是0 result = 0; //則結果是0 } else { result = d1 / d2; } } text.setText(str1 + ' = ' + result); //顯示計算結果 if (!s1.contains('.') && !s2.contains('.') && !op.equals('/')) {//如果兩個整數且不是出發運算 int r = (int) result; //則結果轉為整數 text.setText(str1 + ' = ' + r ); } } }

三、運行測試

Android Studio實現簡單計算器APP

Android Studio實現簡單計算器APP

Android Studio實現簡單計算器APP

Android Studio實現簡單計算器APP

測試結果:

1.可以計算簡單兩位數的四則運算,但是如果計算超過2位數的運算,則會出現異常使程序退出。

2.四則運算中,結果可以為負數,但是運算數若為負數,則會出現異常,原因是該程序公式為【數字1 + 運算符 +數字二】,若輸入負數,即多出一位運算符,則會拋出異常。

四、總結

總的來說,這個計算器確實十分簡單,功能也不完善,還有很多小bug,但是對于剛入門的菜鳥來說,也用了不少的時間。希望自己能更加努力地堅持學習下去!

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

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

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

標簽: Android
相關文章:
主站蜘蛛池模板: 中文字幕亚洲视频 | 日本一级二级三级久久 | 高清性色生活片97 | 一区二区三区中文国产亚洲 | 在线视频欧美日韩 | 亚洲欧美在线免费观看 | 国产精品亚欧美一区二区三区 | 韩日免费视频 | 久久国产精品亚洲va麻豆 | 久久久精品影院 | 成人18网址在线观看 | 青青操手机看 | 草免费视频 | 国产精品揄拍100视频 | 麻豆网站免费观看 | 成人毛片在线播放 | 免费的黄色的视频 | 久久亚洲人成网站 | 鲁大师成人一区二区三区 | 亚洲经典在线观看 | 免费大片在线观看www | 久久久国产一区二区三区 | 国产一区二区播放 | 亚洲一区二区影院 | 中文字幕视频在线播放 | www在线小视频免费 www在线观看免费视频 | 黄色无遮挡 | 在线观看91香蕉国产免费 | 狠狠综合久久 | 1000部国产成人免费视频 | 国产稀缺精品盗摄盗拍 | 进来综合网 | 婷婷激情五月综合 | 国产成人三级视频在线观看播放 | 日韩成人精品视频 | 日韩午夜伦y4480私人影院 | 国产日韩欧美综合在线 | 黄色小视频在线免费观看 | 中国一级特黄特爽刺激大片 | 免费h片在线观看 | 超级碰碰青草久热国产 |