android?studio實現簡易的計算器
更新時間:2022年05月22日 14:58:39 作者:一個地道的碼仔
這篇文章主要為大家詳細介紹了android?studio實現簡易的計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android studio實現簡易計算器的具體代碼,供大家參考,具體內容如下
先看效果圖
基本功能:加,減,乘,除
核心代碼實現
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); ? ? ? ? //給按鈕設置的點擊事件 ? ? ? ? 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: //判斷是否為空,然后在進行刪除 ? ? ? ? ? ? ? ? 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: //單獨運算最后結果 ? ? ? ? ? ? ? ? getResult();//調用下面的方法 ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? private void getResult() { ? ? ? ? String exp = et_input.getText().toString(); ? ? ? ? if (exp == null || exp.equals("")) return; ? ? ? ? //因為沒有運算符所以不用運算 ? ? ? ? if (!exp.contains(" ")) { ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? if (clr_flag) { ? ? ? ? ? ? clr_flag = false; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? clr_flag = true; ? ? ? ? //截取運算符前面的字符串 ? ? ? ? String s1 = exp.substring(0, exp.indexOf(" ")); ? ? ? ? //截取的運算符 ? ? ? ? String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2); ? ? ? ? //截取運算符后面的字符串 ? ? ? ? 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(""); ? ? ? ? } ? ? } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決Android SearchView不顯示搜索icon的問題
這篇文章主要介紹了解決Android SearchView不顯示搜索icon問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05Android App開發(fā)的自動化測試框架UI Automator使用教程
UI Automator為Android程序的UI開發(fā)提供了測試環(huán)境,這里我們就來看一下Android App開發(fā)的自動化測試框架UI Automator使用教程,需要的朋友可以參考下2016-07-07Android自定義ViewGroup(側滑菜單)詳解及簡單實例
這篇文章主要介紹了Android自定義ViewGroup(側滑菜單)詳解及簡單實例的相關資料,需要的朋友可以參考下2017-02-02