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

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

Android table布局開發(fā)實現簡單計算器

瀏覽:56日期:2022-09-24 11:24:19

本文實例為大家分享了Android table布局開發(fā)實現簡單計算器的具體代碼,供大家參考,具體內容如下

結果如圖:

Android table布局開發(fā)實現簡單計算器

XML文件如下:

<FrameLayout xmlns:android='http://schemas.android.com/apk/res/android'xmlns:tools='http://schemas.android.com/tools'android:id='@+id/container'android:layout_width='match_parent'android:layout_height='match_parent'tools:context='com.example.wxhcalculator.MainActivity'tools:ignore='MergeRootFrame' ><TableLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:stretchColumns='1'android:textSize='42sp' ><TableRow><EditTextandroid:id='@+id/result'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_span='4'android:background='@android:drawable/editbox_background'android:cursorVisible='false'android:editable='false'android:gravity='right|center_vertical'android:lines='1'android:textSize='60sp' /></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num7'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='7'android:textSize='42sp' /><Buttonandroid:id='@+id/num8'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='8'android:textSize='42sp' /><Buttonandroid:id='@+id/num9'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='9'android:textSize='42sp' /><Buttonandroid:id='@+id/divide'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='/'android:textSize='42sp' /></LinearLayout></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num4'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='4'android:textSize='42sp' /><Buttonandroid:id='@+id/num5'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='5'android:textSize='42sp' /><Buttonandroid:id='@+id/num6'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='6'android:textSize='42sp' /><Buttonandroid:id='@+id/multiply'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='*'android:textSize='42sp' /></LinearLayout></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num1'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='1'android:textSize='42sp' /><Buttonandroid:id='@+id/num2'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='2'android:textSize='42sp' /><Buttonandroid:id='@+id/num3'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='3'android:textSize='42sp' /><Buttonandroid:id='@+id/subtract'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='-'android:textSize='42sp' /></LinearLayout></TableRow><TableRow><LinearLayoutandroid:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:orientation='horizontal'android:textSize='42sp' ><Buttonandroid:id='@+id/num0'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='0'android:textSize='42sp' /><Buttonandroid:id='@+id/point'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='.'android:textSize='42sp' /><Buttonandroid:id='@+id/add'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='+'android:textSize='42sp' /><Buttonandroid:id='@+id/equal'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_weight='1'android:text='='android:textSize='42sp' /></LinearLayout></TableRow><TableRow><Buttonandroid:id='@+id/clear'android:layout_width='fill_parent'android:layout_height='wrap_content'android:layout_span='4'android:gravity='center_vertical|center_horizontal'android:text='clear'android:textSize='30sp' /></TableRow></TableLayout></FrameLayout>

mainActivity主函數如下:

package com.example.wxhcalculator;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends ActionBarActivity {private Button[] btnNum = new Button[11];// 數值按鈕private Button[] btnCommand = new Button[5];// 符號按鈕private EditText editText = null;// 顯示區(qū)域private Button btnClear = null; // clear按鈕private String lastCommand; // 用于保存運算符private boolean clearFlag; // 用于判斷是否清空顯示區(qū)域的值,true需要,false不需要private boolean firstFlag; // 用于判斷是否是首次輸入,true首次,false不是首次private double result; // 計算結果public MainActivity() {// 初始化各項值result = 0; // x的值firstFlag = true; // 是首次運算clearFlag = false; // 不需要清空lastCommand = '='; // 運算符}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 獲取運算符btnCommand[0] = (Button) findViewById(R.id.add);btnCommand[1] = (Button) findViewById(R.id.subtract);btnCommand[2] = (Button) findViewById(R.id.multiply);btnCommand[3] = (Button) findViewById(R.id.divide);btnCommand[4] = (Button) findViewById(R.id.equal);// 獲取數字btnNum[0] = (Button) findViewById(R.id.num0);btnNum[1] = (Button) findViewById(R.id.num1);btnNum[2] = (Button) findViewById(R.id.num2);btnNum[3] = (Button) findViewById(R.id.num3);btnNum[4] = (Button) findViewById(R.id.num4);btnNum[5] = (Button) findViewById(R.id.num5);btnNum[6] = (Button) findViewById(R.id.num6);btnNum[7] = (Button) findViewById(R.id.num7);btnNum[8] = (Button) findViewById(R.id.num8);btnNum[9] = (Button) findViewById(R.id.num9);btnNum[10] = (Button) findViewById(R.id.point);// 初始化顯示結果區(qū)域editText = (EditText) findViewById(R.id.result);editText.setText('0.0');// 實例化監(jiān)聽器對象NumberAction na = new NumberAction();CommandAction ca = new CommandAction();for (Button bc : btnCommand) {bc.setOnClickListener(ca);}for (Button bc : btnNum) {bc.setOnClickListener(na);}// clear按鈕的動作btnClear = (Button) findViewById(R.id.clear);btnClear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {editText.setText('0.0');// 初始化各項值result = 0; // x的值firstFlag = true; // 是首次運算clearFlag = false; // 不需要清空lastCommand = '='; // 運算符}});}// 數字按鈕監(jiān)聽器private class NumberAction implements OnClickListener {@Overridepublic void onClick(View view) {Button btn = (Button) view;String input = btn.getText().toString();if (firstFlag) { // 首次輸入// 一上就'.',就什么也不做if (input.equals('.')) {return;}// 如果是'0.0'的話,就清空if (editText.getText().toString().equals('0.0')) {editText.setText('');}firstFlag = false;// 改變是否首次輸入的標記值} else {String editTextStr = editText.getText().toString();// 判斷顯示區(qū)域的值里面是否已經有'.',如果有,輸入的又是'.',就什么都不做if (editTextStr.indexOf('.') != -1 && input.equals('.')) {return;}// 判斷顯示區(qū)域的值里面只有'-',輸入的又是'.',就什么都不做if (editTextStr.equals('-') && input.equals('.')) {return;}// 判斷顯示區(qū)域的值如果是'0',輸入的不是'.',就什么也不做if (editTextStr.equals('0') && !input.equals('.')) {return;}}// 如果我點擊了運算符以后,再輸入數字的話,就要清空顯示區(qū)域的值if (clearFlag) {editText.setText('');clearFlag = false;// 還原初始值,不需要清空}editText.setText(editText.getText().toString() + input);// 設置顯示區(qū)域的值}}// 符號按鈕監(jiān)聽器private class CommandAction implements OnClickListener {@Overridepublic void onClick(View view) {Button btn = (Button) view;String inputCommand = (String) btn.getText();if (firstFlag) {// 首次輸入'-'的情況if (inputCommand.equals('-')) {editText.setText('-');// 顯示區(qū)域的內容設置為'-'firstFlag = false;// 改變首次輸入的標記}} else {if (!clearFlag) {// 如果flag=false不需要清空顯示區(qū)的值,就調用方法計算calculate(Double.parseDouble(editText.getText().toString()));// 保存顯示區(qū)域的值,并計算}// 保存你點擊的運算符lastCommand = inputCommand;clearFlag = true;// 因為我這里已經輸入過運算符,}}}// 計算用的方法private void calculate(double x) {if (lastCommand.equals('+')) {result += x;} else if (lastCommand.equals('-')) {result -= x;} else if (lastCommand.equals('*')) {result *= x;} else if (lastCommand.equals('/')) {result /= x;} else if (lastCommand.equals('=')) {result = x;}editText.setText('' + result);}}

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

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

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

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
美女网站一区| 亚洲天堂免费| 黑人精品一区| 亚洲精品一二三区区别| 免费人成在线不卡| 国产精品igao视频网网址不卡日韩 | 日韩在线观看一区二区| 亚久久调教视频| 久久影院资源站| 久久裸体视频| 亚洲永久精品唐人导航网址| 国产精品午夜一区二区三区| 日韩在线免费| 免费观看在线色综合| 国产精品大片免费观看| 麻豆精品蜜桃| 日本不卡一二三区黄网| 国内精品麻豆美女在线播放视频| 久久视频国产| 日本va欧美va精品| 麻豆精品蜜桃| 欧美亚洲国产日韩| 国产在线欧美| 久久99影视| 午夜久久影院| 久久男人av| 亚洲欧美视频| 国产一区二区三区网| 蜜桃av一区| 麻豆理论在线观看| 日本不卡视频在线观看| 日韩不卡在线| 青青草国产成人99久久| 99精品美女| 国产精品久久久久77777丨| 激情欧美日韩一区| 国产精品v亚洲精品v日韩精品| 宅男在线一区| caoporn视频在线| 日本不卡视频在线观看| 久久久久国产精品一区三寸 | 免费久久99精品国产自在现线| 欧美激情在线精品一区二区三区| 婷婷综合网站| 久久久久97| 亚洲理论在线| 91久久国产| 97精品中文字幕| 国产欧美一区二区精品久久久| 亚洲作爱视频| 精品成人免费一区二区在线播放| 国产精品欧美大片| 日av在线不卡| 国产99久久| 高清av一区| 国产精品亚洲人成在99www | 91精品美女| 日韩网站在线| 国产福利片在线观看| 国产精品永久| 日本亚洲欧洲无免费码在线| 欧美福利一区| 另类中文字幕国产精品| 精品视频黄色| 国产精品免费99久久久| 四虎精品永久免费| 视频一区视频二区中文| 91高清一区| 久久视频国产| 999久久久91| 欧美成人基地| 久久毛片亚洲| 91综合网人人| 美女视频黄久久| 欧美一区精品| 婷婷五月色综合香五月| 六月婷婷一区| 夜久久久久久| 欧美日韩三区| 蜜桃视频欧美| 欧美三区四区| 日韩在线观看一区| 在线天堂资源www在线污| 久久精品国产亚洲一区二区三区| 国产欧美日韩精品一区二区免费| 日韩一区免费| 日韩欧美中文字幕电影| 日韩中文字幕一区二区三区| 欧美.日韩.国产.一区.二区| 日韩欧美在线中字| 欧美日韩国产观看视频| a天堂资源在线| 色天使综合视频| 一区二区三区四区在线看| 亚洲成人va| 国产99久久| 日韩一区二区免费看| 欧美午夜不卡影院在线观看完整版免费| 国产在线日韩| 久久先锋影音| 亚洲tv在线| 青青国产精品| 精品一区二区三区免费看| 美女精品久久| 国产91欧美| 欧美香蕉视频| 激情婷婷欧美| 蜜桃一区二区三区在线观看| 视频国产精品| 国产精品一卡| 日韩欧美综合| 性一交一乱一区二区洋洋av| 亚洲青青久久| 欧美亚洲一区二区三区| 欧美激情一区| 婷婷综合六月| 日韩视频二区| 亚洲综合精品| 欧美一区不卡| 国产一区二区三区久久 | 亚洲制服欧美另类| 三级欧美在线一区| 亚洲乱码视频| 青青草精品视频| 国产亚洲第一伦理第一区| 国产日韩一区二区三区在线| 国产精品美女在线观看直播| 精品少妇一区| 午夜日韩在线| 亚洲综合激情在线| 日韩av一二三| 国产欧美一区二区三区国产幕精品| 国产亚洲一卡2卡3卡4卡新区| 国产精品最新| 国产精品国产一区| 欧美二三四区| 黑丝一区二区三区| 久久亚洲不卡| 欧美欧美黄在线二区| 国产精品第十页| 日韩久久精品网| 午夜国产精品视频| 亚洲精品大全| 久久wwww| 亚洲一级少妇| 国产视频一区在线观看一区免费| 伊人久久成人| 中文一区一区三区免费在线观 | 国产一区国产二区国产三区| 国产精品毛片久久| 亚洲a一区二区三区| 亚洲香蕉网站| 午夜在线精品| 亚洲精品极品| 久久久91麻豆精品国产一区| 天堂中文av在线资源库| 精品欧美激情在线观看| 首页亚洲欧美制服丝腿| 国产调教精品| 高清在线一区| 国产精品色网| 欧美日韩1区| 日韩三区免费| 国产精品一区二区三区四区在线观看| 国产一区2区| 男人的天堂久久精品| 国产精品夜夜夜| 欧美日韩激情| 成人在线观看免费视频| 亚洲日本三级| 激情久久五月| 精品国产亚洲一区二区三区在线| 鲁大师影院一区二区三区| 中文av在线全新| 国产日韩欧美中文在线| 亚洲男女av一区二区| 欧美激情一区| 亚州av日韩av| 在线一区视频| 涩涩av在线| 国产精品视频一区二区三区综合 | 蜜桃久久久久久| 久久裸体视频| 九九久久国产| 亚洲a成人v| 不卡中文字幕| 国产一区国产二区国产三区| 日韩欧美中文字幕一区二区三区| 欧美成人日韩| 欧洲一区二区三区精品| 国产精品一区2区3区| 亚洲资源网站| 亚洲免费影视| 1000部精品久久久久久久久| 欧美国产不卡| 日本伊人午夜精品| 亚洲中字黄色| 黑丝一区二区| 精品日韩毛片| 精品国模一区二区三区|