Android studio计算器功能
晴笑歌 人气:0用Android studio做一个简易计算器,供大家参考,具体内容如下
长话短说,先建立一个Android项目;
创建完成后打开activity_main.xml,构建我们的应用界面,输入以下代码:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" > <!--文本标签--> <EditText android:id="@+id/edit" android:layout_width="350dp" android:layout_height="wrap_content" android:layout_columnSpan="4" android:layout_marginLeft="4px" android:gravity="left" android:textSize="50dp" /> <Button android:id="@+id/clear" android:layout_width="353dp" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除" android:textSize="26sp" /> <Button android:id="@+id/btn1" android:text="1" android:textSize="26sp" /> <Button android:id="@+id/btn2" android:text="2" android:textSize="26sp" /> <Button android:id="@+id/btn3" android:text="3" android:textSize="26sp" /> <Button android:id="@+id/jia" android:text="+" android:textSize="26sp" /> <Button android:id="@+id/btn4" android:text="4" android:textSize="26sp" /> <Button android:id="@+id/btn5" android:text="5" android:textSize="26sp" /> <Button android:id="@+id/btn6" android:text="6" android:textSize="26sp" /> <Button android:id="@+id/jian" android:text="-" android:textSize="26sp" /> <Button android:id="@+id/btn7" android:text="7" android:textSize="26sp" /> <Button android:id="@+id/btn8" android:text="8" android:textSize="26sp" /> <Button android:id="@+id/btn9" android:text="9" android:textSize="26sp" /> <Button android:id="@+id/cheng" android:text="*" android:textSize="26sp" /> <Button android:id="@+id/dian" android:text="." android:textSize="26sp" /> <Button android:id="@+id/btn0" android:text="0" android:textSize="26sp" /> <Button android:id="@+id/sum" android:text="=" android:textSize="26sp" /> <Button android:id="@+id/chu" android:text="/" android:textSize="26sp" />
完成后再找到Mainactivity.java,输入以下代码:
package com.example.a00_ff_59_7f_7e_dc_54_3; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0; // 数字按钮 Button jia, jian, cheng, chu, dian, sum, clear;// +号 EditText edit; // 显示文本 private String ss = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取页面上的控件 btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); btn5 = (Button) findViewById(R.id.btn5); btn6 = (Button) findViewById(R.id.btn6); btn7 = (Button) findViewById(R.id.btn7); btn8 = (Button) findViewById(R.id.btn8); btn9 = (Button) findViewById(R.id.btn9); btn0 = (Button) findViewById(R.id.btn0); jia = (Button) findViewById(R.id.jia); jian = (Button) findViewById(R.id.jian); cheng = (Button) findViewById(R.id.cheng); chu = (Button) findViewById(R.id.chu); sum = (Button) findViewById(R.id.sum); dian = (Button) findViewById(R.id.dian); clear = (Button) findViewById(R.id.clear); edit = (EditText) findViewById(R.id.edit); // 按钮的单击事件 btn1.setOnClickListener(new Click()); btn2.setOnClickListener((View.OnClickListener) new Click()); btn3.setOnClickListener(new Click()); btn4.setOnClickListener(new Click()); btn5.setOnClickListener(new Click()); btn6.setOnClickListener(new Click()); btn7.setOnClickListener(new Click()); btn8.setOnClickListener(new Click()); btn9.setOnClickListener(new Click()); btn0.setOnClickListener(new Click()); jia.setOnClickListener(new Click()); jian.setOnClickListener(new Click()); cheng.setOnClickListener(new Click()); chu.setOnClickListener(new Click()); sum.setOnClickListener(new Click()); dian.setOnClickListener(new Click()); clear.setOnClickListener(new Click()); edit.setOnClickListener(new Click()); } // 设置按钮点击后的监听 class Click implements View.OnClickListener { public void onClick(View v) { switch (v.getId()) { //switch循环获取点击按钮后的值 case R.id.clear: { ss = ""; edit.setText(ss); } break; case R.id.btn0: { ss += "0"; edit.setText(ss); } break; case R.id.btn1: { ss += "1"; edit.setText(ss); } break; case R.id.btn2: { ss += "2"; edit.setText(ss); } break; case R.id.btn3: { ss += "3"; edit.setText(ss); } break; case R.id.btn4: { ss += "4"; edit.setText(ss); } break; case R.id.btn5: { ss += "5"; edit.setText(ss); } break; case R.id.btn6: { ss += "6"; edit.setText(ss); } break; case R.id.btn7: { ss += "7"; edit.setText(ss); } break; case R.id.btn8: { ss += "8"; edit.setText(ss); } break; case R.id.btn9: { ss += "0"; edit.setText(ss); } break; case R.id.dian: { if (ss.length() == 0 || ss.indexOf(" ") == ss.length() - 3 || ss.lastIndexOf(".") > ss.indexOf(" ")) { break; } else { ss += "."; edit.setText(ss); } } break; case R.id.jia: { if (ss.length() == 0) { break; } if (ss.contains(" ")) { if (ss.indexOf(" ") == ss.length() - 3 || ss.indexOf(" ") == ss.length() - 2 || ss.indexOf(" ") == ss.length() - 1) break; getResult(); } ss += " + "; edit.setText(ss); } break; case R.id.jian: { if (ss.length() == 0) { break; } if (ss.contains(" ")) { if (ss.indexOf(" ") == ss.length() - 3 || ss.indexOf(" ") == ss.length() - 2 || ss.indexOf(" ") == ss.length() - 1) break; getResult(); } ss += " - "; edit.setText(ss); } break; case R.id.cheng: { if (ss.length() == 0) { break; } if (ss.contains(" ")) { if (ss.indexOf(" ") == ss.length() - 3 || ss.indexOf(" ") == ss.length() - 2 || ss.indexOf(" ") == ss.length() - 1) break; getResult(); } ss += " × "; edit.setText(ss); } break; case R.id.chu: { if (ss.length() == 0) { break; } if (ss.contains(" ")) { if (ss.indexOf(" ") == ss.length() - 3 || ss.indexOf(" ") == ss.length() - 2 || ss.indexOf(" ") == ss.length() - 1) break; getResult(); } ss += " / "; edit.setText(ss); } break; case R.id.sum: getResult(); break; } } private void getResult() { double result = 0; if (ss == null || ss.equals("")) return; if (!ss.contains(" ")) return; String s1 = ss.substring(0, ss.indexOf(" ")); String op = ss.substring(ss.indexOf(" ") + 1, ss.indexOf(" ") + 2); String s2 = ss.substring(ss.indexOf(" ") + 3); if (!s1.equals("") && !s2.equals("")) { double d1 = Double.parseDouble(s1); double d2 = Double.parseDouble(s2); switch (op) { case "+": result = d1 + d2; break; case "-": result = d1 - d2; break; case "×": result = d1 * d2; break; case "÷": { if (d2 == 0) { edit.setText("不能除以零"); break; } result = d1 / d2 * 1.0; } break; } int r = (int) result; if (r == result) { edit.setText("" + r); ss = "" + r; } else { edit.setText(result + ""); ss = "" + result; } } } } }
这时我们可以预览应用程序运行时的界面:
接下来就可以用虚拟设备运行一下我们的应用:
依次检查上面三个地方,没问题后点击运行:
这是我们做好应用的图标,打开运行一下:
接下来进行真机运行,将手机连接到电脑,Android studio显示手机型号后点击运行:
打开安装到手机上的应用程序:
成功运行。
加载全部内容