如何使用 Android Studio 创建简单的计算器 Android 应用
已发表: 2017-07-06在我之前的文章中,我写了关于如何创建一个简单的 android 应用程序的详细步骤。 在那个特定的应用程序中,我还解释了 android 按钮的概念和 android 的基本概念。
你可以在 Android 部分找到我所有的其他文章。
在本文中,我们将创建一个calculator android app
。 这是一个功能有限的简单计算器。
在我们继续之前,最好回顾一下完整的 HelloWorld 教程。 这里再次链接:我的第一个 HelloWorld Android 应用程序
- 如何构建一个简单的计算器应用程序 - 完整教程
- 使用 Android Studio 构建一个简单的计算器
- Android 开发:创建基本计算器
- 创建简单的计算器 Android 应用
- 如何为 Android 创建一个计算器应用程序
让我们开始使用我们的计算器安卓应用程序:
第1步
- 打开你的 Android Studio
- 单击启动一个新的 Android Studio 项目。
- 提供您的应用程序名称
CrunchifyCalculator
并将其他字段留空,然后单击下一步。
第2步
- 选择最低 SDK
API 15: Android 4.0.3(IceCreamSandwich)
。 我选择了 API 15 (IceCreamSandwich),因为它覆盖了几乎 94% 的设备并且几乎具有所有功能。 如果您想覆盖 100% 设备,那么您可以选择 API 8:Android 2.2(Froyo)。
第三步
- 选择
Empty Activity
并单击下一步。 - 保持活动名称
MainActivity
,并保持一切不变。 单击完成。
第四步
- 单击完成后,构建 Activity 和文件大约需要 2 分钟。
- 这是您的应用程序的最终项目结构。
第 5 步
- 现在我们必须在 MainActivity.java 文件中添加我们的 Java 代码。
- 因此,从 IDE 左侧打开
MainActivity.java
文件(app -> java -> com.crunchify.tutorials.crunchifycalculator -> MainActivity.java)
您可以在代码下方找到突出显示的行的说明。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
package com . crunchify . tutorials . crunchifycalculator ; import android . os . Bundle ; import android . support . v7 . app . AppCompatActivity ; import android . view . View ; import android . widget . Button ; import android . widget . EditText ; public class MainActivity extends AppCompatActivity { Button button0 , button1 , button2 , button3 , button4 , button5 , button6 , button7 , button8 , button9 , buttonAdd , buttonSub , buttonDivision , buttonMul , button10 , buttonC , buttonEqual ; EditText crunchifyEditText ; float mValueOne , mValueTwo ; boolean crunchifyAddition , mSubtract , crunchifyMultiplication , crunchifyDivision ; @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . activity_main ) ; button0 = ( Button ) findViewById ( R . id . button0 ) ; button1 = ( Button ) findViewById ( R . id . button1 ) ; button2 = ( Button ) findViewById ( R . id . button2 ) ; button3 = ( Button ) findViewById ( R . id . button3 ) ; button4 = ( Button ) findViewById ( R . id . button4 ) ; button5 = ( Button ) findViewById ( R . id . button5 ) ; button6 = ( Button ) findViewById ( R . id . button6 ) ; button7 = ( Button ) findViewById ( R . id . button7 ) ; button8 = ( Button ) findViewById ( R . id . button8 ) ; button9 = ( Button ) findViewById ( R . id . button9 ) ; button10 = ( Button ) findViewById ( R . id . button10 ) ; buttonAdd = ( Button ) findViewById ( R . id . buttonadd ) ; buttonSub = ( Button ) findViewById ( R . id . buttonsub ) ; buttonMul = ( Button ) findViewById ( R . id . buttonmul ) ; buttonDivision = ( Button ) findViewById ( R . id . buttondiv ) ; buttonC = ( Button ) findViewById ( R . id . buttonC ) ; buttonEqual = ( Button ) findViewById ( R . id . buttoneql ) ; crunchifyEditText = ( EditText ) findViewById ( R . id . edt1 ) ; button1 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "1" ) ; } } ) ; button2 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "2" ) ; } } ) ; button3 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "3" ) ; } } ) ; button4 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "4" ) ; } } ) ; button5 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "5" ) ; } } ) ; button6 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "6" ) ; } } ) ; button7 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "7" ) ; } } ) ; button8 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "8" ) ; } } ) ; button9 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "9" ) ; } } ) ; button0 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "0" ) ; } } ) ; buttonAdd . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { if ( crunchifyEditText == null ) { crunchifyEditText . setText ( "" ) ; } else { mValueOne = Float . parseFloat ( crunchifyEditText . getText ( ) + "" ) ; crunchifyAddition = true ; crunchifyEditText . setText ( null ) ; } } } ) ; buttonSub . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { mValueOne = Float . parseFloat ( crunchifyEditText . getText ( ) + "" ) ; mSubtract = true ; crunchifyEditText . setText ( null ) ; } } ) ; buttonMul . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { mValueOne = Float . parseFloat ( crunchifyEditText . getText ( ) + "" ) ; crunchifyMultiplication = true ; crunchifyEditText . setText ( null ) ; } } ) ; buttonDivision . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { mValueOne = Float . parseFloat ( crunchifyEditText . getText ( ) + "" ) ; crunchifyDivision = true ; crunchifyEditText . setText ( null ) ; } } ) ; buttonEqual . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { mValueTwo = Float . parseFloat ( crunchifyEditText . getText ( ) + "" ) ; if ( crunchifyAddition == true ) { crunchifyEditText . setText ( mValueOne + mValueTwo + "" ) ; crunchifyAddition = false ; } if ( mSubtract == true ) { crunchifyEditText . setText ( mValueOne - mValueTwo + "" ) ; mSubtract = false ; } if ( crunchifyMultiplication == true ) { crunchifyEditText . setText ( mValueOne * mValueTwo + "" ) ; crunchifyMultiplication = false ; } if ( crunchifyDivision == true ) { crunchifyEditText . setText ( mValueOne / mValueTwo + "" ) ; crunchifyDivision = false ; } } } ) ; buttonC . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( "" ) ; } } ) ; button10 . setOnClickListener ( new View . OnClickListener ( ) { @ Override public void onClick ( View v ) { crunchifyEditText . setText ( crunchifyEditText . getText ( ) + "." ) ; } } ) ; } } |
这里我们有 1 个 EditText。 它定义了内容的类型。
让我们更多地了解代码。

