欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于Android studio3.6实现简单计算器

程序员文章站 2024-03-20 12:17:10
...

由于作业的需要,做一个简单的计算器过程记录下来。

实现简单的加、减、乘、除,逻辑比较简单适合新学者(毕竟我也是个新学者)。

此项目采用Android studio3.6.1

下面是控件布局文件的部分:简单的margin定位

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="计算器"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_One"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="1"
        app:layout_constraintBottom_toTopOf="@+id/button_zero"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button_zero"
        android:layout_width="165dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button_two"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="2"
        app:layout_constraintBottom_toTopOf="@+id/button_zero"
        app:layout_constraintStart_toEndOf="@+id/button_One"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button_three"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="3"
        app:layout_constraintBottom_toTopOf="@+id/button10_point"
        app:layout_constraintStart_toEndOf="@+id/button_two"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button10_point"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="·"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button_zero"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button11_equal"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="="
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button10_point"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button12_add"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="+"
        app:layout_constraintBottom_toTopOf="@+id/button11_equal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button_three"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button13_sub"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="-"
        app:layout_constraintBottom_toTopOf="@+id/button12_add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button6_six"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button6_six"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="6"
        app:layout_constraintBottom_toTopOf="@+id/button_three"
        app:layout_constraintStart_toEndOf="@+id/button_five"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button_four"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="4"
        app:layout_constraintBottom_toTopOf="@+id/button_One"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button17_quyu"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="%"
        app:layout_constraintBottom_toTopOf="@+id/button9_nine"
        app:layout_constraintStart_toEndOf="@+id/button18_*"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button9_nine"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="9"
        app:layout_constraintBottom_toTopOf="@+id/button6_six"
        app:layout_constraintStart_toEndOf="@+id/button8_eight"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button_five"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="5"
        app:layout_constraintBottom_toTopOf="@+id/button_two"
        app:layout_constraintStart_toEndOf="@+id/button_four"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/editText_result"
        android:layout_width="338dp"
        android:layout_height="82dp"
        android:layout_marginBottom="10dp"
        android:ems="10"
        android:inputType="number"
        android:text="0"
        android:cursorVisible="false"
        android:clickable="false"
        android:focusable="false"
        app:layout_constraintBottom_toTopOf="@+id/button17_quyu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button18_*"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="+/-"
        app:layout_constraintBottom_toTopOf="@+id/button8_eight"
        app:layout_constraintStart_toEndOf="@+id/button16_ac"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button7_seven"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="7"
        app:layout_constraintBottom_toTopOf="@+id/button_four"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button8_eight"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="8"
        app:layout_constraintBottom_toTopOf="@+id/button_five"
        app:layout_constraintStart_toEndOf="@+id/button7_seven" />

    <Button
        android:id="@+id/button16_ac"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="AC"
        app:layout_constraintBottom_toTopOf="@+id/button7_seven"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button15_chu"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="÷"
        app:layout_constraintBottom_toTopOf="@+id/button14_mul"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button17_quyu"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button14_mul"
        android:layout_width="90dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="5dp"
        android:text="×"
        app:layout_constraintBottom_toTopOf="@+id/button13_sub"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button9_nine"
        tools:ignore="MissingConstraints" />

基于Android studio3.6实现简单计算器

