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

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

Android開發學習實現簡單計算器

瀏覽:264日期:2022-09-24 18:28:12

這里是用線性布局實現的計算器,為防止以后再回顧知識代碼找不到,特將代碼貼在這里:

xml文件的布局代碼:

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.MainActivity'> <EditText android: android:layout_width='match_parent' android:layout_height='100dp' android:paddingBottom='5dp' android:paddingRight='5dp' android:textSize='50sp' android:gravity='right' android:textColor='#00ff00'/> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_marginTop='30dp' android:orientation='horizontal' android:gravity='center_horizontal'> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='C' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' android:textColor='#ff0000'/> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='Ba' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='+' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='-' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:orientation='horizontal' android:gravity='center_horizontal' > <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='7' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='8' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='9' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='×' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_marginTop='10dp' android:orientation='horizontal' android:gravity='center_horizontal' > <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='4' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='5' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='6' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android: android:layout_width='80dp' android:layout_height='80dp' android:text='÷' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:orientation='horizontal' android:layout_marginTop='10dp' android:gravity='center_horizontal'> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='vertical' > <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='horizontal' > <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='1' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='2' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='3' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='horizontal' android:layout_marginTop='10dp'> <Button android:layout_width='170dp' android:layout_height='80dp' android: android:text='0' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' /> <Button android:layout_width='80dp' android:layout_height='80dp' android: android:text='.' android:textSize='30sp' android:layout_marginLeft='10dp' android:paddingRight='15sp' android:paddingBottom='15sp' /> </LinearLayout> </LinearLayout> <Button android: android:layout_width='80dp' android:layout_height='170dp' android:layout_marginLeft='10dp' android:text='=' android:textSize='30sp' android:paddingRight='15sp' android:paddingBottom='15sp' android:gravity='center' android:background='@color/colorAccent'/> </LinearLayout></LinearLayout>

