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)闆]有運(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實(shí)現(xiàn)自動(dòng)播放圖片功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)播放圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android中實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)服務(wù)(service)實(shí)例
這篇文章主要介紹了Android中實(shí)現(xiàn)自動(dòng)啟動(dòng)服務(wù)實(shí)例,并開機(jī)自動(dòng)啟用(無activity),的朋友可以參考下2014-06-06
使用Android studio創(chuàng)建的AIDL編譯時(shí)找不到自定義類的解決辦法
這篇文章主要介紹了使用Android studio創(chuàng)建的AIDL編譯時(shí)找不到自定義類的解決辦法的相關(guān)資料,需要的朋友可以參考下2016-02-02
詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案
這篇文章主要介紹了詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
Flutter利用Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取效果
這篇文章主要為大家詳細(xì)介紹了如何利用Flutter中的Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03
Android編程UI設(shè)計(jì)之GridView和ImageView的用法
這篇文章主要介紹了Android編程UI設(shè)計(jì)之GridView和ImageView的用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中GridView和ImageView組件的相關(guān)方法使用技巧,需要的朋友可以參考下2016-01-01
Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕
本文通過實(shí)例代碼給大家詳解Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕,對(duì)alertdialog退出按鈕相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
android接收到藍(lán)牙配對(duì)請(qǐng)求時(shí)如何點(diǎn)亮屏幕具體實(shí)現(xiàn)
android 在接收到藍(lán)牙配對(duì)請(qǐng)求時(shí)如何自動(dòng)點(diǎn)亮屏幕配對(duì)過程中很實(shí)用,具體的實(shí)現(xiàn)思路及代碼如下,感興趣的朋友可以參考下哈2013-06-06

