简洁明了的小计算器,让学生能看懂的例子。(功能并不完善,仅用于学习)
Based On: Android Studio 2.3.2, Ref:http://www.cnblogs.com/UUUP/p/3983309.html
1. 新建工程,起名字为DavidCalculator,域名 example.com
2. 把代码粘贴到相关文件
3. 运行,在模拟器测试
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:stretchColumns="*" 5 android:layout_height="match_parent" > 6 <TextView 7 android:id="@+id/textviewdavid" 8 android:layout_width="wrap_content" 9 android:layout_height="60dp" 10 android:gravity="right" 11 android:textSize="30sp" 12 android:text="TEXT"/> 13 <EditText 14 android:id="@+id/editviewdavid" 15 android:layout_width="wrap_content" 16 android:layout_height="60dp" 17 android:gravity="right" 18 android:textSize="30sp" 19 android:hint="Edit" 20 /> 21 22 23 <TableRow 24 android:id="@+id/tableRow1" 25 android:layout_weight="1" 26 android:layout_width="wrap_content" 27 android:layout_height="match_parent" > 28 29 <Button 30 android:id="@+id/button7" 31 android:layout_width="wrap_content" 32 android:layout_height="match_parent" 33 android:textSize="25sp" 34 android:text="7" /> 35 36 <Button 37 android:id="@+id/button8" 38 android:layout_width="wrap_content" 39 android:textSize="25sp" 40 android:layout_height="match_parent" 41 android:text="8" /> 42 43 <Button 44 android:id="@+id/button9" 45 android:layout_width="wrap_content" 46 android:textSize="25sp" 47 android:layout_height="match_parent" 48 android:text="9" /> 49 50 <Button 51 android:id="@+id/buttonDivide" 52 android:layout_width="wrap_content" 53 android:layout_height="match_parent" 54 android:textSize="25sp" 55 android:text="/" /> 56 57 </TableRow> 58 59 <TableRow 60 android:id="@+id/tableRow2" 61 android:layout_weight="1" 62 android:layout_width="wrap_content" 63 android:layout_height="match_parent" > 64 65 <Button 66 android:id="@+id/button4" 67 android:textSize="25sp" 68 android:layout_width="wrap_content" 69 android:layout_height="match_parent" 70 android:text="4" /> 71 72 <Button 73 android:id="@+id/button5" 74 android:textSize="25sp" 75 android:layout_width="wrap_content" 76 android:layout_height="match_parent" 77 android:text="5" /> 78 79 <Button 80 android:id="@+id/button6" 81 android:textSize="25sp" 82 android:layout_width="wrap_content" 83 android:layout_height="match_parent" 84 android:text="6" /> 85 86 <Button 87 android:id="@+id/buttonMultiply" 88 android:textSize="25sp" 89 android:layout_width="wrap_content" 90 android:layout_height="match_parent" 91 android:text="*" /> 92 93 </TableRow> 94 95 <TableRow 96 android:id="@+id/tableRow3" 97 android:layout_weight="1" 98 android:layout_width="wrap_content" 99 android:layout_height="match_parent" > 100 101 <Button 102 android:id="@+id/button1" 103 android:textSize="25sp" 104 android:layout_width="wrap_content" 105 android:layout_height="match_parent" 106 android:text="1" /> 107 108 <Button 109 android:id="@+id/button2" 110 android:textSize="25sp" 111 android:layout_width="wrap_content" 112 android:layout_height="match_parent" 113 android:text="2" /> 114 115 <Button 116 android:textSize="25sp" 117 android:id="@+id/button3" 118 android:layout_width="wrap_content" 119 android:layout_height="match_parent" 120 android:text="3" /> 121 122 <Button 123 android:id="@+id/buttonMinus" 124 android:textSize="25sp" 125 android:layout_width="wrap_content" 126 android:layout_height="match_parent" 127 android:text="-" /> 128 129 </TableRow> 130 131 <TableRow 132 android:id="@+id/tableRow4" 133 android:layout_weight="1" 134 android:layout_width="wrap_content" 135 android:layout_height="match_parent" > 136 137 <Button 138 android:id="@+id/button0" 139 android:layout_width="wrap_content" 140 android:layout_height="match_parent" 141 android:textSize="25sp" 142 android:text="0" /> 143 144 <Button 145 android:id="@+id/buttonPoint" 146 android:layout_width="wrap_content" 147 android:textSize="25sp" 148 android:layout_height="match_parent" 149 android:text="." /> 150 151 <Button 152 android:id="@+id/buttonPlus" 153 android:textSize="25sp" 154 android:layout_width="wrap_content" 155 android:layout_height="match_parent" 156 android:text="+" /> 157 158 <Button 159 android:id="@+id/buttonEqual" 160 android:textSize="25sp" 161 android:layout_width="wrap_content" 162 android:layout_height="match_parent" 163 android:text="=" /> 164 165 </TableRow> 166 167 <TableRow 168 android:id="@+id/tableRow5" 169 android:layout_weight="1" 170 android:layout_width="wrap_content" 171 android:layout_height="match_parent" > 172 173 <Button 174 android:id="@+id/buttonClear" 175 android:layout_span="4" 176 android:textSize="25sp" 177 android:layout_width="wrap_content" 178 android:layout_height="match_parent" 179 android:text="clear" /> 180 181 </TableRow> 182 183 </TableLayout>
MainActivity.java
1 package com.example.davidcalculator; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import android.view.View.OnClickListener; 10 import java.text.DecimalFormat; 11 12 //ref:http://www.cnblogs.com/UUUP/p/3983309.html //Edit by Hbu_David 2017.6.15 13 14 public class MainActivity extends AppCompatActivity { 15 private EditText editText; //输入框,输入的数字 16 private String operator; //操作符 17 private double n1 , n2 ,Result; //两个操作数,n1为左操作数,n2为右操作数 18 private TextView textView; //文本框,计算过程和计算结果 19 private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,//十个数字按钮 20 btnPlus,btnMinus,btnMultiply,btnDivide,//加减乘除按钮 21 btnPoint,btnEqual,btnClear;//小数点,等号,清空 22 23 private OnClickListener lisenter = new OnClickListener() {//侦听器 24 @Override 25 public void onClick(View view) {//点击事件 26 editText = (EditText)findViewById(R.id.editviewdavid);//与XML中定义好的EditText控件绑定 27 textView = (TextView)findViewById(R.id.textviewdavid);//与XML中定义好的TextView控件绑定 28 editText.setCursorVisible(false);//隐藏输入框光标 29 String str; 30 Button button =(Button)view;//把点击获得的参数传递给button 31 DecimalFormat MyFormat = new DecimalFormat("###.##");//控制Double转为String的格式 32 33 try{ 34 switch(button.getId())//获取点击按钮的ID,通过ID选择对应的选项执行 35 { 36 case R.id.button1://如果点击了“1” 37 { 38 editText.setText(editText.getText().toString() + 1);//输入框末尾,添加一个“1” 39 break; 40 } 41 case R.id.button2://2 42 { 43 editText.setText(editText.getText().toString() + 2); 44 break; 45 } 46 case R.id.button3://3 47 { 48 editText.setText(editText.getText().toString() + 3); 49 break; 50 } 51 case R.id.button4://4 52 { 53 editText.setText(editText.getText().toString() + + 4); 54 break; 55 } 56 case R.id.button5://5 57 { 58 editText.setText(editText.getText().toString() + 5); 59 break; 60 } 61 case R.id.button6://6 62 { 63 editText.setText(editText.getText().toString() + 6); 64 break; 65 } 66 case R.id.button7://7 67 { 68 editText.setText(editText.getText().toString() + 7); 69 break; 70 } 71 case R.id.button8://8 72 { 73 editText.setText(editText.getText().toString() + 8); 74 break; 75 } 76 case R.id.button9://9 77 { 78 editText.setText(editText.getText().toString() + 9); 79 break; 80 } 81 case R.id.button0://0 82 { 83 str = editText.getText().toString(); 84 //此处可以考虑添加代码,限制用户输入00,000等 2017.6.15 85 editText.setText(str + 0); 86 break; 87 } 88 case R.id.buttonClear://Clear 89 { 90 editText.setText(""); 91 textView.setText(""); 92 Result = 0; 93 break; 94 } 95 case R.id.buttonPoint://. 96 { 97 str = editText.getText().toString(); 98 if(str.indexOf(".") != -1) //判断字符串中是否已包含小数点,如果有,就什么也不做 99 {} 100 else //如果没有小数点 101 { 102 if(str.equals("0"))//如果开始为0 103 editText.setText(("0" + ".").toString()); 104 else if(str.equals(""))//如果初时显示为空,就什么也不做 105 {} 106 else 107 editText.setText(str + "."); 108 } 109 break; 110 } 111 case R.id.buttonPlus://操作符+ 112 { 113 str = editText.getText().toString(); 114 n1 = Double.parseDouble(str); 115 operator = "+"; 116 editText.setText(""); 117 textView.setText(MyFormat.format(n1) + operator); 118 break; 119 } 120 case R.id.buttonMinus://操作符- 121 { 122 str = editText.getText().toString(); 123 n1 = Double.parseDouble(str); 124 operator = "-"; 125 editText.setText(""); 126 textView.setText(MyFormat.format(n1) + operator); 127 break; 128 } 129 case R.id.buttonMultiply://操作符* 130 { 131 str = editText.getText().toString(); 132 n1 = Double.parseDouble(str); 133 operator = "*"; 134 editText.setText(""); 135 textView.setText(MyFormat.format(n1) + operator); 136 break; 137 } 138 139 case R.id.buttonDivide://操作符 / 140 { 141 str = editText.getText().toString(); 142 n1 = Double.parseDouble(str); 143 operator = "/"; 144 editText.setText(""); 145 textView.setText(MyFormat.format(n1) + operator); 146 break; 147 } 148 case R.id.buttonEqual://操作符= 149 { 150 if(operator == "+") 151 { 152 str = editText.getText().toString(); 153 n2 = Double.parseDouble(str); 154 Result = n1 + n2; 155 textView.setText(MyFormat.format(n1) + operator + MyFormat.format(n2) + "=" + MyFormat.format(Result) ); 156 editText.setText(MyFormat.format(Result)+""); 157 } 158 else if(operator == "-") 159 { 160 str = editText.getText().toString(); 161 n2 = Double.parseDouble(str); 162 Result = n1 - n2; 163 textView.setText(MyFormat.format(n1) + operator + MyFormat.format(n2) + "="+MyFormat.format(Result)); 164 editText.setText(MyFormat.format(Result)+""); 165 } 166 else if(operator == "*") 167 { 168 str = editText.getText().toString(); 169 n2 = Double.parseDouble(str); 170 Result = n1 * n2; 171 textView.setText(MyFormat.format(n1) + operator + MyFormat.format(n2) + "="+MyFormat.format(Result)); 172 editText.setText(MyFormat.format(Result)+""); 173 } 174 else if(operator == "/") 175 { 176 str = editText.getText().toString(); 177 n2 = Double.parseDouble(str); 178 if(n2 == 0) 179 { 180 editText.setText(""); 181 textView.setText("除数不能为0"); 182 break; 183 } 184 else 185 { 186 Result = n1 / n2; 187 textView.setText(MyFormat.format(n1) + operator + MyFormat.format(n2) + "="+MyFormat.format(Result)); 188 editText.setText(MyFormat.format(Result)+""); 189 } 190 } 191 break; 192 } 193 default: 194 break; 195 } 196 }catch(Exception e){} 197 } 198 }; 199 200 @Override 201 protected void onCreate(Bundle savedInstanceState) { 202 super.onCreate(savedInstanceState); 203 setContentView(R.layout.activity_main); 204 //获取按钮的id 205 btn1 = (Button) findViewById(R.id.button1); 206 btn2 = (Button) findViewById(R.id.button2); 207 btn3 = (Button) findViewById(R.id.button3); 208 btn4 = (Button) findViewById(R.id.button4); 209 btn5 = (Button) findViewById(R.id.button5); 210 btn6 = (Button) findViewById(R.id.button6); 211 btn7 = (Button) findViewById(R.id.button7); 212 btn8 = (Button) findViewById(R.id.button8); 213 btn9 = (Button) findViewById(R.id.button9); 214 btn0 = (Button) findViewById(R.id.button0); 215 btnPlus = (Button) findViewById(R.id.buttonPlus); 216 btnMinus = (Button) findViewById(R.id.buttonMinus); 217 btnMultiply = (Button) findViewById(R.id.buttonMultiply); 218 btnDivide = (Button) findViewById(R.id.buttonDivide); 219 btnPoint = (Button) findViewById(R.id.buttonPoint); 220 btnEqual = (Button) findViewById(R.id.buttonEqual); 221 btnClear = (Button) findViewById(R.id.buttonClear); 222 //为按钮添加监听器 223 btn1.setOnClickListener(lisenter); 224 btn2.setOnClickListener(lisenter); 225 btn3.setOnClickListener(lisenter); 226 btn4.setOnClickListener(lisenter); 227 btn5.setOnClickListener(lisenter); 228 btn6.setOnClickListener(lisenter); 229 btn7.setOnClickListener(lisenter); 230 btn8.setOnClickListener(lisenter); 231 btn9.setOnClickListener(lisenter); 232 btn0.setOnClickListener(lisenter); 233 btnPlus.setOnClickListener(lisenter); 234 btnMinus.setOnClickListener(lisenter); 235 btnMultiply.setOnClickListener(lisenter); 236 btnDivide.setOnClickListener(lisenter); 237 btnPoint.setOnClickListener(lisenter); 238 btnEqual.setOnClickListener(lisenter); 239 btnClear.setOnClickListener(lisenter); 240 } 241 }