android?studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
本文實(shí)例為大家分享了android studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
先看效果圖
基本功能:加,減,乘,除
核心代碼實(shí)現(xiàn)
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_pt; ? ? Button btn_mul, btn_div, btn_add, btn_sub; ? ? Button btn_clr, btn_del, btn_eq; ? ? EditText et_input; ? ? boolean clr_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_pt = (Button) findViewById(R.id.btn_pt); ? ? ? ? btn_add = (Button) findViewById(R.id.btn_add); ? ? ? ? btn_sub = (Button) findViewById(R.id.btn_sub); ? ? ? ? btn_mul = (Button) findViewById(R.id.btn_mul); ? ? ? ? btn_div = (Button) findViewById(R.id.btn_div); ? ? ? ? btn_clr = (Button) findViewById(R.id.btn_clr); ? ? ? ? btn_del = (Button) findViewById(R.id.btn_del); ? ? ? ? btn_eq = (Button) findViewById(R.id.btn_eq); ? ? ? ? et_input = (EditText) findViewById(R.id.et_input); ? ? ? ? //給按鈕設(shè)置的點(diǎn)擊事件 ? ? ? ? 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_pt.setOnClickListener(this); ? ? ? ? btn_add.setOnClickListener(this); ? ? ? ? btn_sub.setOnClickListener(this); ? ? ? ? btn_mul.setOnClickListener(this); ? ? ? ? btn_div.setOnClickListener(this); ? ? ? ? btn_clr.setOnClickListener(this); ? ? ? ? btn_del.setOnClickListener(this); ? ? ? ? btn_eq.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_pt: ? ? ? ? ? ? ? ? if (clr_flag) { ? ? ? ? ? ? ? ? ? ? clr_flag = false; ? ? ? ? ? ? ? ? ? ? str = ""; ? ? ? ? ? ? ? ? ? ? et_input.setText(""); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? et_input.setText(str + ((Button) v).getText()); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.btn_add: ? ? ? ? ? ? case R.id.btn_sub: ? ? ? ? ? ? case R.id.btn_mul: ? ? ? ? ? ? case R.id.btn_div: ? ? ? ? ? ? ? ? if (clr_flag) { ? ? ? ? ? ? ? ? ? ? clr_flag = false; ? ? ? ? ? ? ? ? ? ? str = ""; ? ? ? ? ? ? ? ? ? ? et_input.setText(""); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (str.contains("+") || str.contains("-") || str.contains("×") || str.contains("÷")) { ? ? ? ? ? ? ? ? ? ? str = str.substring(0, str.indexOf(" ")); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? et_input.setText(str + " " + ((Button) v).getText() + " "); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.btn_clr: ? ? ? ? ? ? ? ? if (clr_flag) ? ? ? ? ? ? ? ? ? ? clr_flag = false; ? ? ? ? ? ? ? ? str = ""; ? ? ? ? ? ? ? ? et_input.setText(""); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.btn_del: //判斷是否為空,然后在進(jìn)行刪除 ? ? ? ? ? ? ? ? if (clr_flag) { ? ? ? ? ? ? ? ? ? ? clr_flag = false; ? ? ? ? ? ? ? ? ? ? str = ""; ? ? ? ? ? ? ? ? ? ? et_input.setText(""); ? ? ? ? ? ? ? ? } else if (str != null && !str.equals("")) { ? ? ? ? ? ? ? ? ? ? et_input.setText(str.substring(0, str.length() - 1)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.btn_eq: //單獨(dú)運(yùn)算最后結(jié)果 ? ? ? ? ? ? ? ? getResult();//調(diào)用下面的方法 ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? private void getResult() { ? ? ? ? String exp = et_input.getText().toString(); ? ? ? ? if (exp == null || exp.equals("")) return; ? ? ? ? //因?yàn)闆](méi)有運(yùn)算符所以不用運(yùn)算 ? ? ? ? if (!exp.contains(" ")) { ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? if (clr_flag) { ? ? ? ? ? ? clr_flag = false; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? clr_flag = true; ? ? ? ? //截取運(yùn)算符前面的字符串 ? ? ? ? String s1 = exp.substring(0, exp.indexOf(" ")); ? ? ? ? //截取的運(yùn)算符 ? ? ? ? String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2); ? ? ? ? //截取運(yùn)算符后面的字符串 ? ? ? ? String s2 = exp.substring(exp.indexOf(" ") + 3); ? ? ? ? double cnt = 0; ? ? ? ? if (!s1.equals("") && !s2.equals("")) { ? ? ? ? ? ? double d1 = Double.parseDouble(s1); ? ? ? ? ? ? double d2 = Double.parseDouble(s2); ? ? ? ? ? ? if (op.equals("+")) { ? ? ? ? ? ? ? ? cnt = d1 + d2; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("-")) { ? ? ? ? ? ? ? ? cnt = d1 - d2; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("×")) { ? ? ? ? ? ? ? ? cnt = d1 * d2; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("÷")) { ? ? ? ? ? ? ? ? if (d2 == 0) cnt = 0; ? ? ? ? ? ? ? ? else cnt = d1 / d2; ? ? ? ? ? ? } ? ? ? ? ? ? if (!s1.contains(".") && !s2.contains(".") && !op.equals("÷")) { ? ? ? ? ? ? ? ? int res = (int) cnt; ? ? ? ? ? ? ? ? et_input.setText(res + ""); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? et_input.setText(cnt + ""); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //如果s1是空 ? ?s2不是空 ?就執(zhí)行下一步 ? ? ? ? else if (!s1.equals("") && s2.equals("")) { ? ? ? ? ? ? double d1 = Double.parseDouble(s1); ? ? ? ? ? ? if (op.equals("+")) { ? ? ? ? ? ? ? ? cnt = d1; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("-")) { ? ? ? ? ? ? ? ? cnt = d1; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("×")) { ? ? ? ? ? ? ? ? cnt = 0; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("÷")) { ? ? ? ? ? ? ? ? cnt = 0; ? ? ? ? ? ? } ? ? ? ? ? ? if (!s1.contains(".")) { ? ? ? ? ? ? ? ? int res = (int) cnt; ? ? ? ? ? ? ? ? et_input.setText(res + ""); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? et_input.setText(cnt + ""); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //如果s1是空 ? ?s2不是空 ?就執(zhí)行下一步 ? ? ? ? else if (s1.equals("") && !s2.equals("")) { ? ? ? ? ? ? double d2 = Double.parseDouble(s2); ? ? ? ? ? ? if (op.equals("+")) { ? ? ? ? ? ? ? ? cnt = d2; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("-")) { ? ? ? ? ? ? ? ? cnt = 0 - d2; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("×")) { ? ? ? ? ? ? ? ? cnt = 0; ? ? ? ? ? ? } ? ? ? ? ? ? if (op.equals("÷")) { ? ? ? ? ? ? ? ? cnt = 0; ? ? ? ? ? ? } ? ? ? ? ? ? if (!s2.contains(".")) { ? ? ? ? ? ? ? ? int res = (int) cnt; ? ? ? ? ? ? ? ? et_input.setText(res + ""); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? et_input.setText(cnt + ""); ? ? ? ? ? ? } ? ? ? ? } else { ? ? ? ? ? ? et_input.setText(""); ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android SearchView不顯示搜索icon的問(wèn)題
這篇文章主要介紹了解決Android SearchView不顯示搜索icon問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Android App開(kāi)發(fā)的自動(dòng)化測(cè)試框架UI Automator使用教程
UI Automator為Android程序的UI開(kāi)發(fā)提供了測(cè)試環(huán)境,這里我們就來(lái)看一下Android App開(kāi)發(fā)的自動(dòng)化測(cè)試框架UI Automator使用教程,需要的朋友可以參考下2016-07-07Android自定義ViewGroup(側(cè)滑菜單)詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android自定義ViewGroup(側(cè)滑菜單)詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02Flutter實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求的方法示例
這篇文章主要介紹了Flutter實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03Android開(kāi)發(fā)實(shí)現(xiàn)消除屏幕鎖的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)消除屏幕鎖的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android鎖屏的原理及消除屏幕鎖的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09Android基于Fresco實(shí)現(xiàn)圓角和圓形圖片
這篇文章主要為大家詳細(xì)介紹了Android基于Fresco實(shí)現(xiàn)圓角和圓形圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04微信小程序首頁(yè)數(shù)據(jù)初始化失敗的解決方法
這篇文章主要介紹了微信小程序首頁(yè)數(shù)據(jù)初始化失敗的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android項(xiàng)目實(shí)戰(zhàn)之百度地圖地點(diǎn)簽到功能
這篇文章主要介紹了Android項(xiàng)目實(shí)戰(zhàn)之百度地圖地點(diǎn)簽到功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android Compose實(shí)現(xiàn)聯(lián)系人列表流程
聲明式UI,更簡(jiǎn)單的自定義,實(shí)時(shí)帶交互的預(yù)覽功能Compose并不是類似于Recyclerview的高級(jí)控件,而是直接拋棄了View,ViewGroup那套東西,從上到下魯了一套全新的框架,直白點(diǎn)說(shuō)就是它的渲染機(jī)制,布局機(jī)制,觸摸算法,以及UI具體寫法全都是新的2023-03-03Android編程中Handler原理及用法實(shí)例分析
這篇文章主要介紹了Android編程中Handler用法,結(jié)合實(shí)例形式分析了Handler的功能,原理及使用技巧,需要的朋友可以參考下2016-01-01