然后就是逻辑代码:

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding viewBinding;//新的控件绑定方法
    //private int OneNum = 0,TwoNum = 0,nowOne,nowTwo;
    private String state = "null",str = "",str1 = "",str2 = "";//缓存键值用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        viewBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        viewBinding.textView.setText("计算器");
        InitButton();
    }

    public void InitButton(){
        viewBinding.buttonZero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("num")){
                    str += "0";
                    str1 += "0";
                    viewBinding.editTextResult.setText(str);
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "0";
                    str2 += "0";
                    viewBinding.editTextResult.setText(str);
                }
                if(viewBinding.editTextResult.getText().toString().equals("0")){
                    viewBinding.editTextResult.setText("0");
                }
            }
        });//0
        viewBinding.buttonOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="1";
                    str1 += "1";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "1";
                    str2 += "1";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//1
        viewBinding.buttonTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="2";
                    str1 += "2";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "2";
                    str2 += "2";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//2
        viewBinding.buttonThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="3";
                    str1 += "3";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "3";
                    str2 += "3";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//3
        viewBinding.buttonFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="4";
                    str1 += "4";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "4";
                    str2 += "4";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//4
        viewBinding.buttonFive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="5";
                    str1 += "5";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "5";
                    str2 += "5";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//5
        viewBinding.button6Six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="6";
                    str1 += "6";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "6";
                    str2 += "6";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//6
        viewBinding.button7Seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="7";
                    str1 += "7";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "7";
                    str2 += "7";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//7
        viewBinding.button8Eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="8";
                    str1 += "8";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "8";
                    str2 += "8";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//8
        viewBinding.button9Nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("null")||state.equals("num")){
                    str +="9";
                    str1 += "9";
                    viewBinding.editTextResult.setText(str);
                    state = "num";
                }else if (state.equals("加")||state.equals("减")||state.equals("乘")||state.equals("除")){
                    str += "9";
                    str2 += "9";
                    viewBinding.editTextResult.setText(str);
                }
            }
        });//9
        viewBinding.button10Point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });//点
        viewBinding.button12Add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("num")){
                    str += "+";
                    viewBinding.editTextResult.setText(str);
                    state = "加";
                }
            }
        });//加
        viewBinding.button13Sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("num")){
                    str += "-";
                    viewBinding.editTextResult.setText(str);
                    state = "减";
                }
            }
        });//减
        viewBinding.button14Mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("num")){
                    str += "×";
                    viewBinding.editTextResult.setText(str);
                    state = "乘";
                }
            }
        });//乘
        viewBinding.button15Chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state.equals("num")){
                    str += "÷";
                    viewBinding.editTextResult.setText(str);
                    state = "除";
                }
            }
        });//除
        viewBinding.button17Quyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });//取余
        viewBinding.button18*.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });//正负切换
        viewBinding.button16Ac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewBinding.editTextResult.setText("0");
                state = "null";
                str = "";
                str1 = "";
                str2 = "";
            }
        });//归零
        viewBinding.button11Equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a,b,c;
                switch (state){
                    case "null":
                        viewBinding.editTextResult.setText("0");
                        break;
                    case "num":
                        viewBinding.editTextResult.setText(str1);
                        break;
                    case "加":
                        if (!str1.equals("")&&!str2.equals("")){
                            a = Integer.parseInt(str1);
                            b = Integer.parseInt(str2);
                            c = a+b;
                            viewBinding.editTextResult.setText(String.valueOf(c));
                            state = "num";
                            str = String.valueOf(c);
                            str1 = String.valueOf(c);
                            str2 = "";
                        }else {
                            Toast.makeText(getApplicationContext(),"请完整输入",Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case "减":
                        a = Integer.parseInt(str1);
                        b = Integer.parseInt(str2);
                        c = a-b;
                        viewBinding.editTextResult.setText(String.valueOf(c));
                        state = "num";
                        str = String.valueOf(c);
                        str1 = String.valueOf(c);
                        str2 = "";
                        break;
                    case "乘":
                        a = Integer.parseInt(str1);
                        b = Integer.parseInt(str2);
                        c = a*b;
                        viewBinding.editTextResult.setText(String.valueOf(c));
                        state = "num";
                        str = String.valueOf(c);
                        str1 = String.valueOf(c);
                        str2 = "";
                        break;
                    case "除":
                        a = Integer.parseInt(str1);
                        b = Integer.parseInt(str2);
                        c = a/b;
                        viewBinding.editTextResult.setText(String.valueOf(c));
                        state = "num";
                        str = String.valueOf(c);
                        str1 = String.valueOf(c);
                        str2 = "";
                        break;
                }
            }
        });//等于
    }
}

这里需要说明的是

   ActivityMainBinding

要实现此功能需要在build.gradle(:app)中添加:

android{
    viewBinding{//
        enabled = true//
    }//
}

然后sync一下就行了

这就是全部的代码,功能没有完全实现,需要通过调试增加逻辑方可完善。

如果有什么不足请大家指正。

后面我会把Demo放出来供参考-->

https://download.csdn.net/download/cm_panpan/12258460