广告
返回顶部
首页 > 资讯 > 移动开发 >Android Studio实现简单计算器开发
  • 134
分享到

Android Studio实现简单计算器开发

2024-04-02 19:04:59 134人浏览 独家记忆
摘要

本文实例为大家分享了Android Studio实现简单计算器开的具体代码,供大家参考,具体内容如下 效果展示: 路径和文件: AndroidManifest.xml <

本文实例为大家分享了Android Studio实现简单计算器开的具体代码,供大家参考,具体内容如下

效果展示:

路径和文件:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="Http://schemas.android.com/apk/res/android"
    package="com.example.calculator">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <cateGory android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

MainActivity.java

package com.example.calculator; 
 
import android.os.Bundle;
 
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
import androidx.appcompat.app.AppCompatActivity;
 
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
 
    private  StringBuilder show_equation=new StringBuilder();//显示运算式
    private  ArrayList calculate_equation;//计算式
    private  int signal=0;//为0 时表示刚输入状态;为1 时表示当前在输出结果上继续输入
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        show_equation=new StringBuilder();
        calculate_equation=new ArrayList<>();
        Button zero=(Button)findViewById(R.id.zero);
        Button one=(Button)findViewById(R.id.one);
        Button two=(Button)findViewById(R.id.two);
        Button three=(Button)findViewById(R.id.three);
        Button four=(Button)findViewById(R.id.four);
        Button five=(Button)findViewById(R.id.five);
        Button six=(Button)findViewById(R.id.six);
        Button seven=(Button)findViewById(R.id.seven);
        Button eight=(Button)findViewById(R.id.eight);
        Button nine=(Button)findViewById(R.id.nine);
        Button cls=(Button)findViewById(R.id.cls);
        Button div=(Button)findViewById(R.id.div);
        Button mul=(Button)findViewById(R.id.mul);
        Button backspace=(Button)findViewById(R.id.Backspace);
        Button sub=(Button)findViewById(R.id.sub);
        Button add=(Button)findViewById(R.id.add);
        final Button equal=(Button)findViewById(R.id.equal);
        final Button point=(Button)findViewById(R.id.spot);
        final EditText result=(EditText)findViewById(R.id.result);
        result.setCursorVisible(true);
        disableShowInput(result);
        //点击文本框时光标始终在文本末尾
        result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result.setSelection(result.getText().length());
            }
        });
        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                if(!(show_equation.toString().equals("0"))){
                    if(signal==0){
                        show_equation.append("0");
                        //显示运算式
                        result.setText(show_equation);
                        //将光标定位到文本末尾
                        result.setSelection(result.getText().length());
                    }else{
                        show_equation.delete(0,show_equation.length());
                        show_equation.append("0");
                        result.setText(show_equation);
                        result.setSelection(result.getText().length());
                        signal=0;
                    }
                }
            }
        });
        //接下来1到9每个控件依次进行此设置
        //保证若是在结果上进行输入时清除结果然后显示点击的数字
        //若是正常输入则直接在运算式末尾加上点击的数字并显示
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("1");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("1");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("2");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("2");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("3");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("3");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("4");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("4");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("5");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("5");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("6");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("6");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("7");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("7");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("8");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("8");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(signal==0){
                    show_equation.append("9");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append("9");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
        //清屏
        cls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show_equation.delete(0,show_equation.length());
                calculate_equation.clear();
                signal=0;
                result.setText("");
            }
        });
        //后退键
        //若在结果上使用,则直接清屏
        //正常输入时使用,后退一格
        backspace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!(show_equation.toString().equals(""))) {
                    if(signal==0){
                        show_equation.deleteCharAt(show_equation.length() - 1);
                        result.setText(show_equation);
                        result.setSelection(result.getText().length());
                    }else{
                        show_equation.delete(0,show_equation.length());
                        result.setText("");
                        signal=0;
                    }
                }
            }
        });
        //小数点的点击事件
        point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //正常输入时点击小数点的处理逻辑
                if(signal==0){
                    //把运算式赋给字符串a
                    String a=show_equation.toString();
                    //运算式为空,直接加一个小数点并显示
                    if(a.equals("")){
                        show_equation.append(".");
                        result.setText(show_equation);
                        result.setSelection(result.getText().length());
                    }
                    //运算式不为空
                    else{
                        int i;
                        char t='0';
                        //从运算式末尾向前遍历,碰到'.''+''-''*''/'后结束遍历退出
                        for(i=a.length();i>0;i--){
                            t=a.charAt(i-1);
                            if(t=='.'||t=='+'||t=='-'||t=='*'||t=='/')
                                break;
                        }
                        //i==0表示遍历运算式没有发现'.''+''-''*''/',则直接在运算式末尾加小数点
                        if(i==0){
                            show_equation.append(".");
                            result.setText(show_equation);
                            result.setSelection(result.getText().length());
                        }
                        //在碰到小数点前碰到了'+''-''*''/',也直接在运算式末尾加小数点
                        else if(t=='+'||t=='-'||t=='*'||t=='/'){
                            show_equation.append(".");
                            result.setText(show_equation);
                            result.setSelection(result.getText().length());
                        }
                        //以上条件均不满足,若说明遍历碰到了小数点,因为一个数不能同时有两个小数点,所以此次点击小数点不做处理
                    }
                }
                //在结果上点击小数点,直接清屏然后加上小数点并显示
                else{
                    show_equation.delete(0,show_equation.length());
                    show_equation.append(".");
                    result.setText(".");
                    result.setSelection(result.getText().length());
                    signal=0;
                }
            }
        });
 
        equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否输入了内容
                if(!show_equation.toString().equals("")&&!(show_equation.toString().equals("错误"))){
                    signal = 1;//表示在输入结果上
                    char temp = show_equation.charAt(show_equation.length() - 1);//把运算式的最后一个字符赋给temp
                    if (show_equation.charAt(0) == '-')//如果运算式的第一个字符是'-',说明用户是想输入一个负数
                        show_equation.insert(0, "0");//此时在运算式的最前面加一个0,用'0-运算数'表示负数
                    if (temp == '+' || temp == '-')//若为加减
                        show_equation.append("0");//则结尾默认加减零
                    if (temp == '*' || temp == '/')//若为乘除
                        show_equation.append("1");//则结尾默认乘除1
                    StringBuilder temp1=new StringBuilder();
                    //从头遍历运算式
                    for(int i=0;i<show_equation.length();i++){
                        if(show_equation.charAt(i)>='0'&&show_equation.charAt(i)<='9'||show_equation.charAt(i)=='.'){
                            //如果是连着的数字和小数点,那就一个一个的拼到temp1中,作为一个完整的运算数
                            temp1.append(String.valueOf(show_equation.charAt(i)));
                        }else if(show_equation.charAt(i)=='N'){
                            //如果是NaN就直接把'NaN'加进计算式里
                            calculate_equation.add("NaN");
                            //跳过2个字符
                            i=i+2;
                        }else if(show_equation.charAt(i)=='∞'){
                            //如果是∞就直接把'∞'加进计算式里
                            calculate_equation.add("∞");
                        }
                        else//这种就是遍历到'+''-''*''/'这四种运算符的情况
                        {
                            //如果temp1长度不为0,说明运算符前有运算数,则把该运算数添加进计算式中并清空temp1中的内容
                            if(temp1.length()!=0){
                                calculate_equation.add(temp1.toString());
                                temp1.delete(0,temp1.length());
                            }
                            //把运算符添加进去
                            calculate_equation.add(String.valueOf(show_equation.charAt(i)));
                        }
                    }
                    //把最后一个运算数加进运算式里
                    if(temp1.length()!=0){
                        calculate_equation.add(temp1.toString());
                    }
                    calculate_equation.add("#");
                    //调用calculate计算出结果返回给temp8
                    String temp8=calculate(calculate_equation);
                    result.setText(temp8);
                    result.setSelection(result.getText().length());
                    //清空运算式和计算式
                    show_equation.delete(0,show_equation.length());
                    calculate_equation.clear();
                    //将结果赋给运算式
                    show_equation.append(temp8);
                }
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否输入了内容
                if(!(show_equation.toString().equals(""))&&!(show_equation.toString().equals("错误"))) {
                    signal=0;
                    char temp=show_equation.charAt(show_equation.length()-1);
                    if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
                    {
                        show_equation.deleteCharAt(show_equation.length()-1);
                        show_equation.append("+");
                    }
                    else
                        show_equation.append("+");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }
            }
        });
        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否输入了内容
                if(!(show_equation.toString().equals(""))&&!(show_equation.toString().equals("错误"))) {
                    signal=0;
                    char temp=show_equation.charAt(show_equation.length()-1);
                    if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
                    {
                        show_equation.deleteCharAt(show_equation.length()-1);
                        show_equation.append("-");
                    }
                    else
                        show_equation.append("-");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }
                else if(!(show_equation.toString().equals("错误"))){
                    signal=0;
                    show_equation.append("-");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }
            }
        });
 
        mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否输入了内容
                if(!(show_equation.toString().equals(""))&&!(show_equation.toString().equals("错误"))) {
                    signal=0;
                    char temp=show_equation.charAt(show_equation.length()-1);
                    if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
                    {
                        show_equation.deleteCharAt(show_equation.length()-1);
                        show_equation.append("*");
                    }
                    else
                        show_equation.append("*");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }
            }
        });
 
        div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否输入了内容
                if(!(show_equation.toString().equals(""))&&!(show_equation.toString().equals("错误"))) {
                    signal=0;
                    char temp=show_equation.charAt(show_equation.length()-1);
                    if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
                    {
                        show_equation.deleteCharAt(show_equation.length()-1);
                        show_equation.append("/");
                    }
                    else
                        show_equation.append("/");
                    result.setText(show_equation);
                    result.setSelection(result.getText().length());
                }
            }
        });
    }
    protected boolean operatorPriorityCompare(char operator1,char operator2)
    {
        int o1=0;
        int o2=0;
        switch (operator1){
            case '+':{o1=0;break;}
            case '-':{o1=0;break;}
            case '*':{o1=1;break;}
            case '/':{o1=1;break;}
        }
        switch (operator2){
            case '+':{o2=0;break;}
            case '-':{o2=0;break;}
            case '*':{o2=1;break;}
            case '/':{o2=1;break;}
        }
        if(o1<=o2)
        {
            return false;
        }
        else
            return true;
    }
    //相加
    public static Double Add(Double d1,Double d2) {
        if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
            return d1+d2;
        }
        if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
            //如果两个运算数只要有一个是非数'NaN',就直接运算即可
            return d1+d2;
        }
        //BigDecimal为精确计算的一个数据类型,你可以理解为使用它进行计算结果将更准确
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        //进行计算并将结果转为double返回
        return b1.add(b2).doubleValue();
    }
    //相减
    public static Double Sub(Double d1,Double d2){
        if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
            return d1-d2;
        }
        if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
            return d1-d2;
        }
        if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
            return d1*d2;
        }
        BigDecimal b1=new BigDecimal(Double.toString(d1));
        BigDecimal b2=new BigDecimal(Double.toString(d2));
        return b1.subtract(b2).doubleValue();
    }
    //相乘
    public static Double Mul(Double d1,Double d2){
        if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
            return d1*d2;
        }
        if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
            return d1*d2;
        }
        BigDecimal b1=new BigDecimal(Double.toString(d1));
        BigDecimal b2=new BigDecimal(Double.toString(d2));
        return b1.multiply(b2).setScale(8).doubleValue();
    }
    //相除
    public static Double Div(Double d1,Double d2){
        if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
            return d1/d2;
        }
        if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
            return d1/d2;
        }
        if(d1==0.0&&d2==0.0){
            return Double.NaN;
        }
        if(d2==0.0){
            return d1/d2;
        }
        BigDecimal b1=new BigDecimal(Double.toString(d1));
        BigDecimal b2=new BigDecimal(Double.toString(d2));
        return b1.divide(b2,8,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    //这个方法就是用来计算结果的,在344行被调用,参数为计算式
    //里面过程比较复杂,你需先弄懂后缀表达式,然后可对照理解大概流程意思即可
    protected String calculate(ArrayList equation){
        Double temp2;
        Double temp3;
        Double result;
        List operator=new ArrayList();
        List<Double> operand=new ArrayList();
        for(int i=0;i<equation.size();i++)
        {
            String temp4=(String) equation.get(i);
            if(temp4.equals("+")||temp4.equals("-")||temp4.equals("*")||temp4.equals("/"))
            {
                if(operator.size()>0)
                {
                    String temp5=operator.get(operator.size()-1).toString();
                    while(!(operatorPriorityCompare(temp4.charAt(0),temp5.charAt(0)))&&operator.size()>0)
                    {
                        operator.remove(operator.size()-1);
                        temp3=operand.get(operand.size()-1);
                        operand.remove(operand.size()-1);
                        temp2=operand.get(operand.size()-1);
                        operand.remove(operand.size()-1);
                        switch (temp5.charAt(0)){
                            case '+':{result=Add(temp2,temp3);operand.add(result);break;}
                            case '-':{result=Sub(temp2,temp3);operand.add(result);break;}
                            case '*':{result=Mul(temp2,temp3);operand.add(result);break;}
                            case '/':{result=Div(temp2,temp3);operand.add(result);break;}
                        }
                        if(operator.size()>0)
                        {
                            temp5=operator.get(operator.size()-1).toString();
                        }
                        else
                            break;
                    }
                    operator.add(temp4);
                }
                else
                    operator.add(temp4);
            }
            else if(temp4.equals("#"))
            {
                while(operator.size()>0)
                {
                    String temp6=(String)operator.get(operator.size()-1);
                    operator.remove(operator.size()-1);
                    temp3=operand.get(operand.size()-1);
                    operand.remove(operand.size()-1);
                    temp2=operand.get(operand.size()-1);
                    operand.remove(operand.size()-1);
                    switch (temp6.charAt(0)){
                        case '+':{result=Add(temp2,temp3);operand.add(result);break;}
                        case '-':{result=Sub(temp2,temp3);operand.add(result);break;}
                        case '*':{result=Mul(temp2,temp3);operand.add(result);break;}
                        case '/':{result=Div(temp2,temp3);operand.add(result);break;}
                    }
                }
            }
            else
            {
                if(temp4.equals("NaN")){
                    operand.add(Double.NaN);
                }else if(temp4.equals("∞")){
                    operand.add(Double.POSITIVE_INFINITY);
                }else if(temp4.equals(".")){
                    return "错误";
                }else{
                    operand.add(Double.parseDouble(temp4));
                }
            }
        }
        if(operand.get(0)==Double.NEGATIVE_INFINITY) return "-∞";
        if(operand.get(0)==Double.POSITIVE_INFINITY) return "∞";
        return operand.get(0).toString();
    }
    //当api最低版小于21时使用这个函数实现点击文本框不弹出键盘
    public void disableShowInput(EditText et) {
        Class<EditText> cls = EditText.class;
        Method method;
        try {
            method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(et, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

buttonstytle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 主体背景颜色值 -->
    <solid android:color="#666666" />
    <!-- 连框宽度和颜色值 -->
    <stroke
        android:width="0.01dp"
        android:color="#FFFFFF" />
</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <!--镶嵌一个标题栏-->
 
    <EditText
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="40sp" />
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
 
        <Button
            android:id="@+id/cls"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="C"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/div"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="/"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/mul"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="*"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/Backspace"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="Backspace"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
 
        <Button
            android:id="@+id/seven"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="7"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/eight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="8"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/nine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="9"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/sub"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="-"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
 
        <Button
            android:id="@+id/four"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="4"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/five"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="5"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/six"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="6"
            android:textColor="#ffffff"
            android:textSize="20sp" />
 
        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/buttonstytle"
            android:text="+"
            android:textColor="#ffffff"
            android:textSize="20sp" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal">
 
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_weight="1"
                android:orientation="horizontal">
 
                <Button
                    android:id="@+id/one"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/buttonstytle"
                    android:text="1"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
 
                <Button
                    android:id="@+id/two"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@drawable/buttonstytle"
                    android:text="2"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_weight="1">
 
                <Button
                    android:id="@+id/zero"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/buttonstytle"
                    android:text="0"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">
 
                <Button
                    android:id="@+id/three"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="@drawable/buttonstytle"
                    android:text="3"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
 
                <Button
                    android:id="@+id/spot"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_weight="1"
                    android:background="@drawable/buttonstytle"
                    android:text="."
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
            </LinearLayout>
 
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
 
                <Button
                    android:id="@+id/equal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/buttonstytle"
                    android:text="="
                    android:textColor="#ffffff"
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android Studio实现简单计算器开发

本文链接: https://www.lsjlt.com/news/149304.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android Studio实现简单计算器开发
    本文实例为大家分享了Android Studio实现简单计算器开的具体代码,供大家参考,具体内容如下 效果展示: 路径和文件: AndroidManifest.xml <...
    99+
    2022-11-13
  • Android Studio开发实现简单计算器功能
    本文实例为大家分享了Android Studio开发实现简单计算器的具体代码,供大家参考,具体内容如下 代码: activity_3.xml <xml version="1.0...
    99+
    2022-11-13
  • Android studio实现简单计算器
    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析 在Android studio中设计并实现一个简单的计算器,实现连...
    99+
    2022-06-06
    Android Studio studio Android
  • Android Studio实现简单计算器APP
    一、简介:用Android Studio 实现一个简单的计算器APP,并在蓝叠模拟器中运行。 该计算器只能实现两位数字的四则运算。 二、代码 activity_mai...
    99+
    2022-06-06
    Android Studio studio app Android
  • Android Studio实现简单计算器功能
    本文实例为大家分享了Android Studio实现简单计算器功能的具体代码,供大家参考,具体内容如下 程序步骤: (1)在布局文件定义一些计算器界面的文本框,按钮等组件。 (...
    99+
    2022-06-06
    Android Studio studio Android
  • Android studio实现简单计算器的编写
    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 话不多说,首先附上代码: MainActivity.java package co...
    99+
    2022-11-13
  • Android开发实现简单计算器功能
    计算器项目,要求实现加、减、乘、除、求倒数、求平方根等简单运算。 真机调试结果如下图: 布局文件:main_activity.xml <?xml version=...
    99+
    2022-11-12
  • Android开发简单计算器实现代码
    计算器项目,要求实现加、减、乘、除、求倒数、求平方根等简单运算。 真机调试结果如下图: 布局文件:main_activity.xml <?xml version=...
    99+
    2022-11-12
  • android studio实现简单的计算器小功能
    本文实例为大家分享了android studio实现简单计算器的具体代码,供大家参考,具体内容如下 布局: <xml version="1.0" encoding="utf-8...
    99+
    2022-11-13
  • Android studio开发实现计算器功能
    Android移动开发实现简单计算器功能,供大家参考,具体内容如下 前言 android 开发小实验android 移动开发实现 简易计算器功能小白也能轻松上手,复制粘贴就可使用 使...
    99+
    2022-11-13
  • Android Studio实现简易计算器设计
    本文实例为大家分享了Android Studio实现简易计算器的具体代码,供大家参考,具体内容如下 一、题目 1、如图所示(实际设计,类似此界面样式即可,全屏时,按钮将会纵向拉伸),...
    99+
    2022-11-13
  • android studio实现简易的计算器
    本文实例为大家分享了android studio实现简易计算器的具体代码,供大家参考,具体内容如下 先看效果图 基本功能:加,减,乘,除 核心代码实现 public class M...
    99+
    2022-11-13
  • android studio怎么实现简单的计算器小功能
    这篇文章主要介绍了android studio怎么实现简单的计算器小功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇android studio怎么实现简单的计算器小功能文章都会有所收获,...
    99+
    2023-06-30
  • Android实现简单计算器
    本文实例为大家分享了Android实现简单计算器的具体代码,供大家参考,具体内容如下 功能 1、加减乘除四则运算 2、归0 3、回退 4、即时运算 配置 在build.gradle...
    99+
    2022-11-12
  • android studio 项目 :UI设计高精度实现简单计算器
    UI设计: 实验目的: 自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局。实验要求: 1.完成一个计算器的设计,可以以手机自带的...
    99+
    2022-11-12
  • Android Studio实现简易计算器源码
    本文实例为大家分享了Android Studio实现简易计算器的具体代码,供大家参考,具体内容如下 效果图: 源码: 布局样式: <xml version="1.0" en...
    99+
    2022-11-13
  • Android studio开发怎么实现计算器功能
    这篇文章主要介绍“Android studio开发怎么实现计算器功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android studio开发怎么实现计算器功能”文章能帮助大...
    99+
    2023-06-30
  • Android Studio简易计算器
    目录 第一步,创建新项目 第二步,设计UI 第三步,实现计算逻辑 第四步,测试应用程序 随着移动互联网的普及,手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一,因此学习如何开发一款简易的计算器应用程序是...
    99+
    2023-09-17
    android studio android
  • 【Android Studio】简易计算器
    简易计算器要求: 1,操作简单,易于掌握,界面简单。 2.方便进行加,减,乘,除等操作。数字保留小数点后两位。 3.包含小数点运算和输入回退功能。 4.能够进行多次叠加运算。 5.系统能够进行多次叠加...
    99+
    2023-09-21
    android studio python android
  • Android Studio怎么实现简易计算器设计
    今天小编给大家分享一下Android Studio怎么实现简易计算器设计的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作