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

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

Android Studio實現(xiàn)簡易計算器(表格布局TableLayout)

瀏覽:37日期:2022-09-25 09:40:56

這是一個運用網(wǎng)格布局來做的簡易計算器,可能沒有那么美觀,大家可以繼續(xù)完善

首先先看看成果吧

Android Studio實現(xiàn)簡易計算器(表格布局TableLayout)

首先先建一個新的Project Calculator然后先編寫顏色背景文件創(chuàng)建一個gray.xml,哪里創(chuàng)建呢?如圖在drawable下右擊,選擇new?Drawable resource file

Android Studio實現(xiàn)簡易計算器(表格布局TableLayout)

Android Studio實現(xiàn)簡易計算器(表格布局TableLayout)

第一個是文件名字,第二個屬性可以自己選擇,我們這里前兩個文件選擇shape,第三個文件選selector,附上顏色背景代碼

gray.xml

<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android'> <corners android:radius='5dp'/> <solid android:color='#f9f9f9'/> <stroke android: android:color='#ffa600'/></shape>

orange.xml

<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android'> <corners android:radius='5dp'/> // 圓角 <solid android:color='#F7B684'/> //顏色</shape>

white.xml

<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android'> <corners android:radius='5dp'/> <solid android:color='#ffffff'/> <stroke android: android:color='#ffa600'/></shape>

change.xml

<?xml version='1.0' encoding='utf-8'?><selector xmlns:android='http://schemas.android.com/apk/res/android'> <item android:drawable='@drawable/gray'/> //默認顏色 <item android:drawable='@drawable/orange' android:state_pressed='true'/> //按下的改變的顏色</selector>

這個是當你按下按鍵的時候按鍵會改變顏色

接下來就是布局文件了

activity_main.xml

我用的是表格布局,大家也可以用表格布局來寫,效果會好一些

<?xml version='1.0' encoding='utf-8'?><TableLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' android:background='#D8ECF3'> <TextView android:gravity='bottom|right' android:textSize='70dp' android:singleLine='true' android:layout_margin='15dp' android:layout_width='match_parent' android:layout_height='120dp' android:background='@drawable/white' android: /> <TableRow android:layout_width='match_parent' android:layout_height='match_parent' android:layout_marginTop='10dp'> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/orange' android:gravity='center' android:text='C' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_span='2' android:background='@drawable/gray' android:gravity='center' android:text='Del' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_marginRight='10dp' android:background='@drawable/gray' android:gravity='center' android:layout_span='1' android:text='/' android:textSize='25sp' /> </TableRow> <TableRow android:layout_width='match_parent' android:layout_height='match_parent' android:layout_marginTop='10dp'> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='7' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='8' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='9' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_marginRight='10dp' android:background='@drawable/gray' android:gravity='center' android:text='*' android:textSize='25sp' /> </TableRow> <TableRow android:layout_width='match_parent' android:layout_height='match_parent' android:layout_marginTop='10dp'> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='4' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='5' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='6' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_marginRight='10dp' android:background='@drawable/gray' android:gravity='center' android:text='+' android:textSize='25sp' /> </TableRow> <TableRow android:layout_width='match_parent' android:layout_height='match_parent' android:layout_marginTop='10dp'> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='1' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='2' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:background='@drawable/white' android:gravity='center' android:text='3' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_marginRight='10dp' android:background='@drawable/gray' android:gravity='center' android:text='-' android:textSize='25sp' /> </TableRow> <TableRow android:layout_width='wrap_content' android:layout_height='match_parent' android:layout_marginTop='10dp'> <Button android: android:layout_marginLeft='10dp' android:layout_span='2' android:background='@drawable/white' android:gravity='center' android:text='0' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_span='1' android:background='@drawable/white' android:gravity='center' android:text='.' android:textSize='25sp' /> <Button android: android:layout_marginLeft='10dp' android:layout_marginRight='10dp' android:layout_span='1' android:background='@drawable/gray' android:gravity='center' android:text='=' android:textSize='25sp' /> </TableRow></TableLayout>

接下來就是MainActivity.java