Activity.java文件實現功能的代碼如下:

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class Main2Activity extends AppCompatActivity implements View.OnClickListener{ //創建Button對象 也就是activity_main.xml里所設置的ID Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt; Button btn_mul,btn_div,btn_add,btn_sub; Button btn_clr,btn_del,btn_eq; EditText et_input; boolean clr_flag; //判斷et編輯文本框中是否清空 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //實例化對象 setContentView(R.layout.activity_main2); btn_0= (Button) findViewById(R.id.btn_0); btn_1= (Button) findViewById(R.id.btn_1); btn_2= (Button) findViewById(R.id.btn_2); btn_3= (Button) findViewById(R.id.btn_3); btn_4= (Button) findViewById(R.id.btn_4); btn_5= (Button) findViewById(R.id.btn_5); btn_6= (Button) findViewById(R.id.btn_6); btn_7= (Button) findViewById(R.id.btn_7); btn_8= (Button) findViewById(R.id.btn_8); btn_9= (Button) findViewById(R.id.btn_9); btn_pt= (Button) findViewById(R.id.btn_pt); btn_add= (Button) findViewById(R.id.btn_add); btn_sub= (Button) findViewById(R.id.btn_sub); btn_mul= (Button) findViewById(R.id.btn_mul); btn_div= (Button) findViewById(R.id.btn_div); btn_clr= (Button) findViewById(R.id.btn_clr); btn_del= (Button) findViewById(R.id.btn_del); btn_eq= (Button) findViewById(R.id.btn_eq); et_input= (EditText) findViewById(R.id.et_input); //給按鈕設置的點擊事件 btn_0.setOnClickListener(this); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_pt.setOnClickListener(this); btn_add.setOnClickListener(this); btn_sub.setOnClickListener(this); btn_mul.setOnClickListener(this); btn_div.setOnClickListener(this); btn_clr.setOnClickListener(this); btn_del.setOnClickListener(this); btn_eq.setOnClickListener(this); } @Override public void onClick(View v) { String str=et_input.getText().toString(); switch (v.getId()){ case R.id.btn_0: case R.id.btn_1: case R.id.btn_2: case R.id.btn_3: case R.id.btn_4: case R.id.btn_5: case R.id.btn_6: case R.id.btn_7: case R.id.btn_8: case R.id.btn_9: case R.id.btn_pt: if(clr_flag){ clr_flag=false; str=''; et_input.setText(''); } et_input.setText(str+((Button)v).getText()); break; case R.id.btn_add: case R.id.btn_sub: case R.id.btn_mul: case R.id.btn_div: if(clr_flag){ clr_flag=false; str=''; et_input.setText(''); } if(str.contains('+')||str.contains('-')||str.contains('×')||str.contains('÷')) { str=str.substring(0,str.indexOf(' ')); } et_input.setText(str+' '+((Button)v).getText()+' '); break; case R.id.btn_clr: if(clr_flag) clr_flag=false; str=''; et_input.setText(''); break; case R.id.btn_del: //判斷是否為空,然后在進行刪除 if(clr_flag){ clr_flag=false; str=''; et_input.setText(''); } else if(str!=null&&!str.equals('')){ et_input.setText(str.substring(0,str.length()-1)); } break; case R.id.btn_eq: //單獨運算最后結果 getResult();//調用下面的方法 break; } } private void getResult() { String exp=et_input.getText().toString(); if(exp==null||exp.equals('')) return ; //因為沒有運算符所以不用運算 if(!exp.contains(' ')){ return ; } if(clr_flag){ clr_flag=false; return; } clr_flag=true; //截取運算符前面的字符串 String s1=exp.substring(0,exp.indexOf(' ')); //截取的運算符 String op=exp.substring(exp.indexOf(' ')+1,exp.indexOf(' ')+2); //截取運算符后面的字符串 String s2=exp.substring(exp.indexOf(' ')+3); double cnt=0; if(!s1.equals('')&&!s2.equals('')){ double d1=Double.parseDouble(s1); double d2=Double.parseDouble(s2); if(op.equals('+')){ cnt=d1+d2; } if(op.equals('-')){ cnt=d1-d2; } if(op.equals('×')){ cnt=d1*d2; } if(op.equals('÷')){ if(d2==0) cnt=0; else cnt=d1/d2; } if(!s1.contains('.')&&!s2.contains('.')&&!op.equals('÷')) { int res = (int) cnt; et_input.setText(res+''); }else { et_input.setText(cnt+'');} } //如果s1是空 s2不是空 就執行下一步 else if(!s1.equals('')&&s2.equals('')){ double d1=Double.parseDouble(s1); if(op.equals('+')){ cnt=d1; } if(op.equals('-')){ cnt=d1; } if(op.equals('×')){ cnt=0; } if(op.equals('÷')){ cnt=0; } if(!s1.contains('.')) { int res = (int) cnt; et_input.setText(res+''); }else { et_input.setText(cnt+'');} } //如果s1是空 s2不是空 就執行下一步 else if(s1.equals('')&&!s2.equals('')){ double d2=Double.parseDouble(s2); if(op.equals('+')){ cnt=d2; } if(op.equals('-')){ cnt=0-d2; } if(op.equals('×')){ cnt=0; } if(op.equals('÷')){ cnt=0; } if(!s2.contains('.')) { int res = (int) cnt; et_input.setText(res+''); }else { et_input.setText(cnt+'');} } else { et_input.setText(''); } }}

結果圖如下所示:

Android開發學習實現簡單計算器

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

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

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

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精品九九| 午夜av一区| 蜜臀精品一区二区三区在线观看| 亚洲激情av| 高清一区二区| 精品中文字幕一区二区三区| 欧美在线观看天堂一区二区三区| 婷婷综合激情| 99国产精品视频免费观看一公开| 欧美亚洲国产精品久久| 韩国三级一区| 日韩精品水蜜桃| 欧美日韩精品一区二区视频| 日韩中文视频| 尹人成人综合网| 国产亚洲欧洲| 亚洲香蕉视频| 91精品国产经典在线观看| 久久国产人妖系列| 麻豆一区二区三区| 精品国产一区二| 精品国产第一福利网站| 久久精品国产网站| 日韩欧美二区| 在线精品小视频| 免费日韩一区二区| 日精品一区二区三区| 国产精品免费大片| 日本久久精品| 伊人精品一区| 天堂成人国产精品一区| 欧美精品三级在线| 福利一区在线| 亚洲精品网址| 四虎精品一区二区免费| 美女尤物国产一区| 99国产精品免费视频观看| 亚洲成人日韩| 久久精品72免费观看| 国产不卡人人| 亚洲一区二区三区免费在线观看| 日本欧美大码aⅴ在线播放| 麻豆91精品视频| 免费不卡中文字幕在线| 日本不卡在线视频| а√天堂8资源中文在线| 亚洲综合日韩| 美女精品久久| 宅男噜噜噜66国产日韩在线观看| 日本欧美久久久久免费播放网| 免费不卡在线视频| 日韩国产欧美在线播放| 日韩一区二区三区精品视频第3页| 国产免费播放一区二区| 成人在线视频区| 久久国产精品成人免费观看的软件| 欧美va亚洲va日韩∨a综合色| 亚洲一区欧美二区| 日本欧美一区二区在线观看| 国产成人黄色| 吉吉日韩欧美| 蜜臀精品久久久久久蜜臀| 日韩毛片网站| 日本一二区不卡| 国产伊人精品| 视频国产精品| 精品国产日韩欧美精品国产欧美日韩一区二区三区 | 久久婷婷亚洲| 日韩精品一卡二卡三卡四卡无卡| 国产亚洲精品精品国产亚洲综合 | 欧美在线看片| 日本蜜桃在线观看视频| 尹人成人综合网| 久久黄色影视| 国产99在线| 国产精品社区| 久久精品毛片| 亚洲欧美日韩视频二区| 国产精品多人| 在线一区电影| 精品国产a一区二区三区v免费| 亚洲综合电影一区二区三区| 欧美交a欧美精品喷水| 欧美.日韩.国产.一区.二区| 欧美中文高清| 国产专区一区| 亚洲精品九九| 日韩不卡免费高清视频| 亚洲+小说+欧美+激情+另类| 久久久久九九精品影院| 欧美日韩国产传媒| 国产精品一在线观看| 亚洲婷婷免费| 国产欧美91| 亚洲一区区二区| 国产 日韩 欧美一区| 国产精品综合色区在线观看| 99热精品在线观看| 91视频一区| 热久久久久久| 夜夜嗨av一区二区三区网站四季av| 久久精品超碰| 狠狠色狠狠色综合日日tαg| 精品精品国产三级a∨在线| 国产免费成人| 92国产精品| 国产精品资源| 黄色精品网站| 日韩欧美一区二区三区免费看| 日韩激情啪啪| 99riav1国产精品视频| 九九九精品视频| 少妇高潮一区二区三区99| 欧美精品羞羞答答| 激情视频网站在线播放色| 国产精品资源| 日本亚洲欧美天堂免费| 欧美日韩国产一区二区三区不卡 | 日韩国产91| 国产婷婷精品| 久久久久蜜桃| 色一区二区三区| 美女久久精品| 国产精品一区免费在线| 视频一区视频二区中文字幕| 欧美a级一区| 四虎国产精品免费观看| 免费精品一区| 国产精品一区二区av日韩在线| 亚洲丝袜啪啪| 久久国产66| 夜夜嗨网站十八久久 | 国精品产品一区| 欧美日本久久| 亚洲乱码视频| 中文无码日韩欧| 亚洲欧美日韩国产一区| av亚洲在线观看| 99国产精品一区二区| 久久精品青草| 999精品一区| 精品免费视频| 天堂中文在线播放| av高清不卡| 国产一区二区精品久| 日韩国产欧美在线播放| 亚洲精品福利| 亚欧洲精品视频在线观看| 热久久久久久久| 免费日韩视频| 日韩影院免费视频| 免费在线观看成人| 免费黄网站欧美| 中文字幕亚洲影视| 国产精品欧美三级在线观看| 国产美女撒尿一区二区| 国产精品美女午夜爽爽| 欧美成a人片免费观看久久五月天| 国产精品黄网站| 激情综合五月| 久久亚洲精精品中文字幕| 卡一卡二国产精品| 九九九精品视频| 国产高清不卡| av一区二区高清| 免费在线观看一区二区三区| 三级欧美韩日大片在线看| 亚洲日韩中文字幕一区| 日韩高清不卡在线| 国产精品久久久久9999高清| 精品三级av| 久久激情一区| 亚洲一区亚洲| 亚洲精品护士| 青青草91视频| 国产中文欧美日韩在线| 97人人精品| 日韩欧美一区二区三区在线视频| 在线日韩av| 丝瓜av网站精品一区二区| 日韩精品欧美精品| 久久精品三级| 91tv亚洲精品香蕉国产一区| 狠狠色综合网| 日本一区二区三区中文字幕| 欧美成人一二区| 日韩国产一区二区| 图片区亚洲欧美小说区| 国产精品视区| 国产精品大片| 国际精品欧美精品| 激情亚洲影院在线观看| 日本在线不卡视频| 久久精品欧洲| 欧美日韩精品一本二本三本| 日韩福利视频网| 成人在线视频区| 另类av一区二区| 国产精品资源| 999国产精品999久久久久久|