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

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

Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)

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

這是一個(gè)運(yùn)用網(wǎng)格布局來(lái)做的簡(jiǎn)易計(jì)算器,可能沒(méi)有那么美觀,大家可以繼續(xù)完善

首先先看看成果吧

Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)

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

Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)

Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)

第一個(gè)是文件名字,第二個(gè)屬性可以自己選擇,我們這里前兩個(gè)文件選擇shape,第三個(gè)文件選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'/> //默認(rèn)顏色 <item android:drawable='@drawable/orange' android:state_pressed='true'/> //按下的改變的顏色</selector>

這個(gè)是當(dāng)你按下按鍵的時(shí)候按鍵會(huì)改變顏色

接下來(lái)就是布局文件了

activity_main.xml

我用的是表格布局,大家也可以用表格布局來(lái)寫,效果會(huì)好一些

<?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>

接下來(lái)就是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; //清空標(biāo)識(shí) @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è)置按鈕的點(diǎn)擊事件 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)); //刪除一個(gè)字符 } 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(' ')); // 獲取到運(yùn)算符前面的字符 String str_y = s.substring(s.indexOf(' ')+1,s.indexOf(' ')+2); //獲取到運(yùn)算符 String str2 = s.substring(s.indexOf(' ')+ 3); //獲取到運(yùn)算符后面的字符 double result = 0; if (!str1.equals ('') && !str2.equals ('')){ double num1 = Double.parseDouble(str1); //將str1、str2強(qiáng)制轉(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; //強(qiáng)制轉(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 (''); } }}

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

更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)

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

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 健康管理师报考条件,考试时间,报名入口—首页 | 粘度计维修,在线粘度计,二手博勒飞粘度计维修|收购-天津市祥睿科技有限公司 | LED投光灯-工矿灯-led路灯头-工业灯具 - 山东普瑞斯照明科技有限公司 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 超声波_清洗机_超声波清洗机专业生产厂家-深圳市好顺超声设备有限公司 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 中宏网-今日新闻-财经新闻| 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | 泰安办公家具-泰安派格办公用品有限公司| 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家 | 西门子代理商_西门子变频器总代理-翰粤百科 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 净化板-洁净板-净化板价格-净化板生产厂家-山东鸿星新材料科技股份有限公司 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | pH污水传感器电极,溶解氧电极传感器-上海科蓝仪表科技有限公司 | 市政路灯_厂家-淄博信达电力科技有限公司 | 塑料撕碎机_编织袋撕碎机_废纸撕碎机_生活垃圾撕碎机_废铁破碎机_河南鑫世昌机械制造有限公司 | 二手注塑机回收_旧注塑机回收_二手注塑机买卖 - 大鑫二手注塑机 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 耐火砖厂家,异形耐火砖-山东瑞耐耐火材料厂 | 防弹玻璃厂家_防爆炸玻璃_电磁屏蔽玻璃-四川大硅特玻科技有限公司 | WTB5光栅尺-JIE WILL磁栅尺-B60数显表-常州中崴机电科技有限公司 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 能耗监测系统-节能监测系统-能源管理系统-三水智能化 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 |