package com.example.calculator;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn_clean,btn_del,btn_divide,btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9, btn_multiply,btn_add,btn_minus,btn_point,btn_equal; TextView textView; boolean clear_flag; //清空標識 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); btn_0 = findViewById(R.id.btn_0); //初始化 btn_1 = findViewById(R.id.btn_1); btn_2 = findViewById(R.id.btn_2); btn_3 = findViewById(R.id.btn_3); btn_4 = findViewById(R.id.btn_4); btn_5 = findViewById(R.id.btn_5); btn_6 = findViewById(R.id.btn_6); btn_7 = findViewById(R.id.btn_7); btn_8 = findViewById(R.id.btn_8); btn_9 = findViewById(R.id.btn_9); btn_multiply = findViewById(R.id.btn_multiply); btn_divide = findViewById(R.id.btn_divide); btn_add = findViewById(R.id.btn_add); btn_minus = findViewById(R.id.btn_minus); btn_point = findViewById(R.id.btn_point); btn_del =findViewById(R.id.btn_del); btn_equal = findViewById(R.id.btn_equal); btn_clean = findViewById(R.id.btn_clean); textView = findViewById(R.id.textView); 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_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_del.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_point.setOnClickListener(this); btn_add.setOnClickListener(this); btn_equal.setOnClickListener(this); btn_clean.setOnClickListener(this); } public void onClick(View v) { String str = textView.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_point: if(clear_flag){ clear_flag=false; str=''; textView.setText (''); } textView.setText(str+((Button)v).getText ()); break; case R.id.btn_add: case R.id.btn_minus: case R.id.btn_multiply: case R.id.btn_divide: if(clear_flag){ clear_flag=false; textView.setText(''); } textView.setText(str+' '+((Button)v).getText()+' '); break; case R.id.btn_del: if(clear_flag){ clear_flag=false; textView.setText (''); }else if (str != null && !str.equals ('')){ textView.setText(str.substring(0,str.length()-1)); //刪除一個字符 } break; case R.id.btn_clean: clear_flag=false; str = ''; textView.setText(''); //清空文本內容 break; case R.id.btn_equal: getResult(); //獲取結果 break; } } private void getResult() { //算法 String s = textView.getText().toString(); if(s == null || s.equals ('')){ return; } if (!s.contains ('')){ return; } if (clear_flag){ clear_flag=false; return; } clear_flag=true; String str1 = s.substring(0,s.indexOf(' ')); // 獲取到運算符前面的字符 String str_y = s.substring(s.indexOf(' ')+1,s.indexOf(' ')+2); //獲取到運算符 String str2 = s.substring(s.indexOf(' ')+ 3); //獲取到運算符后面的字符 double result = 0; if (!str1.equals ('') && !str2.equals ('')){ double num1 = Double.parseDouble(str1); //將str1、str2強制轉化為double類型 double num2 = Double.parseDouble(str2); if (str_y.equals ('+')){ result = num1 + num2; }else if (str_y.equals ('-')){ result = num1 - num2; }else if (str_y.equals ('÷')){ if (num2 == 0){ result = 0; }else { result = num1/num2; } }else if (str_y.equals ('*')){ result = num1*num2; } if (!str1.contains ('.') && !str2.contains ('.') && !s.equals ('÷')){ int k = (int) result; //強制轉換 textView.setText (k); }else{ textView.setText (result+''); } }else if (!str1.equals ('') && str2.equals ('')){ textView.setText (s); }else if (str1.equals ('') && !str2.equals ('')){ double num2 = Double.parseDouble(str2); if (s.equals ('+')){ result = 0 + num2; }else if (s.equals('-')){ result = 0 - num2; }else if (s.equals('×')){ result = 0; }else if (s.equals('÷')){ result = 0; } if (!str2.contains ('.')) { int r = (int) result; textView.setText (r + ''); } else { textView.setText (result + ''); } } else { textView.setText (''); } }}

