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

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

Android table布局開發實現簡單計算器

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

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

結果如圖:

Android table布局開發實現簡單計算器

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;// 顯示區域private Button btnClear = null; // clear按鈕private String lastCommand; // 用于保存運算符private boolean clearFlag; // 用于判斷是否清空顯示區域的值,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);// 初始化顯示結果區域editText = (EditText) findViewById(R.id.result);editText.setText('0.0');// 實例化監聽器對象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 = '='; // 運算符}});}// 數字按鈕監聽器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();// 判斷顯示區域的值里面是否已經有'.',如果有,輸入的又是'.',就什么都不做if (editTextStr.indexOf('.') != -1 && input.equals('.')) {return;}// 判斷顯示區域的值里面只有'-',輸入的又是'.',就什么都不做if (editTextStr.equals('-') && input.equals('.')) {return;}// 判斷顯示區域的值如果是'0',輸入的不是'.',就什么也不做if (editTextStr.equals('0') && !input.equals('.')) {return;}}// 如果我點擊了運算符以后,再輸入數字的話,就要清空顯示區域的值if (clearFlag) {editText.setText('');clearFlag = false;// 還原初始值,不需要清空}editText.setText(editText.getText().toString() + input);// 設置顯示區域的值}}// 符號按鈕監聽器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('-');// 顯示區域的內容設置為'-'firstFlag = false;// 改變首次輸入的標記}} else {if (!clearFlag) {// 如果flag=false不需要清空顯示區的值,就調用方法計算calculate(Double.parseDouble(editText.getText().toString()));// 保存顯示區域的值,并計算}// 保存你點擊的運算符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久久,三上悠亚国产精品一区二区三区
欧美成a人片免费观看久久五月天| 国产视频一区欧美| 快she精品国产999| 亚洲综合精品四区| 亚洲激情二区| 黄色欧美日韩| 美国三级日本三级久久99| 中文亚洲免费| 蜜桃视频一区二区三区在线观看| 视频一区二区国产| 日本亚洲不卡| 欧美性www| 电影91久久久| 国产精品久久久久久久久久10秀| 丰满少妇一区| 国产精品国产三级国产在线观看| 中文在线免费视频| 婷婷丁香综合| 日韩avvvv在线播放| 精品久久中文| 黄色亚洲精品| 欧美精品福利| 日韩精品免费一区二区三区| 欧美日韩精品一本二本三本 | 欧美成人基地 | 午夜欧美理论片| 亚洲人成毛片在线播放女女| 欧美日韩99| 久久精品动漫| 7777精品| 欧美日韩国产亚洲一区| 久久99精品久久久野外观看| 中文字幕在线视频网站| 中文字幕一区二区三区在线视频| 欧美精品97| 红桃视频欧美| 国产精品原创| 日本aⅴ免费视频一区二区三区| 婷婷综合六月| 国产精品久久| 欧美成人精品| 精品亚洲成人| 日本免费一区二区视频| 婷婷中文字幕一区| 伊人久久视频| 欧美91在线| 日韩极品在线观看| 亚洲精品va| 精品美女视频 | 麻豆精品av| 视频一区日韩| 在线一区免费观看| 日韩在线高清| 国内精品亚洲| 精品国产乱码久久久| 国产精品一二| 日韩va欧美va亚洲va久久| 一级欧美视频| 午夜久久av| 视频精品一区| 日韩av电影一区| 欧美一级二级三级视频| 午夜电影一区| 日本午夜精品久久久久| 日韩国产91| 欧美啪啪一区| 精品五月天堂| 国产盗摄——sm在线视频| 中文字幕成在线观看| 久久久久蜜桃| 亚洲欧美日韩专区| 免费欧美在线视频| 午夜精品福利影院| 久久麻豆视频| 国产欧洲在线| 亚洲免费观看| 日本午夜精品视频在线观看| 欧美日韩亚洲一区二区三区在线| 国产精品日本一区二区三区在线| 麻豆国产精品视频| 欧美成人基地| 亚洲2区在线| 国产一级成人av| 麻豆久久久久久| 九九综合九九| 91福利精品在线观看| 色婷婷综合网| 一区免费在线| 国产精品久久久久77777丨| 亚洲一级少妇| 婷婷久久免费视频| 97精品一区二区| 亚洲影视一区| 九九精品调教| 丝袜美腿亚洲一区| 国产精品porn| 久久xxxx| 欧美aaaaaa午夜精品| 自由日本语亚洲人高潮| 久久av日韩| 首页国产欧美久久| 首页国产精品| 69堂免费精品视频在线播放| 91成人超碰| 欧美香蕉视频| 麻豆成人在线观看| 一区二区电影在线观看| 日韩高清中文字幕一区二区| 欧美精品中文| 亚洲久久一区| 免费观看不卡av| 久久影院午夜精品| 国产欧美日韩精品高清二区综合区 | 国产探花在线精品| 男女性色大片免费观看一区二区 | 国产日韩在线观看视频| 米奇777超碰欧美日韩亚洲| 美腿丝袜亚洲一区| 国产精品欧美三级在线观看| 亚洲影视一区| 亚洲一区二区三区无吗| 狠狠久久婷婷| 99亚洲精品| 久久国产日韩| 亚洲va在线| 日韩综合精品| 日韩欧美在线中字| a日韩av网址| 亚洲不卡av不卡一区二区| 色乱码一区二区三区网站| 国产成人精品一区二区免费看京 | 免费在线亚洲| 国产一区二区三区探花| 国产福利一区二区三区在线播放| 青草久久视频| 国产日韩中文在线中文字幕 | 日韩精品亚洲专区在线观看| 亚洲五月综合| 亚洲欧洲日韩精品在线| 欧美日韩一区二区三区不卡视频| 国产精品宾馆| 国产一区二区三区视频在线| 亚洲天堂一区二区| 国产亚洲在线| 国产精东传媒成人av电影| 久久精品一区二区国产| 中文字幕在线高清| 欧美专区在线| 美女性感视频久久| 秋霞影视一区二区三区| 喷白浆一区二区| 麻豆国产精品视频| 午夜久久一区| 日韩欧美激情| 精品国产乱码久久久久久1区2匹| 久久久精品日韩| 日韩一区二区三区在线看| 久久成人av| 欧美精品九九| 欧美视频精品全部免费观看| 日本精品在线中文字幕| 日韩精品三级| 99精品电影| 国产精品手机在线播放| 99pao成人国产永久免费视频| 国产欧美一区二区三区米奇| 欧美日韩精品免费观看视完整| 欧美亚洲免费| 黄色免费成人| 精品欧美视频| 亚洲精品裸体| 久久影院一区| 精品视频一区二区三区在线观看 | 国产精品videosex极品| 国产视频一区三区| 老司机免费视频一区二区| 亚洲深夜av| 亚洲高清久久| 国产精品蜜芽在线观看| 国产精品久久久久久久久久齐齐 | 婷婷五月色综合香五月| 91精品高清| 日韩一区电影| 岛国精品一区| 美女久久久久久 | 日韩av免费大片| 国产精品v亚洲精品v日韩精品| 日韩精品三区四区| 一区二区不卡| 色综合视频一区二区三区日韩 | 国产精品婷婷| 欧美特黄一级| 亚洲大全视频| 久久精品国内一区二区三区水蜜桃| 国产精品久久久久久妇女| 久久国产欧美日韩精品| 欧美一区自拍| 国产精品porn| 日产精品一区二区| 夜鲁夜鲁夜鲁视频在线播放|