今天小編給大家分享一下Android開(kāi)發(fā)怎么實(shí)現(xiàn)計(jì)算器功能的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的拉薩網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!先上圖看效果
比較簡(jiǎn)單,所以我就沒(méi)怎么寫(xiě)注釋,應(yīng)該一看就能明白的
有不明白的可以發(fā)信問(wèn)我
先貼MainActivity.java代碼
package com.example.calculator; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bp, bs, bm, bd, bc, be; ImageView delete; TextView tv; EditText show; String showString = "", option = ""; int showfirst = 0; String exception = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b0 = (Button) findViewById(R.id.bt_0); b1 = (Button) findViewById(R.id.bt_1); b2 = (Button) findViewById(R.id.bt_2); b3 = (Button) findViewById(R.id.bt_3); b4 = (Button) findViewById(R.id.bt_4); b5 = (Button) findViewById(R.id.bt_5); b6 = (Button) findViewById(R.id.bt_6); b7 = (Button) findViewById(R.id.bt_7); b8 = (Button) findViewById(R.id.bt_8); b9 = (Button) findViewById(R.id.bt_9); bp = (Button) findViewById(R.id.bt_plus); bs = (Button) findViewById(R.id.bt_sub); bm = (Button) findViewById(R.id.bt_mutilate); bd = (Button) findViewById(R.id.bt_div); bc = (Button) findViewById(R.id.bt_c); be = (Button) findViewById(R.id.bt_equ); b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); b4.setOnClickListener(this); b5.setOnClickListener(this); b6.setOnClickListener(this); b7.setOnClickListener(this); b8.setOnClickListener(this); b9.setOnClickListener(this); b0.setOnClickListener(this); bp.setOnClickListener(this); bs.setOnClickListener(this); bm.setOnClickListener(this); bd.setOnClickListener(this); bc.setOnClickListener(this); be.setOnClickListener(this); show = (EditText) findViewById(R.id.et_show); delete = (ImageView) findViewById(R.id.iv_delete); delete.setOnClickListener(this); tv=(TextView) findViewById(R.id.author); tv.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_0: showString += "0"; break; case R.id.bt_1: showString += "1"; break; case R.id.bt_2: showString += "2"; break; case R.id.bt_3: showString += "3"; break; case R.id.bt_4: showString += "4"; break; case R.id.bt_5: showString += "5"; break; case R.id.bt_6: showString += "6"; break; case R.id.bt_7: showString += "7"; break; case R.id.bt_8: showString += "8"; break; case R.id.bt_9: showString += "9"; break; case R.id.bt_plus: if (showString.equals("")) exception = "先輸入數(shù)值哦"; else { showfirst = Integer.parseInt(showString); showString = ""; option = "+"; } break; case R.id.bt_sub: if (showString.equals("")) exception = "先輸入數(shù)值哦"; else { showfirst = Integer.parseInt(showString); showString = ""; option = "-"; } break; case R.id.bt_mutilate: if (showString.equals("")) exception = "先輸入數(shù)值哦"; else { showfirst = Integer.parseInt(showString); showString = ""; option = "*"; } break; case R.id.bt_div: if (showString.equals("")) exception = "先輸入數(shù)值哦"; else { showfirst = Integer.parseInt(showString); showString = ""; option = "/"; } break; case R.id.bt_equ: if (option.equals("+")) showString = showfirst + Integer.parseInt(showString) + ""; else if (option.equals("-")) { showString = showfirst - Integer.parseInt(showString) + ""; } else if (option.equals("*")) { showString = showfirst * Integer.parseInt(showString) + ""; } else if (option.equals("/")) { if (showString.equals("0")) { exception = "除數(shù)不能為0!"; } else showString = showfirst / Integer.parseInt(showString) + ""; } break; case R.id.bt_c: showString = ""; break; case R.id.iv_delete: Toast.makeText(MainActivity.this, showString + "已被清空", Toast.LENGTH_SHORT).show(); showString = ""; break; case R.id.author: Toast.makeText(MainActivity.this, "鄭明亮\n軟件工程\nQQ:1072307340", Toast.LENGTH_SHORT).show(); break; default: break; } if (exception.equals("")) show.setText(showString); else { show.setText(exception); exception = ""; } // 設(shè)置文本框顏色; if (!show.getText().toString().equals("")) { delete.setBackgroundColor(R.drawable.delete_gray); } else { delete.setBackgroundResource(R.drawable.delete); } } }
再貼布局activity_main.xml:
我還寫(xiě)了一個(gè)drawable的xml,自己看吧
delete_and_deletegray.xml:
以上就是“Android開(kāi)發(fā)怎么實(shí)現(xiàn)計(jì)算器功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。