亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

Android入門計(jì)算器編寫(xiě)代碼

瀏覽:7日期:2022-09-23 08:59:12

這個(gè)簡(jiǎn)易計(jì)算器是我按照一本android開(kāi)發(fā)入門書(shū)學(xué)的,書(shū)上的第一個(gè)例子就是計(jì)算器的編寫(xiě)。計(jì)算器的編寫(xiě)主要涉及到按鍵的布局和按鍵輸入要點(diǎn)。

一個(gè)總的Lnearlayout的布局下orientation設(shè)置為vertical垂直分布,然后此布局下再設(shè)置1給我Edittext的一個(gè)文本框4個(gè)Lnearlayout子布局(第4個(gè)布局里可以嵌套另外3個(gè)Lnearlayout的布局來(lái)實(shí)現(xiàn)按鈕排版)這4個(gè)子布局在你的界面上肯定是垂直分布的,因?yàn)槟愕目偛季衷O(shè)置vertical。第一個(gè)子布局放置4個(gè)Button,分別是mc、m+、m-和mr這4個(gè)功能按鈕。

Android入門計(jì)算器編寫(xiě)代碼

布局代碼就不貼了,貼下加減乘除的代碼。

package com.example.boss.calculator;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_equal, btn_point, btn_clean, btn_del, btn_plus, btn_minus, btn_multiply, btn_divide; EditText et_input; boolean clear_flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_clean = (Button) findViewById(R.id.btn_clean); btn_equal = (Button) findViewById(R.id.btn_equal); btn_minus = (Button) findViewById(R.id.btn_minus); btn_multiply = (Button) findViewById(R.id.btn_multiplay); btn_plus = (Button) findViewById(R.id.btn_plus); btn_point = (Button) findViewById(R.id.btn_point); btn_del = (Button) findViewById(R.id.btn_del); btn_divide = (Button) findViewById(R.id.btn_divide); et_input = (EditText) findViewById(R.id.et_input); 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_equal.setOnClickListener(this); btn_minus.setOnClickListener(this); btn_multiply.setOnClickListener(this); btn_divide.setOnClickListener(this); btn_del.setOnClickListener(this); btn_point.setOnClickListener(this); btn_plus.setOnClickListener(this); btn_clean.setOnClickListener(this); et_input.setOnClickListener(this); } @Override public void onClick(View v) { String str = et_input.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 = ''; et_input.setText(''); } et_input.setText(str + ((Button) v).getText()); break; case R.id.btn_plus: case R.id.btn_minus: case R.id.btn_multiplay: case R.id.btn_divide: if (clear_flag) { clear_flag = false; str = ''; et_input.setText(''); } et_input.setText(str + ' ' + ((Button) v).getText() + ' '); break; case R.id.btn_clean: clear_flag = false; et_input.setText(''); break; case R.id.btn_del: if (clear_flag) { clear_flag = false; et_input.setText(''); } else if (str != null && !str.equals('')) { et_input.setText(str.substring(0, str.length() - 1)); } case R.id.btn_equal: getResult(); break; default: break; } } private void getResult() { String exp = et_input.getText().toString(); if(exp==null||exp.equals('')){ return; } if(!exp.contains(' ')){ return; } if(clear_flag){ clear_flag=false; return ; } clear_flag = true; double result = 0; String s1 = exp.substring(0, exp.indexOf(' ')); String op = exp.substring(exp.indexOf(' ') + 1, exp.indexOf(' ') + 2); String s2 = exp.substring(exp.indexOf(' ') + 3); if (!s1.equals('') && !s2.equals('')) { double d1 = Double.parseDouble(s1); double d2 = Double.parseDouble(s2); if (op.equals('+')) { result = d1 + d2; } else if (op.equals('-')) { result = d1 - d2; } else if (op.equals('*')) { result = d1 * d2; } else if (op.equals('/')) { if (d2 == 0) { result = 0; } else { result = d1 / d2; } } if (!s1.contains('.') && !s2.contains('.') && !op.equals('/')) { int r = (int) result; et_input.setText(r + ''); } else { et_input.setText(result + ''); } } else if(!s1.equals('')&&s2.equals('')){ et_input.setText(exp); } else if(s1.equals('')&&!s2.equals('')){ double d2=Double.parseDouble(s2); if(op.equals('+')){ result=0+d2; }else if(op.equals('-')){ result=0-d2; }else if(op.equals('*')){ result=0; }else if(op.equals('/')){ result = 0; } if(!s2.contains('.')){ int r=(int) result; et_input.setText(r+' '); } else{ et_input.setText(result+' '); } }else{ et_input.setText(''); } }}

更多計(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)文章:
主站蜘蛛池模板: 国产乱码精品一区二区 | 在线视频观看一区 | 亚洲欧美综合网 | 色婷婷香蕉| 亚洲精品国产美女在线观看 | 精品一区二区三区免费站 | 亚洲综合欧美色综合小说 | 亚洲国产精品视频在线观看 | aa级毛片毛片免费观看久 | 亚洲国产人成中文幕一级二级 | 国产自在线 | 日韩 亚洲 制服 欧美 综合 | 国产精品video | 欧美日韩亚毛片免费观看 | 久久毛片免费看 | 亚洲图片在线播放 | 久久免费精品一区二区 | 亚洲男女视频 | 久久精品韩国三级 | 日韩久久一级毛片 | 黄色免费观看 | 欧美成人中文字幕 | 91青青草| 亚洲综合伦理一区 | 欧美黄成人免费网站大全 | 久久国产精品成人免费 | 久久精品视频大全 | 人人澡人人澡碰人人看软件 | 亚洲黄色小视频 | 欧美视频一区二区在线观看 | 欧美一级黄色录相 | 日韩特黄特色大片免费视频 | 免费一级在线观看 | 亚洲精品国产一区二区三区在 | 精品视频免费 | 久草一级片 | 精品理论片一区二区三区 | 国产黄色大片网站 | 欧美人成片免费看视频不卡 | 国产一级黄色片子 | 久草在线视频资源站 |