Android计算器-项目3

本文介绍了Android计算器的实现过程,包括XML布局文件Hyl_editstyle1.xml、Hyl_btnstyle3.xml、hyl_btn34.xml和Activity_main.xml的设计,以及对应的按钮点击事件处理,实现了数字输入和四则运算功能。
摘要由CSDN通过智能技术生成

hyl_btn34.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/hyl_btnstyle3" android:state_pressed="false"/>
    <item android:drawable="@drawable/hyl_btnstyle4" android:state_pressed="true"/>
</selector>

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@drawable/calculator"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/txt_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0"
        android:gravity="right"
        android:textSize="40sp"
        android:textColor="#000000"
        android:padding="10dp"
        android:layout_margin="10dp"
        android:background="@drawable/hyl_editstyle1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="7"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="8"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="9"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_jia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="+"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="4"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="5"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="6"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_jian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="-"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="1"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="2"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="3"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_cheng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="*"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4">
        <Button
            android:id="@+id/btn_0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="0"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_qing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="C"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_deng"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="="
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btn_chu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/hyl_btn34"
            android:padding="5dp"
            android:text="/"
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {
    private TextView txt_result;
    private Button btn_7;
    private Button btn_8;
    private Button btn_9;
    private Button btn_jia;
    private Button btn_4;
    private Button btn_5;
    private Button btn_6;
    private Button btn_jian;
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_cheng;
    private Button btn_0;
    private Button btn_qing;
    private Button btn_deng;
    private Button btn_chu;
    private double num1=0,num2=0;   //声明两个参数,接受数据
    private double result=0;    //运算结果
    Boolean isClickdeng=false;  //判断是否单击了=
    String op="%";  //操作符+-*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件
        txt_result=findViewById(R.id.txt_result);
        btn_7 = findViewById(R.id.btn_7);
        btn_8 = findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_jia = (Button) findViewById(R.id.btn_jia);
        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_jian = (Button) findViewById(R.id.btn_jian);
        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_cheng = (Button) findViewById(R.id.btn_cheng);
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_qing = (Button) findViewById(R.id.btn_qing);
        btn_deng = (Button) findViewById(R.id.btn_deng);
        btn_chu = (Button) findViewById(R.id.btn_chu);
        btn_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"7");
            }
        });
        btn_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"8");
            }
        });
        btn_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"9");
            }
        });
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"4");
            }
        });
        btn_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"5");
            }
        });
        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"6");
            }
        });
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"1");
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"2");
            }
        });
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"3");
            }
        });
        btn_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClickdeng){    //说明刚单击了=,上一个运算刚结束
                    txt_result.setText(""); //重新计算,文本框清空
                    isClickdeng=false;  //更改=按钮的状态
                }
                txt_result.setText(txt_result.getText().toString()+"0");
            }
        });
        btn_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String st1=txt_result.getText().toString();//获取点了加号之前字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1=Double.parseDouble(st1);
                txt_result.setText("");
                op="+";
                isClickdeng=false;

            }
        });
        btn_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String st1=txt_result.getText().toString();//获取点了加号之前字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1=Double.parseDouble(st1);
                txt_result.setText("");
                op="-";
                isClickdeng=false;
            }
        });
        btn_cheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String st1=txt_result.getText().toString();//获取点了加号之前字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1=Double.parseDouble(st1);
                txt_result.setText("");
                op="*";
                isClickdeng=false;
            }
        });
        btn_chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String st1=txt_result.getText().toString();//获取点了加号之前字符串类型的数据
                if(st1.equals("")){
                    return;
                }
                num1=Double.parseDouble(st1);
                txt_result.setText("");
                op="/";
                isClickdeng=false;
            }
        });
        btn_qing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt_result.setText("");
            }
        });
        btn_deng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str2=txt_result.getText().toString();
                if(str2.equals("")){
                    return;
                }
                num2=Double.parseDouble(str2);
                txt_result.setText("");
                switch (op){
                    case "+":result=num1+num2;break;
                    case "-":result=num1-num2;break;
                    case "*":result=num1*num2;break;
                    case "/":result=num1/num2;break;
                    case "%":result=num2;break;
                    default:result=0.0;break;
                }
                txt_result.setText(result+"");
                op="=";
                isClickdeng=true;
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值