电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:2日期: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); //設(shè)置按鈕的點擊事件 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(''); //清空文本內(nèi)容 break; case R.id.btn_equal: getResult(); //獲取結(jié)果 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強制轉(zhuǎn)化為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; //強制轉(zhuǎn)換 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),請點擊專題: 計算器功能匯總 進行學(xué)習(xí)

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

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

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 | 百度网站优化,关键词排名,SEO优化-搜索引擎营销推广 | 政府园区专业委托招商平台_助力企业选址项目快速落地_东方龙商务集团 | 干式磁选机_湿式磁选机_粉体除铁器-潍坊国铭矿山设备有限公司 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 隧道烘箱_隧道烘箱生产厂家-上海冠顶专业生产烘道设备 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 三板富 | 专注于新三板的第一垂直服务平台 | 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 泰安办公家具-泰安派格办公用品有限公司| 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 电缆接头_防水接头_电缆防水接头_防水电缆接头_上海闵彬 | 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | 破碎机_上海破碎机_破碎机设备_破碎机厂家-上海山卓重工机械有限公司 | 合肥弱电工程_安徽安防工程_智能化工程公司-合肥雷润 | 中药二氧化硫测定仪,食品二氧化硫测定仪|俊腾百科 | 上海深蓝_缠绕机_缠膜机-上海深蓝机械装备有限公司 | 双杰天平-国产双杰电子天平-美国双杰-常熟双杰仪器 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 电解抛光加工_不锈钢电解抛光_常州安谱金属制品有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | ★济南领跃标识制作公司★济南标识制作,标牌制作,山东标识制作,济南标牌厂 | 污水处理设备,一体化泵站,一体化净水设备-「梦之洁环保设备厂家」 | 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | ★济南领跃标识制作公司★济南标识制作,标牌制作,山东标识制作,济南标牌厂 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 对夹式止回阀_对夹式蝶形止回阀_对夹式软密封止回阀_超薄型止回阀_不锈钢底阀-温州上炬阀门科技有限公司 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 |