欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android?studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器

 更新時(shí)間:2022年05月22日 14:58:39   作者:一個(gè)地道的碼仔  
這篇文章主要為大家詳細(xì)介紹了android?studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

最新評(píng)論