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

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

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

瀏覽:262日期: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久久,三上悠亚国产精品一区二区三区
免费人成黄页网站在线一区二区| 欧美精品国产| 97精品资源在线观看| 欧美日韩国产欧| 精品国产美女a久久9999| 国产欧美一区二区三区国产幕精品 | 蜜桃久久av一区| 午夜日韩福利| 亚洲黄页一区| 蜜臀av在线播放一区二区三区| 国产精品社区| 久久亚洲欧洲| 综合欧美精品| 亚洲aa在线| 丝袜美腿亚洲一区| 老牛国产精品一区的观看方式| 亚洲一区国产一区| 伊人国产精品| 日韩中文字幕| 国产日韩三级| 欧美激情亚洲| 精品久久亚洲| 欧美不卡高清一区二区三区| 四虎884aa成人精品最新| 亚洲成人精品| 一区二区自拍| 亚洲精品乱码久久久久久蜜桃麻豆| 亚洲欧美在线专区| 亚洲精品视频一二三区| 蜜桃视频免费观看一区| 日韩成人午夜精品| 国产精品99精品一区二区三区∴| 精品一区二区三区中文字幕在线| 国产66精品| 国产综合亚洲精品一区二| 首页亚洲欧美制服丝腿| 日韩va亚洲va欧美va久久| 久久女人天堂| 久久久国产亚洲精品| 欧美粗暴jizz性欧美20| 蜜臀久久99精品久久久久宅男| 亚洲精品福利| 欧美激情久久久久久久久久久| 成人在线丰满少妇av| 久久精品123| 99在线精品免费视频九九视 | 在线观看一区| 国产精品亚洲综合久久| 国产 日韩 欧美一区| 午夜日韩在线| 婷婷精品进入| 日本不卡视频在线观看| 国产精品亲子伦av一区二区三区 | 日韩在线观看| 亚洲成人精选| 国产黄大片在线观看| 日本精品不卡| 午夜国产一区二区| 日韩精品中文字幕吗一区二区| 欧美国产亚洲精品| 91精品推荐| 亚洲ww精品| 成人一区而且| 一区二区亚洲视频| 精品国产亚洲日本| 在线视频亚洲| 久久精品资源| 午夜亚洲一区| 国产成人精品一区二区三区视频 | 国产福利一区二区精品秒拍 | 日韩精品麻豆| 欧美一级二级三级视频| 婷婷综合六月| 国产调教精品| 噜噜噜躁狠狠躁狠狠精品视频| 老司机精品视频在线播放| 亚洲黄页一区| 一区二区精品伦理...| 亚洲精品系列| 丰满少妇一区| 日韩精品第一| 欧美日韩1区2区3区| 亚洲精选91| 日韩午夜在线| 亚洲一区中文| 亚洲美女91| 综合在线一区| 亚洲麻豆一区| 蜜臀av国产精品久久久久 | 加勒比视频一区| 国产一区二区中文| 国产精品久久久久久久久久白浆 | 久久中文视频| 久久婷婷一区| 免费在线观看一区| 亚洲精品影视| 国产精品yjizz视频网| 成人av动漫在线观看| 日韩国产激情| 国产一区二区三区久久| 捆绑调教美女网站视频一区| 午夜电影一区| 婷婷综合社区| 一区二区精彩视频| 午夜视频一区二区在线观看| 一区二区三区网站| 在线观看一区| 欧美aa国产视频| 成人影视亚洲图片在线| 精品久久国产一区| 精品国产黄a∨片高清在线| 亚洲免费福利| 国产精品蜜芽在线观看| 日本精品在线播放| 日韩av一区二区三区四区| 国产欧美日韩一级| 亚洲欧美综合| 亚洲精品美女91| 欧美国产中文高清| 日本欧美一区二区| 日本在线不卡视频| av亚洲一区二区三区| 国产精品极品在线观看| 男女男精品网站| 超碰在线99| 精品国产美女a久久9999| 亚洲一区欧美| 88xx成人免费观看视频库| 999国产精品永久免费视频app| 四虎国产精品免费观看| 999久久久精品国产| 国产精品二区不卡| 香蕉成人av| 日韩视频免费| 欧美成人基地| 中文在线资源| 精品不卡一区| 激情综合婷婷| 日韩av二区在线播放| 欧美伊人久久| 亚洲专区视频| 91伊人久久| 日韩精选在线| 亚洲资源网站| 亚洲日韩中文字幕一区| 亚洲精品乱码日韩| 亚洲精品麻豆| 国产美女精品视频免费播放软件| 青青草91久久久久久久久| 麻豆久久久久久| 日韩专区视频网站| 国产伦精品一区二区三区在线播放| 亚洲一区二区三区免费在线观看| 一区二区日韩免费看| 久久久水蜜桃av免费网站| 国产伊人精品| 红桃视频国产精品| 日韩精品免费视频一区二区三区| 深夜视频一区二区| 在线精品视频在线观看高清| 一区二区小说| 少妇精品久久久一区二区| 免费人成精品欧美精品| 国产亚洲一区| 久久激情五月激情| 亚洲黄色免费av| 久久gogo国模啪啪裸体| 免费亚洲婷婷| 天堂8中文在线最新版在线| 久草免费在线视频| 在线视频免费在线观看一区二区| 视频在线观看一区| 国产精品宾馆| 欧美成a人片免费观看久久五月天| 日韩在线视频精品| 亚洲午夜黄色| 欧美一区久久| 国际精品欧美精品| 香蕉精品999视频一区二区| 亚洲日本国产| 黑森林国产精品av| 91成人精品| 国产精品主播| 日韩成人免费| 亚洲一区欧美| 牛牛精品成人免费视频| 精品一区欧美| 日韩天堂在线| 日韩影院精彩在线| 亚洲最新av| 国产精品毛片aⅴ一区二区三区| 久久av免费| 高清av一区| 一区二区不卡| 国产日产一区| 日韩免费小视频| 国产欧美一区二区三区国产幕精品 | 国产成人精品一区二区三区在线| 日韩.com| 日韩精品亚洲专区|