Android版计算器(java实现,包含小数、负数、括号)代码和讲解

1.界面设计

使用的ConstraintLayout作为主布局容器。
关于ConstrainLayout如何使用
结果如下
在这里插入图片描述

设计代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/output"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:gravity="bottom|right"
        style="@style/OutputTextStyle"
        android:textStyle="bold"
        android:hint="0"
        android:padding="10dp"
        app:layout_constraintBottom_toTopOf="@id/line"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <View
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        app:layout_constraintBottom_toTopOf="@id/add"
        android:background="@color/black"/>

    <Button
        android:id="@+id/one"
        style="@style/ButtonStyle"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:text="1"
        android:layout_marginLeft="5dp"
        app:layout_constraintBottom_toTopOf="@id/dot"
        app:layout_constraintRight_toLeftOf="@id/two"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/four"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        android:onClick="onClick" />

    <Button
        android:id="@+id/two"
        style="@style/ButtonStyle"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:text="2"
        app:layout_constraintBottom_toTopOf="@id/zero"
        app:layout_constraintLeft_toRightOf="@+id/one"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/three"
        android:layout_width="100dp"
        android:layout_height="80dp"
        style="@style/ButtonStyle"
        android:text="3"
        app:layout_constraintBottom_toTopOf="@id/equal"
        app:layout_constraintLeft_toRightOf="@id/two"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/del"
        android:layout_width="100dp"
        android:layout_height="80dp"
        style="@style/ButtonStyle"
        android:text="C"
        app:layout_constraintBottom_toTopOf="@id/nine"
        app:layout_constraintLeft_toRightOf="@id/rightParenthesis"
        android:onClick="onClick"
        />

    <Button
        android:id="@+id/four"
        style="@style/ButtonStyle"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:text="4"
        app:layout_constraintBottom_toTopOf="@id/one"
        app:layout_constraintStart_toStartOf="parent"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/five"
        style="@style/ButtonStyle"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:text="5"
        app:layout_constraintStart_toEndOf="@id/four"
        app:layout_constraintBottom_toTopOf="@id/two"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/six"
        android:layout_width="100dp"
        android:layout_height="80dp"
        style="@style/ButtonStyle"
        android:text="6"
        app:layout_constraintStart_toEndOf="@id/five"
        app:layout_constraintBottom_toTopOf="@id/three"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/seven"
        style="@style/ButtonStyle"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:text="7"
        app:layout_constraintBottom_toTopOf="@id/four"
        app:layout_constraintLeft_toLeftOf="parent"
        a
  • 0
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个简单的Android Studio计算器的示例代码: MainActivity.java ``` package com.example.calculator; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView mResultTV; private Button mOneBtn, mTwoBtn, mThreeBtn, mFourBtn, mFiveBtn, mSixBtn, mSevenBtn, mEightBtn, mNineBtn, mZeroBtn; private Button mAddBtn, mSubtractBtn, mMultiplyBtn, mDivideBtn, mClearBtn, mEqualsBtn; private String mNumOne = ""; private String mNumTwo = ""; private String mOperator = ""; private String mResult = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initialize views mResultTV = findViewById(R.id.result_tv); mOneBtn = findViewById(R.id.one_btn); mTwoBtn = findViewById(R.id.two_btn); mThreeBtn = findViewById(R.id.three_btn); mFourBtn = findViewById(R.id.four_btn); mFiveBtn = findViewById(R.id.five_btn); mSixBtn = findViewById(R.id.six_btn); mSevenBtn = findViewById(R.id.seven_btn); mEightBtn = findViewById(R.id.eight_btn); mNineBtn = findViewById(R.id.nine_btn); mZeroBtn = findViewById(R.id.zero_btn); mAddBtn = findViewById(R.id.add_btn); mSubtractBtn = findViewById(R.id.subtract_btn); mMultiplyBtn = findViewById(R.id.multiply_btn); mDivideBtn = findViewById(R.id.divide_btn); mClearBtn = findViewById(R.id.clear_btn); mEqualsBtn = findViewById(R.id.equals_btn); // set listeners mOneBtn.setOnClickListener(this); mTwoBtn.setOnClickListener(this); mThreeBtn.setOnClickListener(this); mFourBtn.setOnClickListener(this); mFiveBtn.setOnClickListener(this); mSixBtn.setOnClickListener(this); mSevenBtn.setOnClickListener(this); mEightBtn.setOnClickListener(this); mNineBtn.setOnClickListener(this); mZeroBtn.setOnClickListener(this); mAddBtn.setOnClickListener(this); mSubtractBtn.setOnClickListener(this); mMultiplyBtn.setOnClickListener(this); mDivideBtn.setOnClickListener(this); mClearBtn.setOnClickListener(this); mEqualsBtn.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.one_btn: updateNum("1"); break; case R.id.two_btn: updateNum("2"); break; case R.id.three_btn: updateNum("3"); break; case R.id.four_btn: updateNum("4"); break; case R.id.five_btn: updateNum("5"); break; case R.id.six_btn: updateNum("6"); break; case R.id.seven_btn: updateNum("7"); break; case R.id.eight_btn: updateNum("8"); break; case R.id.nine_btn: updateNum("9"); break; case R.id.zero_btn: updateNum("0"); break; case R.id.add_btn: setOperator("+"); break; case R.id.subtract_btn: setOperator("-"); break; case R.id.multiply_btn: setOperator("*"); break; case R.id.divide_btn: setOperator("/"); break; case R.id.clear_btn: clear(); break; case R.id.equals_btn: calculate(); break; } } private void updateNum(String num) { if (mOperator.isEmpty()) { mNumOne += num; mResultTV.setText(mNumOne); } else { mNumTwo += num; mResultTV.setText(mNumTwo); } } private void setOperator(String operator) { if (!mNumOne.isEmpty() && mNumTwo.isEmpty()) { mOperator = operator; mResultTV.setText(""); } else if (!mNumOne.isEmpty() && !mNumTwo.isEmpty()) { calculate(); mOperator = operator; mNumTwo = ""; } } private void clear() { mNumOne = ""; mNumTwo = ""; mOperator = ""; mResultTV.setText(""); } private void calculate() { if (!mNumOne.isEmpty() && !mNumTwo.isEmpty() && !mOperator.isEmpty()) { double numOne = Double.parseDouble(mNumOne); double numTwo = Double.parseDouble(mNumTwo); double result = 0; switch (mOperator) { case "+": result = numOne + numTwo; break; case "-": result = numOne - numTwo; break; case "*": result = numOne * numTwo; break; case "/": result = numOne / numTwo; break; } mResult = String.valueOf(result); mResultTV.setText(mResult); mNumOne = mResult; mNumTwo = ""; mOperator = ""; } } } ``` activity_main.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/result_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:textSize="24sp" android:textAlignment="textEnd" android:text=""/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/seven_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="7"/> <Button android:id="@+id/eight_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="8"/> <Button android:id="@+id/nine_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="9"/> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/four_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4"/> <Button android:id="@+id/five_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5"/> <Button android:id="@+id/six_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="6"/> <Button android:id="@+id/subtract_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="-"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/one_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1"/> <Button android:id="@+id/two_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2"/> <Button android:id="@+id/three_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3"/> <Button android:id="@+id/multiply_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="*"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/zero_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="0"/> <Button android:id="@+id/clear_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="C"/> <Button android:id="@+id/equals_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="="/> <Button android:id="@+id/divide_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="/"/> </LinearLayout> </LinearLayout> ``` 这个计算器只支持整数和小数的四则运算,如果需要增加更多功能,请根据需求添加。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值