activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.aaa.MainActivity" > 10 11 <TextView 12 android:id="@+id/tv2" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignLeft="@+id/et2" 16 android:layout_centerVertical="true" 17 android:gravity="center" 18 android:text="显示计算结果" 19 android:textStyle="italic" /> 20 21 <EditText 22 android:id="@+id/et2" 23 android:layout_width="fill_parent" 24 android:layout_height="wrap_content" 25 android:layout_above="@+id/tv2" 26 android:layout_alignLeft="@+id/et1" 27 android:ems="10" > 28 29 <requestFocus /> 30 </EditText> 31 32 <EditText 33 android:id="@+id/et1" 34 android:layout_width="fill_parent" 35 android:layout_height="wrap_content" 36 android:layout_above="@+id/et2" 37 android:layout_alignLeft="@+id/tv1" 38 android:ems="10" /> 39 40 <Button 41 android:id="@+id/btn3" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:layout_below="@+id/btn1" 45 android:layout_marginTop="16dp" 46 android:layout_toRightOf="@+id/tv1" 47 android:gravity="center" 48 android:onClick="click" 49 android:text="*" 50 android:textColor="#FFF79E38" 51 android:textSize="25sp" 52 android:textStyle="italic" /> 53 54 <Button 55 android:id="@+id/btn4" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:layout_alignBaseline="@+id/btn3" 59 android:layout_alignBottom="@+id/btn3" 60 android:layout_toRightOf="@+id/btn3" 61 android:gravity="center" 62 android:onClick="click" 63 android:text="÷" 64 android:textColor="#FFF79E38" 65 android:textSize="25sp" 66 android:textStyle="italic" /> 67 68 <TextView 69 android:id="@+id/tv1" 70 android:layout_width="wrap_content" 71 android:layout_height="wrap_content" 72 android:layout_alignBottom="@+id/btn1" 73 android:layout_alignParentLeft="true" 74 android:gravity="center" 75 android:text="计算器" 76 android:textColor="#FFF79E38" 77 android:textSize="25sp" 78 android:textStyle="italic" /> 79 80 <Button 81 android:id="@+id/btn2" 82 android:layout_width="wrap_content" 83 android:layout_height="wrap_content" 84 android:layout_above="@+id/btn3" 85 android:layout_alignLeft="@+id/btn4" 86 android:gravity="center" 87 android:onClick="click" 88 android:text="-" 89 android:textColor="#FFF79E38" 90 android:textSize="25sp" 91 android:textStyle="italic" /> 92 93 <Button 94 android:id="@+id/btn1" 95 android:layout_width="wrap_content" 96 android:layout_height="wrap_content" 97 android:layout_alignParentTop="true" 98 android:layout_marginTop="20dp" 99 android:layout_toLeftOf="@+id/btn2" 100 android:gravity="center" 101 android:onClick="click" 102 android:text="+" 103 android:textColor="#FFF79E38" 104 android:textSize="25sp" 105 android:textStyle="italic" /> 106 107 </RelativeLayout>
MainActivity.java
1 package com.example.bbb; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.TextView; 11 import android.widget.Toast; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 } 20 21 public void click(View v) { 22 TextView tv2 = (TextView) findViewById(R.id.tv2); 23 EditText et1 = (EditText) findViewById(R.id.et1); 24 EditText et2 = (EditText) findViewById(R.id.et2); 25 26 int num1,num2,result=0; 27 num1=Integer.parseInt(et1.getText().toString()); 28 num2=Integer.parseInt(et2.getText().toString()); 29 30 switch (v.getId()) { 31 case R.id.btn1: 32 result=num1+num2; 33 break; 34 case R.id.btn2: 35 result=num1-num2; 36 break; 37 case R.id.btn3: 38 result=num1*num2; 39 break; 40 case R.id.btn4: 41 result=num1/num2; 42 break; 43 } 44 tv2.setText(result+""); 45 } 46 47 }