做了一个A+B的APP,虽然很简单,但是作为初学者还是弄了几个小时才弄好,什么东西都要看书或者百度,但最后成功了,还是很开心的,收货蛮大的。现在把过程写一下:
首先给出效果图:
一开始布局一直有问题,不知道为什么我定义了两个编辑框跟一个按钮,但画出来的时候全都重叠在左上角了,只能输入到一个编辑框,一直卡在这里,后来找了一个输入用户名密码的布局文件参考了一下,发现把原来生成的前面那些删掉,然后设置为垂直布局就不会重叠在一起了,正常画出来之后,代码部分就简单了,一共有三个变量,我把第三个显示结果的框设置成了只读的属性,设置的方法是:android:editable="false"
activity_main.xml如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="请输入第一个数:" 11 /> 12 <EditText 13 android:id="@+id/num1" 14 android:textColorHint="#ff2323" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:hint="num" 18 /> 19 <TextView 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:text="请输入第二个数:" 23 /> 24 <EditText 25 android:id="@+id/num2" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:hint="num" 29 /> 30 <TextView 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 android:text="结果:" 34 /> 35 <EditText 36 android:id="@+id/num3" 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:editable="false" 40 android:hint="结果" 41 /> 42 43 <Button android:id="@+id/button1" 44 android:onClick="button_click" 45 android:layout_width="fill_parent" 46 android:layout_height="wrap_content" 47 android:text="@string/hello_world" 48 /> 49 50 </LinearLayout>