Android 计算器

开发环境:android stdio 3.6.1

最终效果:

竖屏:

横屏:

完整项目:百度网盘链接
密码 v89n

主要代码:

MainActivity.java:

package com.example.experience_two;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.content.pm.PermissionGroupInfo;import android.icu.util.VersionInfo;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.util.TypedValue;import android.view.Gravity;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.GridLayout;import android.widget.TextView;import java.math.BigDecimal;import java.net.CacheRequest;import java.sql.BatchUpdateException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private static final String TAG = "MainActivity";    private GridLayout mGridLauout;    private TextView text,answer;    private int colCount,rowCount;    private int screenWidthDp,screenHeightDp,ScreenWidthPx,ScreenHeightPx,flag=0;    float density;    int fontMaxCount1=11;    float fontSize1=60;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        getSupportActionBar().hide();//去掉标题栏        mGridLauout = (GridLayout) findViewById(R.id.GridLayout1);        colCount = mGridLauout.getColumnCount();//按钮列数        rowCount = mGridLauout.getRowCount();//按钮行数        text=(TextView)findViewById(R.id.input_number);        answer=(TextView)findViewById(R.id.out_number);        text.setHorizontallyScrolling(true);//使text可以滑动        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics dm = new DisplayMetrics();        wm.getDefaultDisplay().getMetrics(dm);        ScreenWidthPx = dm.widthPixels;// 屏幕宽度(像素)        ScreenHeightPx= dm.heightPixels; // 屏幕高度(像素)        density = dm.density;//屏幕密度(0.75 / 1.0 / 1.5)        int densityDpi = dm.densityDpi;//屏幕密度dpi(120 / 160 / 240)        //屏幕宽度算法:屏幕宽度(像素)/屏幕密度        screenWidthDp = (int) (ScreenWidthPx/density);//屏幕宽度(dp)        screenHeightDp = (int)(ScreenHeightPx/density);//屏幕高度(dp)        //设置按钮在下半屏幕内均匀分布        for (int i = 0; i < mGridLauout.getChildCount(); i++) {            Button button = (Button) mGridLauout.getChildAt(i);            Log.e(TAG, "column:" + colCount + ";  screenwidth:" + ScreenWidthPx);            button.setWidth(ScreenWidthPx / colCount);            if(screenWidthDp<screenHeightDp){                flag=1;                button.setHeight(ScreenHeightPx/ 2 / rowCount);            }//竖屏时按钮部分占屏幕的二分是一            else{                button.setHeight(ScreenHeightPx/rowCount*2/3);            }//横屏时按钮部分占屏幕的三分之二            button.setOnClickListener(this);        }//为网格布局中的每个按钮注册监听器并动态设置大小        if(savedInstanceState!=null){            String s=savedInstanceState.getString("KEY");            String s1=savedInstanceState.getString("ANSWER");            text.setText(s);            answer.setText(s1);        }//恢复text和answer的数据    }    @Override    protected void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        outState.putString("KEY",text.getText().toString());        outState.putString("ANSWER",answer.getText().toString());    }//在旋转屏幕时保存text和answer中的数据    @Override    public void onClick(View view) {        answer=(TextView)findViewById(R.id.out_number);        String input=text.getText().toString();        Button button=(Button)findViewById(view.getId());        String btn=button.getText().toString();        if(btn.charAt(0)>='0'&&btn.charAt(0)<='9'){            if(input.equals("0")||input.matches("^.*[+−×÷]+0$")==true){                text.setText(input.substring(0,input.length()-1)+btn);            }//如果遇到以0打头的数字则先去掉0            else if(!input.matches("^.*[%)]$")){                text.setText(input+btn);            }//如果text中为合法的式子则在末尾追加数字            answer.setText(Calculator.Calculate(text.getText().toString()));//每次输入数字之后更新一次计算结果        }//数字按钮        else if(btn.matches("[+−×÷]+")&&!input.equals("")){            if(input.matches("^.*[+−×÷.]+$")){                text.setText(input.substring(0,text.length()-1)+btn);            }//如果以运算符结尾,则改变运算符            else{                text.setText(input+btn);            }//如果不以运算符结尾则追加运算符        }//普通运算符        else if(btn.equals("C")){            text.setText("");            answer.setText("");            fontMaxCount1=11;            fontSize1=60;//字符大小初始化            if(flag==1)            text.setTextSize(TypedValue.COMPLEX_UNIT_SP,fontSize1);        }//清0按钮        else if(!input.equals("")&&btn.equals("CE")){                String str=input.substring(0,input.length()-1);                if(str.equals("")){                    answer.setText("");                }                else if(str.charAt(str.length()-1)>='0'&&str.charAt(str.length()-1)<='9'){                    answer.setText(Calculator.Calculate(str));                }//每回退一次计算结果更新一次            text.setText(str);            if(flag==1&&fontMaxCount1>11){                fontMaxCount1-=1;            } else if(flag==1){                fontMaxCount1=11;                fontSize1=60;                text.setTextSize(TypedValue.COMPLEX_UNIT_SP,fontSize1);            }//根据字符串长度动态调整字体大小        }//退格按钮        else if(btn.equals("%")){            if(input.matches("^.*[0-9%]+$")){                text.setText(input+btn);                answer.setText(Calculator.Calculate(text.getText().toString()));            }//只有数字或百分号之后才可以接百分号        }//百分号        else if(btn.equals(".")){            if(!input.matches("^.*[0-9][.]+[0-9]+$|^.*[+−×÷.%)]$")){                text.setText(input+btn);            }//如果当前text最后一个数字含有小数点或以预算符结尾则加上小数点        }//小数点        else if(!input.equals("")&&btn.equals("+/−")){            if(input.matches("^.*\\(−[0-9.]+\\)$")){                String s1=input.substring(0,input.lastIndexOf("(−"))+input.substring(input.lastIndexOf("(−")+2,input.length()-1);                text.setText(s1);                answer.setText(Calculator.Calculate(s1));            }//复数变正数            else if(input.charAt(input.length()-1)>='0'&&input.charAt(input.length()-1)<='9'){                int i;                for (i = input.length()-1; i >= 0; i--) {                    if (!(input.charAt(i) == '.' || input.charAt(i) >= '0' && input.charAt(i) <= '9')) {                        break;                    }                }                int len=input.length();                String s1=input.substring(0, i +1),s2=input.substring(i + 1,len );                text.setText(s1+ "(−" + s2+ ")");                answer.setText(Calculator.Calculate(text.getText().toString()));            }//正数变复数        }//正负号转换        else if(btn.equals("=")){            fontMaxCount1=10;            fontSize1=60;            if(flag==1)            text.setTextSize(TypedValue.COMPLEX_UNIT_SP,fontSize1);                if(!answer.getText().toString().equals("")){                text.setText(answer.getText());            }//answer的结果转到text显示            answer.setText("");//answer清空        }//等号        else if(btn.equals("x²")){            if(!btn.matches("^.*[+−×÷]$")){                text.setText(input+"^2");                answer.setText(Calculator.Calculate(input+"^2"));            }        }//平方        else if(btn.equals("x³")){            if(!btn.matches("^.*[+−×÷]$")){                text.setText(input+"^3");                answer.setText(Calculator.Calculate(input+"^3"));            }        }        else if(btn.equals("yˣ")){            if(!btn.matches("^.*[+−×÷]$")){                text.setText(input+"^");            }        }        else if(btn.equals("10ˣ")){            if(!btn.matches("^.*[+−×÷]$")){                System.out.println(input+"10^");                text.setText(input+"10^");            }            else{                System.out.println(input+"×10^");                text.setText(input+"×10^");            }        }        else if(btn.equals("√")){                text.setText(input+"√");        }//开平方        else if(btn.equals("ˣ√y")){            text.setText(input+btn);        }        else if(btn.equals("ln")||btn.equals("lg")){            text.setText(input+btn+"(");        }        else if(btn.equals("Rad")){            button.setText("Deg");        }        else if(btn.equals("Deg")){            button.setText("Rad");        }        else if(btn.equals("x!")){            if(!btn.matches("^.*[+−×÷]$")){                text.setText(input+"!");                answer.setText(Calculator.Calculate(input+"!"));            }        }//阶乘        else if(btn.equals("EE")){            text.setText(input+"E");        }        else if(btn.equals("eˣ")){            text.setText(input+"e^");        }        else if(btn.equals("1/x")){            text.setText(input+"^-1");        }        else if(btn.equals("(")){            text.setText(input+btn);        }        else if(btn.equals(")")){            input=input+btn;            text.setText(input);            answer.setText(Calculator.Calculate(input));//输入右括号之后也要更新answer        }        else if(btn.equals("sin")||btn.equals("cos")||btn.equals("tan")||btn.equals("sinh")||btn.equals("cosh")||btn.equals("tanh")){            text.setText(input+btn+"(");        }//三角函数运算        else if(btn.equals("Rand")){            input=input+btn;            text.setText(input);            answer.setText(Calculator.Calculate(input));        }//随机数        else if(btn.equals("π")){            text.setText(input+Math.PI);        }        if(flag==1&&input.length()>fontMaxCount1){            fontMaxCount1=fontMaxCount1+1;            fontSize1=40;            text.setTextSize(TypedValue.COMPLEX_UNIT_SP,fontSize1);//根据字符串长度动态调整字符大小        }    }//根据按钮的内容设置不同的响应函数}

