Do a a+b app, although very simple, but as a beginner or to get a few hours to fix, anything to read or Baidu, but finally succeeded, or very happy, the receipt is quite large. Now write the process:
First, give the following:
At the beginning of the layout has been a problem, do not know why I defined two edit box with a button, but the picture is all overlapping in the upper left corner, can only be entered into an edit box, has been stuck here, and later found a user name password input layout file reference, found that the original generated before those deleted, And then set to vertical layout will not overlap together, after normal drawing, the code part is simple, a total of three variables, I set the third box to display the result is a read-only property, set the method is: android:editable= "false"
Activity_main.xml as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns: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 <TextView8 Android:layout_width= "Fill_parent" 9 Android:layout_height= "Wrap_content" Ten Android:text= "Please enter the first number:" One /> A <EditText - Android:id= "@+id/num1" - Android:textcolorhint= "#ff2323" the Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:hint= "num" - /> + <TextView - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" A Android:text= "Please enter a second number:" at /> - <EditText - Android:id= "@+id/num2" - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:hint= "num" in /> - <TextView to Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" - Android:text= "Result:" the /> * <EditText $ Android:id= "@+id/num3"Panax Notoginseng Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" the android:editable= "false" + Android:hint= "Result" A /> the + <ButtonAndroid:id= "@+id/button1" - Android:onclick= "Button_Click" $ Android:layout_width= "Fill_parent" $ Android:layout_height= "Wrap_content" - Android:text= "@string/hello_world" - /> the - </LinearLayout>
View Code
Mainactivity.java as follows:
1 PackageCom.example.hehe;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.view.Menu;6 7 ImportAndroid.view.View;8 9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.EditText; One A ImportAndroid.widget.Toast; - Importandroid.app.Activity; - ImportAndroid.graphics.Color; the ImportAndroid.os.Bundle; - Importandroid.provider.CalendarContract.Colors; - Importandroid.view.KeyEvent; - ImportAndroid.view.View; + ImportAndroid.view.View.OnKeyListener; - ImportAndroid.widget.EditText; + ImportAndroid.widget.TextView; A at Public classMainactivityextendsActivity { - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - } in - to @Override + Public BooleanOncreateoptionsmenu (Menu menu) { - //inflate the menu; This adds items to the action bar if it is present. the getmenuinflater (). Inflate (R.menu.main, menu); * return true; $ }Panax Notoginseng Public voidButton_Click (View v) - { the //Button button1 = (button) Findviewbyid (r.id.button1); +Button button1 =(Button) v; A theEditText NUM1 =(EditText) Findviewbyid (R.ID.NUM1); +EditText num2 =(EditText) Findviewbyid (r.id.num2); -EditText num3 =(EditText) Findviewbyid (R.ID.NUM3); $ intA =Integer.parseint (Num1.gettext (). toString ()); $ intb =Integer.parseint (Num2.gettext (). toString ()); - intc = A +b; -String te = "+a+" + "+b+" = "+C; the Num3.settext (TE); - Num3.settextcolor (color.green);Wuyi } the}View Code
Android Development Learning notes-a calculator with interface a+b