文章目录

  • 线性布局
  • 横屏界面实现
  • 横竖屏切换数值的简单保存
  • 计算器界面的实现


线性布局

  • 当布局方向为水平方向时:
    width设置为0dp,权重weight才会按比例分配。
  • 当布局方向为竖直方向时:
    height设置为0dp,权重weight才会按比例分配。

例子:

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

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent" android:orientation="vertical">
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp" android:layout_weight="1"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp" android:layout_weight="2"/>
       <Button
           android:layout_width="match_parent"
           android:layout_height="0dp" android:layout_weight="3"/>
   </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

android 计算器表格布局 安卓计算器怎么设置_xml

横屏界面实现

方式一:直接创建一个land.xml配置文件

android 计算器表格布局 安卓计算器怎么设置_android 计算器表格布局_02


android 计算器表格布局 安卓计算器怎么设置_android 计算器表格布局_03


方式二:在layout下,建立两个布局文件,一个是横屏的一个是竖屏的(名称任意),然后再java文件中布置这两个文件就行

android 计算器表格布局 安卓计算器怎么设置_4s_04

横竖屏切换数值的简单保存

onSaveInstanceState方法会在什么时候被执行,有这么几种情况:

1、当用户按下HOME键时。

这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则

2、长按HOME键,选择运行其他的程序时。

3、按下电源按键(关闭屏幕显示)时。

4、从activity A中启动一个新的activity时。

5、屏幕方向切换时,例如从竖屏切换到横屏时。

在屏幕切换之前,系统会销毁activity A,在屏幕切换之后系统又会自动地创建activity A,所以onSaveInstanceState一定会被执行

总而言之,onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据(当然你不保存那就随便你了)

public class MainActivity extends AppCompatActivity {
    Button bt1,bt2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = findViewById(R.id.onebutton);
        bt2 = findViewById(R.id.twobutton);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bt1.setText(bt2.getText().toString());
            }
        });
        if(savedInstanceState!=null){
            String s = savedInstanceState.getString("KEY");
            bt1.setText(s);
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("KEY",bt1.getText().toString());
    }
}

计算器界面的实现

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

<!--竖屏一个EditText,4*6个键实现计算器-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.82"
        android:background="@null"
        android:gravity="right|bottom"
        android:text=""
        android:textSize="30sp"
        android:id="@+id/editText"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" android:layout_weight="6.18" android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical">

            <Button
                android:id="@+id/bmc"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="mc"
                android:textAllCaps="false"
                android:textSize="24sp" />

            <Button
                android:id="@+id/bac"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="AC"
                android:textAllCaps="false"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="7"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="4"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="1"
                android:textSize="24sp" />

            <Button
                android:id="@+id/bpercent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="%"
                android:textSize="24sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical">

            <Button
                android:id="@+id/bmadd"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="m+"
                android:textAllCaps="false"
                android:textSize="24sp" />

            <Button
                android:id="@+id/bmul"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="*"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="8"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="5"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="2"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b0"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="0"
                android:textSize="24sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical">

            <Button
                android:id="@+id/bmsub"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="m-"
                android:textAllCaps="false"
                android:textSize="24sp" />

            <Button
                android:id="@+id/bdiv"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="/"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="9"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="6"
                android:textSize="24sp" />

            <Button
                android:id="@+id/b3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="3"
                android:textSize="24sp" />

            <Button
                android:id="@+id/bpoint"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff"
                android:text="."
                android:textSize="24sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical">

            <Button
                android:id="@+id/bmr"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="mr"
                android:textAllCaps="false"
                android:textSize="24sp" />

            <Button
                android:id="@+id/bdel"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="DEL"
                android:textAllCaps="false"
                android:textSize="24sp" />

            <Button
                android:id="@+id/badd"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="+"
                android:textSize="24sp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#E8E8E8"
                android:text="-"
                android:textSize="24dp" android:id="@+id/bsub"/>

            <Button
                android:id="@+id/bequal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="#03A9F4"
                android:text="="
                android:textColor="#FFFFFF"
                android:textSize="24sp" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

界面如下:

android 计算器表格布局 安卓计算器怎么设置_4s_05


简单计算代码实现:

