layout: activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout android:layout_width="fill_parent" 8 android:layout_height="wrap_content"> 9 <TextView 10 android:id="@+id/tvResult" 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:height="50dp" 14 android:text="tvResult" 15 /> 16 </LinearLayout> 17 <LinearLayout android:layout_width="fill_parent" 18 android:layout_height="wrap_content"> 19 <Button 20 android:id="@+id/btnBackspace" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:width="150dp" 24 android:layout_marginLeft="10dp" 25 android:text="btnBackspace"/> 26 <Button 27 android:id="@+id/btnCE" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:width="150dp" 31 android:text="CE"/> 32 </LinearLayout> 33 <LinearLayout android:layout_width="fill_parent" 34 android:layout_height="wrap_content"> 35 <Button 36 android:id="@+id/btn7" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_marginLeft="10dp" 40 android:width="75dp" 41 android:text="7"/> 42 <Button 43 android:id="@+id/btn8" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:width="75dp" 47 android:text="8"/> 48 <Button 49 android:id="@+id/btn9" 50 android:layout_width="wrap_content" 51 android:layout_height="wrap_content" 52 android:width="75dp" 53 android:text="9"/> 54 <Button 55 android:id="@+id/btnDiv" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:width="75dp" 59 android:text="/"/> 60 </LinearLayout> 61 <LinearLayout android:layout_width="fill_parent" 62 android:layout_height="wrap_content"> 63 <Button 64 android:id="@+id/btn4" 65 android:layout_width="wrap_content" 66 android:layout_height="wrap_content" 67 android:layout_marginLeft="10dp" 68 android:width="75dp" 69 android:text="4"/> 70 <Button 71 android:id="@+id/btn5" 72 android:layout_width="wrap_content" 73 android:layout_height="wrap_content" 74 android:width="75dp" 75 android:text="5"/> 76 <Button 77 android:id="@+id/btn6" 78 android:layout_width="wrap_content" 79 android:layout_height="wrap_content" 80 android:width="75dp" 81 android:text="6"/> 82 <Button 83 android:id="@+id/btnMul" 84 android:layout_width="wrap_content" 85 android:layout_height="wrap_content" 86 android:width="75dp" 87 android:text="*"/> 88 </LinearLayout> 89 <LinearLayout android:layout_width="fill_parent" 90 android:layout_height="wrap_content"> 91 <Button 92 android:id="@+id/btn1" 93 android:layout_width="wrap_content" 94 android:layout_height="wrap_content" 95 android:layout_marginLeft="10dp" 96 android:width="75dp" 97 android:text="1"/> 98 <Button 99 android:id="@+id/btn2" 100 android:layout_width="wrap_content" 101 android:layout_height="wrap_content" 102 android:width="75dp" 103 android:text="2"/> 104 <Button 105 android:id="@+id/btn3" 106 android:layout_width="wrap_content" 107 android:layout_height="wrap_content" 108 android:width="75dp" 109 android:text="3"/> 110 <Button 111 android:id="@+id/btnAdd" 112 android:layout_width="wrap_content" 113 android:layout_height="wrap_content" 114 android:width="75dp" 115 android:text="+"/> 116 </LinearLayout> 117 <LinearLayout android:layout_width="fill_parent" 118 android:layout_height="wrap_content"> 119 <Button 120 android:id="@+id/btn0" 121 android:layout_width="wrap_content" 122 android:layout_height="wrap_content" 123 android:layout_marginLeft="10dp" 124 android:width="75dp" 125 android:text="0"/> 126 <Button 127 android:id="@+id/btnC" 128 android:layout_width="wrap_content" 129 android:layout_height="wrap_content" 130 android:width="75dp" 131 android:text="C"/> 132 <Button 133 android:id="@+id/btnEqu" 134 android:layout_width="wrap_content" 135 android:layout_height="wrap_content" 136 android:width="75dp" 137 android:text="="/> 138 <Button 139 android:id="@+id/btnSub" 140 android:layout_width="wrap_content" 141 android:layout_height="wrap_content" 142 android:width="75dp" 143 android:text="-"/> 144 </LinearLayout> 145 </LinearLayout>
MainActivity.java
package com.example.cal; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener{ //声明一些控件 Button btn0=null; Button btn1=null; Button btn2=null; Button btn3=null; Button btn4=null; Button btn5=null; Button btn6=null; Button btn7=null; Button btn8=null; Button btn9=null; Button btnBackspace=null; Button btnCE=null; Button btnC=null; Button btnAdd=null; Button btnSub=null; Button btnMul=null; Button btnDiv=null; Button btnEqu=null; TextView tvResult=null; //声明两个参数。接收tvResult前后的值 double num1=0,num2=0; double Result=0;//计算结果 int op=0;//判断操作数, boolean isClickEqu=false;//判断是否按了“=”按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //从布局文件中获取控件, btn0=(Button)findViewById(R.id.btn0); btn1=(Button)findViewById(R.id.btn1); btn2=(Button)findViewById(R.id.btn2); btn3=(Button)findViewById(R.id.btn3); btn4=(Button)findViewById(R.id.btn4); btn5=(Button)findViewById(R.id.btn5); btn6=(Button)findViewById(R.id.btn6); btn7=(Button)findViewById(R.id.btn7); btn8=(Button)findViewById(R.id.btn8); btn9=(Button)findViewById(R.id.btn9); btnBackspace=(Button)findViewById(R.id.btnBackspace); btnCE=(Button)findViewById(R.id.btnCE); btnC=(Button)findViewById(R.id.btnC); btnEqu=(Button)findViewById(R.id.btnEqu); btnAdd=(Button)findViewById(R.id.btnAdd); btnSub=(Button)findViewById(R.id.btnSub); btnMul=(Button)findViewById(R.id.btnMul); btnDiv=(Button)findViewById(R.id.btnDiv); tvResult=(TextView)findViewById(R.id.tvResult); //添加监听\ btnBackspace.setOnClickListener(this); btnCE.setOnClickListener(this); btn0.setOnClickListener(this); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); btn7.setOnClickListener(this); btn8.setOnClickListener(this); btn9.setOnClickListener(this); btnAdd.setOnClickListener(this); btnSub.setOnClickListener(this); btnMul.setOnClickListener(this); btnDiv.setOnClickListener(this); btnEqu.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { //btnBackspace和CE-------------------- case R.id.btnBackspace: String myStr=tvResult.getText().toString(); try { tvResult.setText(myStr.substring(0, myStr.length()-1)); } catch (Exception e) { tvResult.setText(""); } break; case R.id.btnCE: tvResult.setText(null); break; //btn0--9--------------------------- case R.id.btn0: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString=tvResult.getText().toString(); myString+="0"; tvResult.setText(myString); break; case R.id.btn1: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString1=tvResult.getText().toString(); myString1+="1"; tvResult.setText(myString1); break; case R.id.btn2: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString2=tvResult.getText().toString(); myString2+="2"; tvResult.setText(myString2); break; case R.id.btn3: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString3=tvResult.getText().toString(); myString3+="3"; tvResult.setText(myString3); break; case R.id.btn4: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString4=tvResult.getText().toString(); myString4+="4"; tvResult.setText(myString4); break; case R.id.btn5: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString5=tvResult.getText().toString(); myString5+="5"; tvResult.setText(myString5); break; case R.id.btn6: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString6=tvResult.getText().toString(); myString6+="6"; tvResult.setText(myString6); break; case R.id.btn7: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString7=tvResult.getText().toString(); myString7+="7"; tvResult.setText(myString7); break; case R.id.btn8: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString8=tvResult.getText().toString(); myString8+="8"; tvResult.setText(myString8); break; case R.id.btn9: if(isClickEqu) { tvResult.setText(null); isClickEqu=false; } String myString9=tvResult.getText().toString(); myString9+="9"; tvResult.setText(myString9); break; //btn+-*/=-------------------------------- case R.id.btnAdd: String myStringAdd=tvResult.getText().toString(); if(myStringAdd.equals(null)) { return; } num1=Double.valueOf(myStringAdd); tvResult.setText(null); op=1; isClickEqu=false; break; case R.id.btnSub: String myStringSub=tvResult.getText().toString(); if(myStringSub.equals(null)) { return; } num1=Double.valueOf(myStringSub); tvResult.setText(null); op=2; isClickEqu=false; break; case R.id.btnMul: String myStringMul=tvResult.getText().toString(); if(myStringMul.equals(null)) { return; } num1=Double.valueOf(myStringMul); tvResult.setText(null); op=3; isClickEqu=false; break; case R.id.btnDiv: String myStringDiv=tvResult.getText().toString(); if(myStringDiv.equals(null)) { return; } num1=Double.valueOf(myStringDiv); tvResult.setText(null); op=4; isClickEqu=false; break; case R.id.btnEqu: String myStringEqu=tvResult.getText().toString(); if(myStringEqu.equals(null)) { return; } num2=Double.valueOf(myStringEqu); tvResult.setText(null); switch (op) { case 0: Result=num2; break; case 1: Result=num1+num2; break; case 2: Result=num1-num2; break; case 3: Result=num1*num2; break; case 4: Result=num1/num2; break; default: Result=0; break; } tvResult.setText(String.valueOf(Result)); isClickEqu=true; break; default: break; } } }
时间: 2024-10-14 08:31:42