- 第 11 - 14 行:这里我们创建了 Buttons 和 EditText 的引用。
- 第 16 行:这里我们为 value1 和 value2 创建了两个浮点变量。
- 第 21 行:我们重写了 onCreate() 方法,它是 Activity 类的方法。
- 第 45 – 50 行:我们在 Button1 上设置了 onClickListener。 如果我们单击 Button1,将显示 EditText。
- 我们为每个按钮实现了相同的逻辑。
- 第 115 – 127 行:这里我们在添加按钮上设置了点击监听器。
- 在这里,我们将条件设置为,如果我们 EditText 为 Null,那么我们将 EditText 设置为空值。 否则,我们添加在单击添加按钮之前和单击添加按钮之后单击的两个值。
- 我们还将
crunchifyAddition
布尔值设置为 True。 此 true 表示单击了添加按钮,并且将在用户单击“=”按钮时使用。 - 我们为其他按钮实现相同的逻辑,例如 buttonSub、ButtonMul、buttonDivision。
- 第 156 – 183 行:这里我们在“=”按钮上设置 clickListener。 在这里,我们将条件设置为,如果用户单击添加按钮,则
crunchifyAddition
值在添加按钮的单击侦听器上设置为 True。 - 据此,将针对单击的按钮执行相应的操作。
1 2 3 4 |
if ( crunchifyAddition == true ) { crunchifyEditText . setText ( mValueOne + mValueTwo + "" ) ; crunchifyAddition = false ; } |
如果在“=”按钮之前单击添加按钮,则将执行添加操作,如上所示。
- 执行完动作后,我们将
crunchifyAddition
的值设置为 false,这样我们就可以再次执行 Add 动作。
下面是布局文件,帮助设计计算器的前端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns : android = "http://schemas.android.com/apk/res/android" xmlns : tools = "http://schemas.android.com/tools" android : id = "@+id/relative1" android : layout_width = "match_parent" android : layout_height = "match_parent" tools : context = ".MainActivity" > < EditText android : id = "@+id/edt1" android : layout_width = "match_parent" android : layout_height = "wrap_content" / > < Button android : id = "@+id/button1" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignEnd = "@+id/button4" android : layout_alignRight = "@+id/button4" android : layout_below = "@+id/edt1" android : layout_marginTop = "94dp" android : text = "1" / > < Button android : id = "@+id/button2" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignTop = "@+id/button1" android : layout_toLeftOf = "@+id/button3" android : layout_toStartOf = "@+id/button3" android : text = "2" / > < Button android : id = "@+id/button3" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignTop = "@+id/button2" android : layout_centerHorizontal = "true" android : text = "3" / > < Button android : id = "@+id/button4" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_below = "@+id/button1" android : layout_toLeftOf = "@+id/button2" android : text = "4" / > < Button android : id = "@+id/button5" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignBottom = "@+id/button4" android : layout_alignLeft = "@+id/button2" android : layout_alignStart = "@+id/button2" android : text = "5" / > < Button android : id = "@+id/button6" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignLeft = "@+id/button3" android : layout_alignStart = "@+id/button3" android : layout_below = "@+id/button3" android : text = "6" / > < Button android : id = "@+id/button7" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_below = "@+id/button4" android : layout_toLeftOf = "@+id/button2" android : text = "7" / > < Button android : id = "@+id/button8" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignLeft = "@+id/button5" android : layout_alignStart = "@+id/button5" android : layout_below = "@+id/button5" android : text = "8" / > < Button android : id = "@+id/button9" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignLeft = "@+id/button6" android : layout_alignStart = "@+id/button6" android : layout_below = "@+id/button6" android : text = "9" / > < Button android : id = "@+id/buttonadd" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignEnd = "@+id/edt1" android : layout_alignRight = "@+id/edt1" android : layout_alignTop = "@+id/button3" android : layout_marginLeft = "46dp" android : layout_marginStart = "46dp" android : layout_toRightOf = "@+id/button3" android : text = "+" / > < Button android : id = "@+id/buttonsub" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignEnd = "@+id/buttonadd" android : layout_alignLeft = "@+id/buttonadd" android : layout_alignRight = "@+id/buttonadd" android : layout_alignStart = "@+id/buttonadd" android : layout_below = "@+id/buttonadd" android : text = "-" / > < Button android : id = "@+id/buttonmul" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignLeft = "@+id/buttonsub" android : layout_alignParentEnd = "true" android : layout_alignParentRight = "true" android : layout_alignStart = "@+id/buttonsub" android : layout_below = "@+id/buttonsub" android : text = "*" / > < Button android : id = "@+id/button10" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_below = "@+id/button7" android : layout_toLeftOf = "@+id/button2" android : text = "." / > < Button android : id = "@+id/button0" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignLeft = "@+id/button8" android : layout_alignStart = "@+id/button8" android : layout_below = "@+id/button8" android : text = "0" / > < Button android : id = "@+id/buttonC" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignLeft = "@+id/button9" android : layout_alignStart = "@+id/button9" android : layout_below = "@+id/button9" android : text = "C" / > < Button android : id = "@+id/buttondiv" style = "?android:attr/buttonStyleSmall" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignEnd = "@+id/buttonmul" android : layout_alignLeft = "@+id/buttonmul" android : layout_alignRight = "@+id/buttonmul" android : layout_alignStart = "@+id/buttonmul" android : layout_below = "@+id/buttonmul" android : text = "/" / > < Button android : id = "@+id/buttoneql" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_alignEnd = "@+id/buttondiv" android : layout_alignLeft = "@+id/button10" android : layout_alignRight = "@+id/buttondiv" android : layout_alignStart = "@+id/button10" android : layout_below = "@+id/button0" android : layout_marginTop = "37dp" android : text = "=" / > < / RelativeLayout > |
现在,一切正常,我们准备运行我们的计算器 Android 应用程序。 要运行我们的应用程序,我使用我的手机,您可以使用模拟器或您的设备。
运行我们的计算器 Android 应用
- 点击安卓设备管理器。 在
Android device manager
窗口中选择您的自定义设备后,单击START
。 - 单击运行按钮。
- 选择您的设备或模拟器,然后单击确定。
- 现在您可以看到作为此屏幕截图运行的计算器 android 应用程序。
恭喜!! 如果您已遵循所有步骤并达到这一点,则意味着您已正确遵循所有步骤,并且您的 Calculator android 应用程序已启动并运行良好。