這里的算法可能寫的沒有那么好,大家可以網(wǎng)上找找其他案例參照一下,繼續(xù)完善算法

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

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

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

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产极品一区| 日韩三级一区| 欧美另类中文字幕 | 一区二区三区国产盗摄| 国产综合婷婷| 久久五月天小说| 婷婷亚洲综合| 鲁大师成人一区二区三区| 中文精品在线| 日韩精品欧美成人高清一区二区| 一区二区三区午夜视频| 性色一区二区| 亚洲精品少妇| 国产精品对白| 国模精品一区| 亚洲综合在线电影| 国产在线日韩| 中文字幕免费一区二区| 国产精品一区三区在线观看| 精品国产亚洲一区二区三区大结局| 麻豆国产欧美日韩综合精品二区| 精品国产午夜| 免费黄色成人| 欧美日本精品| 日本美女一区| 一区二区三区网站| 里番精品3d一二三区| 色偷偷色偷偷色偷偷在线视频| 91成人精品视频| 日韩精品一区二区三区中文| 欧美精品三级在线| 日韩精品一区二区三区免费观影| 亚洲欧美日韩高清在线| 欧美亚洲免费| 亚洲福利专区| 欧美国产另类| 视频在线观看一区二区三区| 久久精品资源| 日韩欧美中文字幕一区二区三区| 国际精品欧美精品| 综合亚洲自拍| 999国产精品视频| 国产日韩欧美一区二区三区在线观看 | 欧美黄页在线免费观看| 欧美成人综合| 久久国产免费看| 秋霞国产精品| 成人国产精选| 日韩av电影一区| 午夜电影亚洲| 欧美影院三区| 日韩欧美中文| 日韩88av| 日本成人在线一区| 99视频精品免费观看| 午夜亚洲福利在线老司机| 蜜臀久久99精品久久久画质超高清| 中文精品在线| 日韩福利在线观看| 精品一区二区三区亚洲| 精品美女久久| 日韩在线短视频| 久久久久网站| 国产美女精品| 国产日韩在线观看视频| 狠狠色综合网| 欧美国产日本| 欧美一区久久| 日韩国产激情| 91久久在线| 日韩精品a在线观看91| 国产精品va| 亚洲激情中文在线| 日韩国产欧美三级| 国产v日韩v欧美v| 亚洲欧美视频| 精品三区视频| 91精品91| 麻豆精品久久久| 亚洲激情五月| 国产亚洲久久| 国产无遮挡裸体免费久久| 激情黄产视频在线免费观看| av亚洲在线观看| 久色成人在线| 日韩1区在线| 亚洲日本免费电影| 麻豆网站免费在线观看| 亚洲欧美日韩综合国产aⅴ| 久久狠狠久久| 欧美一级精品| 久久只有精品| 亚洲一级大片| 久久精品国产亚洲夜色av网站| 日韩精品五月天| 99riav国产精品| 久久中文字幕av一区二区不卡| 蜜桃久久久久| 亚洲深夜影院| 久久先锋影音| 岛国av在线网站| 欧美福利在线| 日日夜夜免费精品视频| 国产精品国产三级在线观看| 欧美日韩视频免费观看| 丝袜美腿高跟呻吟高潮一区| 里番精品3d一二三区| 视频在线观看91| 91亚洲人成网污www| 日韩av一区二区三区四区| 欧洲毛片在线视频免费观看| 久久精品国产99国产| 在线精品福利| 欧美日韩国产一区精品一区| 日韩精品三区四区| 三级欧美在线一区| 久久天堂精品| 国产不卡一区| 国产麻豆一区二区三区| 一区二区亚洲视频| 91久久视频| 一区福利视频| 欧美日韩国产欧| 欧美sss在线视频| 精品国产一区二区三区av片| 日韩**一区毛片| 色婷婷成人网| 亚洲精品国产精品粉嫩| 精品一区二区男人吃奶 | 日韩精品中文字幕吗一区二区| 国产激情精品一区二区三区| 婷婷五月色综合香五月| 模特精品在线| 日韩精品一区第一页| 香蕉成人久久| 玖玖玖国产精品| 亚洲一区二区网站| 蜜臀av一区二区在线免费观看| 每日更新成人在线视频| 在线精品一区| 日本一区福利在线| 日韩av一区二区三区| 亚洲神马久久| 天堂俺去俺来也www久久婷婷| 亚洲精品在线二区| 国产精品色在线网站| 成人台湾亚洲精品一区二区| 久久精品动漫| 免费在线观看成人| 日韩 欧美一区二区三区| 91成人精品观看| 成人精品高清在线视频| 亚洲成人三区| 日韩avvvv在线播放| 久久久久久一区二区| 香蕉精品久久| 亚洲精品一级| 精品国产亚洲一区二区在线观看| 今天的高清视频免费播放成人| 中文不卡在线| 91亚洲国产| 日韩福利视频一区| 日韩av一级| 国产日韩欧美一区在线| 欧美亚洲国产精品久久| 国产美女亚洲精品7777| 国产综合视频| 麻豆精品99| 蜜桃久久久久久| 精品久久久久久久| 午夜精品婷婷| 精品国产欧美| 亚洲电影在线一区二区三区| 国产精品成人自拍| 美女尤物久久精品| 水蜜桃精品av一区二区| 91伊人久久| 免播放器亚洲| 麻豆精品蜜桃| 久久精品国产99国产| 色综合视频一区二区三区日韩| 久久精品国产www456c0m| 国产精品www.| 日韩av成人高清| 久久亚洲影院| 欧美日韩视频| 久久久精品久久久久久96| 欧美日本一区| 亚洲综合中文| 午夜在线视频一区二区区别 | 色婷婷精品视频| 日韩欧美字幕| 福利一区视频| 成人在线视频免费| 日韩伦理在线一区| 午夜精品久久久久久久久久蜜桃| 久久精品一区二区国产| 欧美黄页在线免费观看| 97se亚洲| 国产精品扒开腿做爽爽爽软件|