Calculator:

package com.example.experience_two;import android.support.v4.app.INotificationSideChannel;import java.util.Random;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Calculator {    public static String Calculate(String input){        while(input.matches("^.*\\(−?[0-9.]\\).*$")) {            String reg = "\\((−?[0-9.])\\)";            Pattern p = Pattern.compile(reg);            Matcher m = p.matcher(input);            while(m.find()) {                input=input.replace(m.group(0),m.group(1));            }        }        if(input.matches("^.*\\(.*\\).*$")){            String reg = "\\(([^()]*)\\)";//匹配最里边的括号            Pattern p = Pattern.compile(reg);            Matcher m = p.matcher(input);            while(m.find()) {                input=input.replace(m.group(0),Calculate(m.group(1)));            }        }        while(input.contains("!")){            String reg = "([0-9]+)!";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                int a=Integer.parseInt(str1);                double b=1;                for(int i=1;i<=a;i++){                    b=b*i;                }                input = input.replace(str, b + "");            }        }        while(input.contains("sinh")) {            System.out.println("sinh");            String reg = "sinh\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.sinh(a) + "");                }                else {                    input = input.replace(str,"×" + Math.sinh(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("cosh")) {            String reg = "cosh\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1,str1.length()));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.cosh(a) + "");                }                else {                    input = input.replace(str,"×" + Math.cosh(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("tanh")) {            System.out.println(input);            String reg = "tanh\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.tanh(a) + "");                }                else {                    input = input.replace(str,"×" + Math.tanh(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("sin")) {            String reg = "sin\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.sin(a) + "");                }                else {                    input = input.replace(str,"×" + Math.sin(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("cos")) {            String reg = "cos\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.cos(a) + "");                }                else {                    input = input.replace(str,"×" + Math.cos(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("tan")) {            System.out.println("tan");            String reg = "tan\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.tan(a) + "");                }                else {                    input = input.replace(str,"×" + Math.tan(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("ln")) {            String reg = "ln\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.log(a) + "");                }                else {                    input = input.replace(str,"×" + Math.log(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("lg")) {            String reg = "lg\\(?(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.log10(a) + "");                }                else {                    input = input.replace(str,"×" + Math.log10(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("e")) {            String reg = "e\\^(−?[0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                System.out.println(str1);                double a;                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1));                } else {                    a=Double.parseDouble(str1);                }                   b=Double.parseDouble(str2);                }                input=input.replace(m.group(1),Math.pow(a,b)+"");            }            input=input.replaceAll("-","−");        }        while(input.contains("√")) {            String reg = "√\\(?([0-9.]+)";            Pattern p=Pattern.compile(reg);            Matcher m=p.matcher(input);            while(m.find()) {                String str=m.group(0),str1=m.group(1);                double a;                a=Double.parseDouble(str1);                if(input.contains("+"+str)||input.contains("−"+str)||input.contains("×"+str)||input.contains("÷"+str)){                    input = input.replace(str, Math.sqrt(a) + "");                }                else {                    input = input.replace(str,"×" + Math.sqrt(a));                }            }            if(input.startsWith("×")){                input=input.substring(1);            }            input=input.replaceAll("-","−");        }        while(input.contains("%")){            String reg = "((\\d*\\.*\\d+)%)";            Pattern p=Pattern.compile(reg);            Matcher m = p.matcher(input);            while(m.find()){                double a=0.01*Double.parseDouble(m.group(2));                input=input.replace(m.group(1),a+"");            }        }        while(input.contains("×")||input.contains("÷")) {            String reg = "((−?\\d*\\.*\\d+)[×÷](−?\\d*\\.*\\d+))";            Pattern p = Pattern.compile(reg);            Matcher m = p.matcher(input);            while (m.find()){                double a,b;                String str=m.group(0),str1=m.group(2).toString(),str2=m.group(3).toString();                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1,str1.length()));                } else {                    a=Double.parseDouble(str1);                }                if(str2.contains("−")){                    b=-Double.parseDouble(str2.substring(1,str2.length()));                } else {                    b=Double.parseDouble(str2);                }                if (str.contains("×")){                    input=input.replace(str,a*b+"");                } else {                    if(Math.abs(b)<1e-5) {return "error";}                    input=input.replace(str,a/b+"");                }            }        }        if(input.contains("E")){            input="error";        }        input=input.replace("-","−");        while(input.contains("+")||input.contains("−")){            if(input.matches("^−\\d*\\.*\\d+$")) {                break;            };            String reg = "((−?\\d*\\.*\\d+)[+−](−?\\d*\\.*\\d+))";            Pattern p = Pattern.compile(reg);            Matcher m = p.matcher(input);            while (m.find()){                double a,b;                String str=m.group(0),str1=m.group(2).toString(),str2=m.group(3).toString();                if(str1.contains("−")){                    a=-Double.parseDouble(str1.substring(1,str1.length()));                } else {                    a=Double.parseDouble(str1);                }                if(str2.contains("−")){                    b=-Double.parseDouble(str2.substring(1,str2.length()));                } else {                    b=Double.parseDouble(str2);                }                if (str.contains("+")){                    input=input.replace(str,a+b+"");                } else {                    input=input.replace(str,a-b+"");                }            }            input=input.replaceAll("-","−");        }        if(!input.matches("−?\\d*\\.*\\d+")){            input="error!";        }        if(input.matches("^.*\\.0$")){            int len=input.length();            input=input.substring(0,len-2);        }        return input;    }}

竖屏布局xml文件:

<?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:ignore="NewApi"    android:theme="@android:style/Theme.Black.NoTitleBar"    tools:context=".MainActivity">    <TextView        android:id="@+id/input_number"        android:layout_width="match_parent"        android:layout_height="70dp"        android:clickable="true"        android:gravity="right"        android:textColor="@color/button_one"        android:textSize="60sp"        app:layout_constraintBottom_toTopOf="@+id/out_number"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <TextView        android:id="@+id/out_number"        android:layout_width="match_parent"        android:layout_height="50dp"        android:clickable="true"        android:gravity="right"        android:textColor="#c0c0c0"        android:textSize="40sp"        app:layout_constraintBottom_toTopOf="@+id/GridLayout1"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.662" />    <GridLayout        android:id="@+id/GridLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:columnCount="4"        android:rowCount="5"        app:layout_constraintBottom_toBottomOf="parent">        <Button            android:id="@+id/number1"            style="@style/btn_nomal_style"            android:layout_row="3"            android:layout_column="0"            android:text="1" />        <Button            android:id="@+id/number2"            style="@style/btn_nomal_style"            android:layout_row="3"            android:layout_column="1"            android:text="2" />        <Button            android:id="@+id/number3"            style="@style/btn_nomal_style"            android:layout_row="3"            android:layout_column="2"            android:text="3" />        <Button            android:id="@+id/number4"            style="@style/btn_nomal_style"            android:layout_row="2"            android:layout_column="0"            android:text="4" />        <Button            android:id="@+id/number5"            style="@style/btn_nomal_style"            android:layout_row="2"            android:layout_column="1"            android:text="5" />        <Button            android:id="@+id/number6"            style="@style/btn_nomal_style"            android:layout_row="2"            android:layout_column="2"            android:text="6" />        <Button            android:id="@+id/number7"            style="@style/btn_nomal_style"            android:layout_row="1"            android:layout_column="0"            android:text="7" />        <Button            android:id="@+id/number8"            style="@style/btn_nomal_style"            android:layout_row="1"            android:layout_column="1"            android:text="8" />        <Button            android:id="@+id/number9"            style="@style/btn_nomal_style"            android:layout_row="1"            android:layout_column="2"            android:text="9"            />        <Button            android:id="@+id/number0"            style="@style/btn_nomal_style"            android:layout_row="4"            android:layout_column="1"            android:text="0" />        <Button            android:id="@+id/clear"            style="@style/btn_nomal_style"            android:layout_row="0"            android:layout_column="0"            android:text="C"            android:textColor="@color/button_one" />        <Button            android:id="@+id/back"            style="@style/btn_nomal_style"            android:layout_row="0"            android:layout_column="1"            android:text="CE"            android:textColor="@color/button_one" />        <Button            android:id="@+id/add"            style="@style/btn_nomal_style"            android:layout_row="3"            android:layout_column="3"            android:text="+"            android:textColor="@color/button_one" />        <Button            android:id="@+id/min"            style="@style/btn_nomal_style"            android:layout_row="2"            android:layout_column="3"            android:text="−"            android:textColor="@color/button_one" />        <Button            android:id="@+id/mul"            style="@style/btn_nomal_style"            android:layout_row="1"            android:layout_column="3"            android:text="×"            android:textColor="@color/button_one" />        <Button            android:id="@+id/div"            style="@style/btn_nomal_style"            android:layout_row="0"            android:layout_column="3"            android:text="÷"            android:textColor="@color/button_one" />        <Button            android:id="@+id/dot"            style="@style/btn_nomal_style"            android:layout_row="4"            android:layout_column="2"            android:text="." />        <Button            android:id="@+id/equal"            style="@style/btn_nomal_style"            android:layout_row="4"            android:layout_column="3"            android:text="="            android:textColor="@color/button_one" />        <Button            android:id="@+id/symbol"            style="@style/btn_nomal_style"            android:layout_row="0"            android:layout_column="2"            android:text="+/−"            android:textColor="@color/button_one" />        <Button            android:id="@+id/percent"            style="@style/btn_nomal_style"            android:layout_row="4"            android:layout_column="0"            android:text="%" />    </GridLayout></androidx.constraintlayout.widget.ConstraintLayout>

横屏布局xml文件:

<?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:ignore="NewApi"    android:theme="@android:style/Theme.Black.NoTitleBar"    tools:context=".MainActivity">    <TextView        android:id="@+id/input_number"        android:layout_width="match_parent"        android:layout_height="57sp"        android:clickable="true"        android:gravity="right"        android:textColor="@color/button_one"        android:textSize="45sp"        app:layout_constraintBottom_toTopOf="@+id/out_number"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <TextView        android:id="@+id/out_number"        android:layout_width="match_parent"        android:layout_height="25sp"        android:clickable="true"        android:gravity="right"        android:textColor="#c0c0c0"        android:textSize="20sp"        app:layout_constraintBottom_toTopOf="@+id/GridLayout1"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.90" />    <GridLayout        android:id="@+id/GridLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:columnCount="8"        android:rowCount="6"        app:layout_constraintBottom_toBottomOf="parent">        <Button            android:id="@+id/mc"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="4"            android:text="mc" />        <Button            android:id="@+id/madd"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="5"            android:text="m+" />        <Button            android:id="@+id/mmin"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="6"            android:text="m−" />        <Button            android:id="@+id/tenX"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="3"            android:text="10ˣ" />        <Button            android:id="@+id/yX"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="3"            android:text="yˣ" />        <Button            android:id="@+id/lg"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="3"            android:text="lg" />        <Button            android:id="@+id/ln"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="3"            android:text="ln" />        <Button            android:id="@+id/eX"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="3"            android:text="eˣ" />        <Button            android:id="@+id/right_bracket"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="2"            android:text=")" />        <Button            android:id="@+id/left_bracket"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="1"            android:text="(" />        <Button            android:id="@+id/twoND"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="0"            android:text="2nd" />        <Button            android:id="@+id/reciprocal"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="0"            android:text="1/x" />        <Button            android:id="@+id/factorial"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="0"            android:text="x!" />        <Button            android:id="@+id/sin"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="0"            android:text="sin" />        <Button            android:id="@+id/sinh"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="0"            android:text="sinh" />        <Button            android:id="@+id/rad"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="0"            android:text="Rad" />        <Button            android:id="@+id/square"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="1"            android:text="x²" />        <Button            android:id="@+id/sqrt"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="1"            android:text="√" />        <Button            android:id="@+id/cos"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="1"            android:text="cos" />        <Button            android:id="@+id/cosh"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="1"            android:text="cosh" />        <Button            android:id="@+id/pai"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="1"            android:text="π" />        <Button            android:id="@+id/cubic"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="2"            android:text="x³" />        <Button            android:id="@+id/Xy"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="2"            android:text="ˣ√y" />        <Button            android:id="@+id/tan"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="2"            android:text="tan" />        <Button            android:id="@+id/tanh"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="2"            android:text="tanh" />        <Button            android:id="@+id/EE"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="2"            android:text="EE" />        <Button            android:id="@+id/rand"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="3"            android:text="Rand" />        <Button            android:id="@+id/mmul"            style="@style/btn_nomal_style_land"            android:layout_row="0"            android:layout_column="7"            android:text="mr" />        <Button            android:id="@+id/number1"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="4"            android:text="1" />        <Button            android:id="@+id/number2"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="5"            android:text="2" />        <Button            android:id="@+id/number3"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="6"            android:text="3" />        <Button            android:id="@+id/number4"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="4"            android:text="4" />        <Button            android:id="@+id/number5"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="5"            android:text="5" />        <Button            android:id="@+id/number6"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="6"            android:text="6" />        <Button            android:id="@+id/number7"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="4"            android:text="7" />        <Button            android:id="@+id/number8"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="5"            android:text="8" />        <Button            android:id="@+id/number9"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="6"            android:text="9"            />        <Button            android:id="@+id/number0"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="5"            android:text="0" />        <Button            android:id="@+id/clear"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="4"            android:text="C"            android:textColor="@color/button_one" />        <Button            android:id="@+id/back"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="5"            android:text="CE"            android:textColor="@color/button_one" />        <Button            android:id="@+id/add"            style="@style/btn_nomal_style_land"            android:layout_row="4"            android:layout_column="7"            android:text="+"            android:textColor="@color/button_one" />        <Button            android:id="@+id/min"            style="@style/btn_nomal_style_land"            android:layout_row="3"            android:layout_column="7"            android:text="−"            android:textColor="@color/button_one" />        <Button            android:id="@+id/mul"            style="@style/btn_nomal_style_land"            android:layout_row="2"            android:layout_column="7"            android:text="×"            android:textColor="@color/button_one" />        <Button            android:id="@+id/div"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="7"            android:text="÷"            android:textColor="@color/button_one" />        <Button            android:id="@+id/dot"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="6"            android:text="." />        <Button            android:id="@+id/equal"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="7"            android:text="="            android:textColor="@color/button_one" />        <Button            android:id="@+id/symbol"            style="@style/btn_nomal_style_land"            android:layout_row="1"            android:layout_column="6"            android:text="+/−"            android:textColor="@color/button_one" />        <Button            android:id="@+id/percent"            style="@style/btn_nomal_style_land"            android:layout_row="5"            android:layout_column="4"            android:text="%" />    </GridLayout></androidx.constraintlayout.widget.ConstraintLayout>

更多相关文章

  1. [置顶] Android多分辨率和多屏幕的布局适配详解
  2. 用TextView自定义按钮
  3. Android(安卓)屏幕滑动事件
  4. android 自定义view--点击屏幕生成圆点,点击圆点可以拖动
  5. Android(安卓)屏幕滑动事件
  6. android 设置 dialog位置
  7. 详解Android中的屏幕方向
  8. Android(安卓)各种基础控件布局
  9. 详解Android中的屏幕方向

随机推荐

  1. Android中对Log日志文件的分析
  2. 想抢先体验Android操作系统的魅力吗?那就
  3. Android通过Socket(TCP/IP)与PC通讯
  4. 环境装好后,创建手机的模拟器
  5. 新手学习linux需了解的内容
  6. Android(安卓)2.3系统令人为之兴奋的十大
  7. Android开发实践:在任意目录执行NDK编译
  8. Android(安卓)开发艺术探索读书笔记 11 -
  9. 转!Android(安卓)onTouchEvent,onClick和o
  10. Android之Glide(非常好用的图片加载框架)