一、实验目的
掌握菜单的创建方式。
掌握普通对话框及自定义对话框的创建方式。
二、实验内容
使用菜单、对话框实现简易计算器。效果大致如下图所示:
三、实验指导
1、创建布局界面,放置两个EditText用于输入数值,运算符使用spinner控件进行绑定。
2、对除数进行判断,如为0表示不合法,使用Snackbar给出错误提示。
3、创建菜单,显示退出及关于选项,当用户点击关于弹出对话框。
4、将模拟器运行结果截图到word文档中,并将核心代码复制到word文档中。
四、模拟器效果截图
五、实验源代码
Calculator.java
1. package com.example.myapplication02; 2. 3. import android.app.AlertDialog; 4. import android.app.Dialog; 5. import android.content.Context; 6. import android.content.DialogInterface; 7. import android.os.Bundle; 8. import android.text.TextUtils; 9. import android.util.Log; 10. import android.view.Menu; 11. import android.view.MenuInflater; 12. import android.view.MenuItem; 13. import android.view.View; 14. import android.widget.AdapterView; 15. import android.widget.ArrayAdapter; 16. import android.widget.Button; 17. import android.widget.EditText; 18. import android.widget.Spinner; 19. import android.widget.TextView; 20. import android.widget.Toast; 21. import androidx.annotation.NonNull; 22. import androidx.appcompat.app.AppCompatActivity; 23. import androidx.coordinatorlayout.widget.CoordinatorLayout; 24. import com.google.android.material.snackbar.BaseTransientBottomBar; 25. import com.google.android.material.snackbar.Snackbar; 26. 27. public class Calculator extends AppCompatActivity { 28. private EditText first; 29. private Spinner spinner; 30. private EditText second; 31. private TextView result; 32. CoordinatorLayout coor; 33. private Button equal; 34. private String[] sign={"+","-","*","/"}; 35. String flag; 36. 37. @Override 38. protected void onCreate(Bundle savedInstanceState) { 39. super.onCreate(savedInstanceState); 40. setContentView(R.layout.calculator); 41. first=(EditText) findViewById(R.id.first); 42. spinner =(Spinner) findViewById(R.id.select); 43. second=(EditText) findViewById(R.id.second); 44. equal=(Button) findViewById(R.id.equ); 45. result=(TextView) findViewById(R.id.result); 46. coor=findViewById(R.id.coor); 47. ArrayAdapter sign1=new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item,sign); 48. spinner.setAdapter(sign1); 49. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 50. @Override 51. public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 52. flag=spinner.getSelectedItem().toString(); 53. } 54. 55. @Override 56. public void onNothingSelected(AdapterView<?> adapterView) { 57. 58. } 59. }); 60. 61. equal.setOnClickListener(new View.OnClickListener() { 62. @Override 63. public void onClick(View view) { 64. if(flag.equals("+")){ 65. Add add=new Add(); 66. add.onClick(view); 67. } 68. 69. if(flag.equals("-")){ 70. Subtract subtract=new Subtract(); 71. subtract.onClick(view); 72. } 73. 74. if(flag.equals("*")){ 75. Multiply multiply=new Multiply(); 76. multiply.onClick(view); 77. } 78. 79. if(flag.equals("/")){ 80. Divide divide=new Divide(); 81. divide.onClick(view); 82. } 83. } 84. }); 85. } 86. 87. private void show(){ 88. AlertDialog.Builder h1=new AlertDialog.Builder(this); 89. h1.setTitle("出错!"); 90. h1.setMessage("数字输入不符合规范!"); 91. h1.setPositiveButton("确定",null); 92. h1.show(); 93. } 94. 95. private class Add implements View.OnClickListener{ 96. public void onClick(View v){ 97. if(TextUtils.isEmpty(first.getText().toString())||TextUtils.isEmpty(second.getText().toString())){ 98. show(); 99. }else{ 100. float m1=Float.parseFloat((first.getText().toString())); 101. float m2=Float.parseFloat((second.getText().toString())); 102. result.setText(m1 + m2+""); 103. } 104. } 105. } 106. 107. private class Subtract implements View.OnClickListener{ 108. public void onClick(View v){ 109. if(TextUtils.isEmpty(first.getText().toString())||TextUtils.isEmpty(second.getText().toString())){ 110. show(); 111. }else{ 112. float m1=Float.parseFloat((first.getText().toString())); 113. float m2=Float.parseFloat((second.getText().toString())); 114. result.setText(m1 - m2+""); 115. } 116. } 117. } 118. 119. private class Multiply implements View.OnClickListener{ 120. public void onClick(View v){ 121. if(TextUtils.isEmpty(first.getText().toString())||TextUtils.isEmpty(second.getText().toString())){ 122. show(); 123. }else{ 124. float m1=Float.parseFloat((first.getText().toString())); 125. float m2=Float.parseFloat((second.getText().toString())); 126. result.setText(m1 * m2+""); 127. } 128. } 129. } 130. 131. private class Divide implements View.OnClickListener{ 132. public void onClick(View v){ 133. if(TextUtils.isEmpty(first.getText().toString())||TextUtils.isEmpty(second.getText().toString())){ 134. show(); 135. }else{ 136. float m1=Float.parseFloat((first.getText().toString())); 137. float m2=Float.parseFloat((second.getText().toString())); 138. if(m2==0){ 139. Snackbar.make(coor,"出错,除数不能为0!", BaseTransientBottomBar.LENGTH_SHORT).show(); 140. }else{ 141. result.setText(m1 / m2+""); 142. } 143. } 144. } 145. } 146. @Override 147. public boolean onCreateOptionsMenu(Menu menu){ 148. MenuInflater inflater=new MenuInflater(this); 149. inflater.inflate(R.menu.calculator,menu); 150. return true; 151. } 152. 153. @Override 154. public boolean onOptionsItemSelected(@NonNull MenuItem item){ 155. switch (item.getItemId()){ 156. case R.id.about: 157. AlertDialog.Builder builder=new AlertDialog.Builder(Calculator.this); 158. builder.setTitle("关于") 159. .setMessage("这是制作的计算器!") 160. .setPositiveButton("确定", new DialogInterface.OnClickListener() { 161. @Override 162. public void onClick(DialogInterface dialogInterface, int i) { 163. 164. } 165. }).setNegativeButton("返回", new DialogInterface.OnClickListener() { 166. @Override 167. public void onClick(DialogInterface dialogInterface, int i) { 168. 169. } 170. }).create().show(); 171. 172. 173. break; 174. case R.id.back: 175. AlertDialog.Builder builder2=new AlertDialog.Builder(Calculator.this); 176. builder2.setTitle("退出") 177. .setMessage("是否确认退出?") 178. .setPositiveButton("是", new DialogInterface.OnClickListener() { 179. @Override 180. public void onClick(DialogInterface dialogInterface, int i) { 181. finish(); 182. } 183. }).setNegativeButton("否", new DialogInterface.OnClickListener() { 184. @Override 185. public void onClick(DialogInterface dialogInterface, int i) { 186. } 187. }).create().show(); 188. break; 189. } 190. return super.onContextItemSelected(item); 191. } 192. }
XML
1. <?xml version="1.0" encoding="utf-8"?> 2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. xmlns:tools="http://schemas.android.com/tools" 4. android:layout_width="match_parent" 5. android:layout_height="match_parent" 6. android:background="@drawable/p01" 7. android:orientation="vertical"> 8. 9. <EditText 10. android:id="@+id/first" 11. android:layout_width="match_parent" 12. android:layout_height="75dp" 13. android:gravity="center" 14. android:hint="请输入数字" 15. android:textSize="30sp"/> 16. 17. <Spinner 18. android:layout_width="match_parent" 19. android:layout_height="wrap_content" 20. android:id="@+id/select"> 21. </Spinner> 22. 23. <EditText 24. android:id="@+id/second" 25. android:layout_width="match_parent" 26. android:layout_height="75dp" 27. android:gravity="center" 28. android:hint="请输入数字" 29. android:textSize="30sp"/> 30. 31. <Button 32. android:id="@+id/equ" 33. android:layout_width="match_parent" 34. android:layout_height="wrap_content" 35. android:text="="> 36. </Button> 37. 38. <EditText 39. android:id="@+id/result" 40. android:layout_width="match_parent" 41. android:layout_height="75dp" 42. android:gravity="center" 43. android:hint="输出结果" 44. android:textSize="30sp" 45. android:ems="6"/> 46. 47. <androidx.coordinatorlayout.widget.CoordinatorLayout 48. android:layout_width="match_parent" 49. android:layout_height="match_parent" 50. android:id="@+id/coor"> 51. </androidx.coordinatorlayout.widget.CoordinatorLayout> 52. 53. </LinearLayout>