实现一个计算器的例子

Posted 夜月薇凉映银弩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现一个计算器的例子相关的知识,希望对你有一定的参考价值。

layout

absolute_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@drawable/shape_rectangle"
        android:text="C"
        android:textSize="30sp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@drawable/shape_rectangle"
        android:text="+/-"
        android:textSize="30sp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@drawable/shape_rectangle"
        android:text="%"
        android:textSize="30sp"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="@drawable/shape_rectangle_orange"
        android:text="÷"
        android:textSize="30sp"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="7"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="8"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="9"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle_orange"
            android:text="×"
            android:textSize="30sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="4"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="5"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="6"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle_orange"
            android:text="-"
            android:textSize="30sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="1"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle"
            android:text="2"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="3"
            android:background="@drawable/shape_rectangle"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/shape_rectangle_orange"
            android:text="+"
            android:textSize="30sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center_vertical"
            android:paddingLeft="40dp"
            android:text="0"
            android:background="@drawable/shape_rectangle"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="."
            android:background="@drawable/shape_rectangle"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="="
            android:background="@drawable/shape_rectangle"
            android:textSize="30sp"/>

    </LinearLayout>
</LinearLayout>

 

drawable

shape_recangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
       android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <stroke android:color="#333333" android:width="1dp"/>
</shape>

shape_recangle_orange.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
       android:shape="rectangle">
    <solid android:color="#e47f26"/>
    <stroke android:color="#333333" android:width="1dp"/>
</shape>

以上是关于实现一个计算器的例子的主要内容,如果未能解决你的问题,请参考以下文章

如何计算破折号媒体片段名称的 $Time$ 变量?

分享几个实用的代码片段(附代码例子)

分享几个实用的代码片段(附代码例子)

VS中添加自定义代码片段——偷懒小技巧

golang代码片段(摘抄)

计算片段着色器内平面的法线