package com.example.cal;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    EditText input;
    //mc,m+,m-,mr
    Button bmC,bmAdd,bmSub,bmR;
    //+,-,*,/
    Button bAdd,bSub,bMul,bDiv;
    //=,%,.,ac,del
    Button bEqual,bPercent,bPoint,bAc,bDel;
    //0-9
    Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        input = findViewById(R.id.editText);
        input.setOnClickListener(this);
        
        bmC = findViewById(R.id.bmc);
        bmC.setOnClickListener(this);
        bmAdd = findViewById(R.id.bmadd);
        bmAdd.setOnClickListener(this);
        bmSub = findViewById(R.id.bmsub);
        bmSub.setOnClickListener(this);
        bmR = findViewById(R.id.bmr);
        bmR.setOnClickListener(this);

        bAdd = findViewById(R.id.badd);
        bAdd.setOnClickListener(this);
        bSub= findViewById(R.id.bsub);
        bSub.setOnClickListener(this);
        bMul= findViewById(R.id.bmul);
        bMul.setOnClickListener(this);
        bDiv = findViewById(R.id.bdiv);
        bDiv.setOnClickListener(this);

        bEqual = findViewById(R.id.bequal);
        bEqual.setOnClickListener(this);
        bPercent = findViewById(R.id.bpercent);
        bPercent.setOnClickListener(this);
        bPoint = findViewById(R.id.bpoint);
        bPoint.setOnClickListener(this);
        bAc = findViewById(R.id.bac);
        bAc.setOnClickListener(this);
        bDel = findViewById(R.id.bdel);
        bDel.setOnClickListener(this);

        b0 = findViewById(R.id.b0);
        b0.setOnClickListener(this);
        b1 = findViewById(R.id.b1);
        b1.setOnClickListener(this);
        b2 = findViewById(R.id.b2);
        b2.setOnClickListener(this);
        b3 = findViewById(R.id.b3);
        b3.setOnClickListener(this);
        b4 = findViewById(R.id.b4);
        b4.setOnClickListener(this);
        
        b5 = findViewById(R.id.b5);
        b5.setOnClickListener(this);
        b6 = findViewById(R.id.b6);
        b6.setOnClickListener(this);
        b7 = findViewById(R.id.b7);
        b7.setOnClickListener(this);
        b8 = findViewById(R.id.b8);
        b8.setOnClickListener(this);
        b9 = findViewById(R.id.b9);
        b9.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String temp = input.getText().toString();
        switch (view.getId()){
            case R.id.bac:
                input.setText("0");
                break;
            case R.id.bdel:
                if(temp.length()<=1){
                    input.setText("0");
                }else {
                    input.setText(temp.substring(0,temp.length()-1));
                }
                break;
            case R.id.bpercent:
            case R.id.badd:
            case R.id.bsub:
            case R.id.bmul:
            case R.id.bdiv:
                input.setText(temp+" "+((Button)view).getText().toString()+" ");
                break;
            case R.id.b0:
            case R.id.b1:
            case R.id.b2:
            case R.id.b3:
            case R.id.b4:
            case R.id.b5:
            case R.id.b6:
            case R.id.b7:
            case R.id.b8:
            case R.id.b9:
            case R.id.bpoint:
                input.setText(temp+((Button)view).getText().toString());
                break;
            case R.id.bequal:
                getResult();
                break;
        }
    }

    private void getResult() {
        String temp = input.getText().toString();
        if(TextUtils.isEmpty(temp)){
            return;
        }
        double one=Double.parseDouble(temp.substring(0,temp.indexOf(" ")));
        String fuHao=temp.substring(temp.indexOf(" ")+1,temp.indexOf(" ")+2);
        double two = 0;
        if(!"%".equals(fuHao)) {
            two = Double.parseDouble(temp.substring(temp.indexOf(" ") + 3, temp.length()));
        }
        double result = 0;
        if("+".equals(fuHao)){
            result=one+two;
        }else if("-".equals(fuHao)){
            result=one-two;
        }else if("*".equals(fuHao)){
            result=one*two;
        }else if("/".equals(fuHao)){
            if(two==0) return;
            result=one/two;
        }else if("%".equals(fuHao)){
            result=one/100;
        }
        input.setText(String.valueOf(result));
    }
}