Preface: The project document has a perceptual knowledge, then we need to through the continuous practice, the project documents have a rational understanding. Previously toss Unity3d, iOS development is to take calculator, so this time Android development practice is no exception, continue to take calculator toss. Through my summary, you can learn to grasp the text, buttons, input box control of the event listener trigger and some control read and write operations, the programming language can contact the string segmentation, stitching, find and other methods to use, as well as the use of If-else,switch control statements In short, by operating a simple calculator, you can basically familiarize yourself with the process and programming language used in the development environment.
First show the effect of the toss:
Go to Toss steps
The first step: Create the project, complete the project file structure as follows:
The second step: layout interface, find the project file in the Layout->activity_main, the layout code is written as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.wyz.calc.MainActivity" > <textview android:id= "@+id/title" android:layout_width= "Wrap_ Content "android:layout_height=" Wrap_content "android:layout_centerhorizontal=" true "android:layout_m Arginbottom= "15DP" android:text= "@string/mycalc"/> <edittext android:id= "@+id/displaynum" a Ndroid:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true "Android:layout_alignparentright=" true "Android: layout_below= "@id/title" android:layout_marginbottom= "15DP"/> <linearlayout android:id= "@+id/row1 "Android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:layout_below=" @id/D Isplaynum "android:orientation=" horizontal "> <button android:id=" @+id/btn_num_1 " Android:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" android:layout_weight= "1" android:text= "1"/> <button android:id= "@+id/btn_num_2" Android:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" android:layout_weight= "1" android:text= "2"/> <button android:id= "@+id/btn_nu M_3 "android:layout_width=" 0DP "android:layout_height=" 40DP "android:layout_marginright=" 20DP "Android: layout_weight= "1" android:text= "3"/> <button android:id= "@+id/btn_add" and Roid:layout_width= "0DP" android:layout_height= "40DP" android:layout_weight= "1" android:te xt= "+"/> </LinearLayout> <linearlayout android:id= "@+id/row2" android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "android:layout_below=" @id/row1 "android:layout_margintop = "20DP" android:orientation= "horizontal" > <button android:id= "@+id/btn_num_4" a Ndroid:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" android:layout_weight= "1" android:text= "4"/> <button android:id= "@+id/btn_num_5" Android:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" Android:layout_weight= "1" android:text= "5"/> <button android:id= "@+id/btn_num_6" Android:lay Out_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" Android:la yout_weight= "1" android:text= "6"/> <button android:id= "@+id/btn_sub" Androi D:layout_width= "0DP" android:layout_height= "40DP" android:layout_weight= "1" android:text= "-"/> </LinearLayout> <linearlayout android:id= "@+id/row3" android:layout_width= "Fill_par Ent "android:layout_height=" wrap_content "android:layout_below=" @id/row2 "android:layout_margintop=" 2 0DP "android:orientation=" horizontal "> <button android:id=" @+id/btn_num_7 "Android Oid:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" and roid:layout_weight= "1" android:text= "7"/> <button android:id= "@+id/btn_num_8" android:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" Android:layout_weigh t= "1" android:text= "8"/> <button android:id= "@+id/btn_num_9" android:layout _width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" android:layou t_weight= "1" android:text= "9"/> <button android:id= "@+id/btn_mul" android:l Ayout_width= "0DP" android:layout_height= "40DP" android:layout_weight= "1" android:text= "*" /> </LinearLayout> <linearlayout android:id= "@+id/row4" android:layout_width= "fill_parent "Android:layout_height=" wrap_content "android:layout_below=" @id/row3 "android:layout_margintop=" 20DP "Android:orientation= "Horizontal" > <button android:id= "@+id/btn_num_0" android:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" android:layout_weight= "1" android:text= "0"/> <button android:id= "@+id/btn_douhao" android:layout_width= "0 DP "android:layout_height=" 40DP "android:layout_marginright=" 20DP "android:layout_weight= "1" android:text= "•"/> <button android:id= "@+id/btn_equal" Android:layout_w Idth= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" Android:layout_ weight= "1" android:text= "="/> <button android:id= "@+id/btn_div" Android:lay Out_width= "0DP" android:layout_height= "40DP" android:layout_weight= "1" android:text= "/"/ > </linearlayout> <linearlayout android:id= "@+id/row5" android:layout_width= "Fill_parent" android:layout_he ight= "Wrap_content" android:layout_below= "@id/row4" android:layout_margintop= "20DP" Android:orientati on= "Horizontal" > <button android:id= "@+id/btn_ce" android:layout_width= "0DP" android:layout_height= "40DP" android:layout_marginright= "20DP" android:layout_weight= "1" android:text= "CE"/> <button android:id= "@+id/btn_del" android:layout_width= "0DP" android:layout_height= "40DP" android:layout_weight= "1" android:text= "Del"/> </line Arlayout></relativelayout>
Note: The main use of the five layout of the relative layout relativelayout and linear layout linearlayout, there is the layout of their properties; Layout file We also first perceptual knowledge, slowly practice, and then rise to reason.
The third step: write the main code, find the project file in the SRC under the Mainactivity.java, the code is written as follows:
Package Com.wyz.calc;import Android.app.activity;import Android.os.bundle;import android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;public class Mainactivity extends Activity {Button btn_num _1; Button btn_num_2; Button Btn_num_3; Button Btn_num_4; Button btn_num_5; Button Btn_num_6; Button btn_num_7; Button Btn_num_8; Button Btn_num_9; Button Btn_num_0; Button Btn_douhao; Button btn_equal; Button Btn_add; Button btn_sub; Button Btn_mul; Button Btn_div; Button Btn_ce; Button Btn_del; EditText DisplayText; The int ioperator=0;//operator Flag 1 represents the +,2 representative of the-,3 delegate *,4 represents the/double result=0;//the result of the double num1=0;//first operand double num2=0;// The second operand @overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//The Control object of the interface is found based on the control ID recorded in R btn_num_1 = (Button) Findviewbyid (r.id.btn_num_1); Btn_num_2 = (Button) Findviewbyid (r.id.btn_num_2); btn_num_3 = (Button) Findviewbyid (r.id.btn_num_3); btn_num_4 = (Button) Findviewbyid (r.id.btn_num_4); btn_num_5 = ( button) Findviewbyid (r.id.btn_num_5), Btn_num_6 = (button) Findviewbyid (r.id.btn_num_6); btn_num_7 = (Button) Findviewbyid (r.id.btn_num_7); btn_num_8 = (Button) Findviewbyid (r.id.btn_num_8); btn_num_9 = (Button) Findviewbyid ( R.id.btn_num_9) Btn_num_0 = (Button) Findviewbyid (r.id.btn_num_0); Btn_douhao = (Button) Findviewbyid (R.id.btn_douhao ); btn_equal = (Button) Findviewbyid (r.id.btn_equal); btn_add = (Button) Findviewbyid (r.id.btn_add); btn_sub = (Button) Findviewbyid (r.id.btn_sub); Btn_mul = (Button) Findviewbyid (R.id.btn_mul); btn_div = (Button) Findviewbyid (r.id.btn_ div), Btn_ce = (button) Findviewbyid (r.id.btn_ce), Btn_del = (button) Findviewbyid (r.id.btn_del);d Isplaytext = (EditText ) Findviewbyid (R.id.displaynum); Btnlistener Btnlistener = new Btnlistener (); Create Event Listener Btn_num_1.setonclicklistener (Btnlistener); Binding Listener Btn_num_2.setonclicklistener (Btnlistener); Btn_num_3.setonclicklistener (btnlisteNER); Btn_num_4.setonclicklistener (Btnlistener); Btn_num_5.setonclicklistener (Btnlistener); btn_num_6. Setonclicklistener (Btnlistener); Btn_num_7.setonclicklistener (Btnlistener); Btn_num_8.setonclicklistener ( Btnlistener); Btn_num_9.setonclicklistener (Btnlistener); Btn_num_0.setonclicklistener (BtnListener); btn_ Douhao.setonclicklistener (Btnlistener); Btn_equal.setonclicklistener (Btnlistener); Btn_add.setOnClickListener ( Btnlistener); Btn_sub.setonclicklistener (Btnlistener); Btn_mul.setonclicklistener (Btnlistener); btn_ Div.setonclicklistener (Btnlistener); Btn_ce.setonclicklistener (Btnlistener); Btn_del.setonclicklistener ( Btnlistener);} Create a button listener class yourself, implement interface Onclicklistener, implement interface OnClick method class Btnlistener implements onclicklistener{@Overridepublic void OnClick (View v) {charsequence nowtext = Displaytext.gettext ();//each time it is pressed, first get the text content that is displayed string mystring=nowtext.tostring (); Convert to String switch (V.getid ()) {case r.id.btn_num_1:mystring+= "1"; Num1=double.parsedouble (myString); Break;case r.id.btn_num_2:mystring+= "2"; Num1=doubLe.parsedouble (myString); Break;case r.id.btn_num_3:mystring+= "3"; num1=double.parsedouble (myString); break;case r.id.btn_num_4:mystring+= "4"; num1=double.parsedouble (myString); Break;case r.id.btn_num_5:mystring+= "5"; num1= Double.parsedouble (myString); Break;case r.id.btn_num_6:mystring+= "6"; num1=double.parsedouble (myString); break; Case r.id.btn_num_7:mystring+= "7"; num1=double.parsedouble (myString); Break;case r.id.btn_num_8:mystring+= "8"; num1 =double.parsedouble (myString); Break;case r.id.btn_num_9:mystring+= "9"; num1=double.parsedouble (myString); break; Case r.id.btn_num_0:mystring+= "0"; num1=double.parsedouble (myString); Break;case r.id.btn_douhao:if (! Mystring.contains (".")) If there is already, do not add {mystring+= ".";} Break;case r.id.btn_add:ioperator = 1;num2=double.parsedouble (myString); mystring= ""; Break;case R.id.btn_sub: Ioperator = 2;num2=double.parsedouble (myString); mystring= ""; break;case R.id.btn_mul:ioperator = 3;num2= Double.parsedouble (myString); mystring= ""; break;case R.id.btn_div:ioperator = 4;num2=douBle.parsedouble (myString); mystring= ""; Break;case r.id.btn_equal:if (Mystring.isempty ())//If you get the string null, do not operate {break;} Num1=double.parsedouble (myString); if (Ioperator = = 1) {result = Num1 + num2;} else if (Ioperator = = 2) {result = NUM2-NUM1;} else if (Ioperator = = 3) {result = num1*num2;} else if (Ioperator = = 4) {result = NUM2/NUM1;} Num2=result;num1=0;long tmp=0;if (result%1.0 = = 0)//Remove the decimal point after the integer {tmp= (long) result;mystring = string.valueof (TMP); }else{mystring = string.valueof (result); }break;case r.id.btn_ce:num1=0;num2=0;result=0;ioperator=0;mystring= ""; Break;case R.id.btn_del:if ( Mystring.length ()! = 0)//not empty to delete {mystring=mystring.substring (0, Mystring.length ()-1);} Break;default:break;} Debug Information log.d ("Debugnum1", String.valueof (NUM1)); LOG.D ("Debugnum2", String.valueof (num2)); LOG.D ("myString", myString);d isplaytext.settext (myString); }} @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is Present.getmenuinflater (). Inflate (R.menu.main, menu);return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
The main ideas are:
1. The control variables and the variables to be used in the Declaration interface
2. Locate the control object in the layout file by Findviewbyid and assign the object to the variable that corresponds to the declaration
3. Write a class listener, implement the button listener interface, trigger when the control is clicked
4. Control variables, binding listeners
5. Start in class listener, write the logic that triggers the event business processing
The fourth step: the basic completion, the rest is debugging to find the bug
Note: I debug mainly through LOG.D () to print the information to the output log console Logcat