我正在为 Android 创建一个单位转换器,它使用按钮(选择单位类型)和微调器(选择单位),但我遇到了问题。可能的方程太多了。如果每个微调器中有十个单位用于所有单位类型,那么您最终会拥有数百种不同的方程式可能性。有谁知道如何制作更短的代码?
这是我的微调器字符串数组:
<string-array name="speed">
<item>Centimeters/Second</item>
<item>Meters/Second</item>
<item>Feet/Second</item>
<item>Feet/Minute</item>
<item>Miles/Hour</item>
<item>Kilometers/Hour</item>
<item>Furlongs/Minute</item>
<item>Knots</item>
<item>Leagues/Day</item>
<item>Mach</item>
</string-array>
<string-array name="distance">
<item>Meter</item>
<item>Foot</item>
<item>Centimeter</item>
<item>Millimeter</item>
<item>Micrometer</item>
<item>Nanometer</item>
<item>Mile</item>
<item>Yard</item>
<item>Inch</item>
<item>Light Year</item>
</string-array>
<string-array name="temperature">
<item>Celsius</item>
<item>Kelvin</item>
<item>Fahrenheit</item>
</string-array>
<string-array name="area">
<item>Square Meter</item>
<item>Square Kilometer</item>
<item>Square Centimeter</item>
<item>Square Millimeter</item>
<item>Square Micrometer</item>
<item>Hectare</item>
<item>Square Mile</item>
<item>Square Yard</item>
<item>Square Foot</item>
<item>Square Inch</item>
<item>Acre</item>
</string-array>
<string-array name="volume">
<item>Cubic Meter</item>
<item>Cubic Kilometer</item>
<item>Cubic Centimeter</item>
<item>Cubic Millimeter</item>
<item>Liter</item>
<item>Milliliter</item>
<item>US Gallon</item>
<item>US Quart</item>
<item>US Pint</item>
<item>US Cup</item>
<item>US Fluid Ounce</item>
<item>US Table Spoon</item>
<item>US Tea Spoon</item>
<item>Imperial Gallon</item>
<item>Imperial Quart</item>
<item>Imperial Pint</item>
<item>Imperial Fluid Ounce</item>
<item>Imperial Table Spoon</item>
<item>Imperial Tea Spoon</item>
<item>Cubic Mile</item>
<item>Cubic Foot</item>
<item>Cubic Inch</item>
<item>Cubic Yard</item>
</string-array>
<string-array name="mass">
<item>Kilogram</item>
<item>Gram</item>
<item>Milligram</item>
<item>Metric Ton</item>
<item>Long Ton</item>
<item>Short Ton</item>
<item>Pound</item>
<item>Ounce</item>
<item>Carat</item>
<item>Atomic Mass Unit</item>
<item>Stone</item>
</string-array>
<string-array name="time">
<item>Second</item>
<item>Millisecond</item>
<item>Microsecond</item>
<item>Nanosecond</item>
<item>Picoseconds</item>
<item>Minute</item>
<item>Hour</item>
<item>Day</item>
<item>Week</item>
<item>Month</item>
<item>Year</item>
</string-array>
<string-array name="energy">
<item>Joule</item>
<item>Gram Calorie</item>
<item>KiloJoule</item>
<item>Foot-Pound</item>
<item>Kilocalorie</item>
<item>Watt Hour</item>
<item>Electronvolt</item>
<item>BTU</item>
<item>US Therm</item>
</string-array>
这是我的转换器 xml:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="ca.software.appart.calcu_everything.Converter">
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_alignTop="@+id/spinner2"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/what"
android:backgroundTint="#2125ff"/>
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_above="@+id/calculate"
android:layout_alignEnd="@+id/linearLayout"
android:layout_toEndOf="@+id/what"
android:backgroundTint="#2125ff"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear1">
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/speed"
android:textAllCaps="false"
android:id="@+id/speed" />
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/mass"
android:textAllCaps="false"
android:id="@+id/mass" />
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/area"
android:textAllCaps="false"
android:id="@+id/area" />
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/temp"
android:textAllCaps="false"
android:id="@+id/temp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linear1"
android:id="@+id/linearLayout">
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/energy"
android:textAllCaps="false"
android:id="@+id/energy" />
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/volume"
android:textAllCaps="false"
android:id="@+id/volume" />
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/distance"
android:textAllCaps="false"
android:id="@+id/distance" />
<Button
android:background="@drawable/calculator_buttons"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/time"
android:textAllCaps="false"
android:id="@+id/time" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calculate"
android:id="@+id/calculate"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@drawable/science_buttons"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="1"
android:id="@+id/what"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="1"
android:id="@+id/answer"
android:layout_below="@+id/calculate"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/calculate"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />