Android Development notes (4) -- MainActivity. java File Modification & amp; layout nesting, androidmainactivity

Source: Internet
Author: User
Tags html notes custom name

Android Development notes (4) -- MainActivity. java File Modification & LAYOUT nesting, androidmainactivity
Note Link: Http://www.cnblogs.com/igoslly/p/6805020.html notes with the development nameCoffeeOrderOfAppActivity as a clue to introduce how the appFunction design → layout settings → code writing → Improvement, IntroductionJava File ModificationAndLayout nesting.I. Application knowledgeFirst, let's take a look at some basic knowledge of AndroidStudio to help you have a basic concept. In the L1 and 2A courses, we learned how to modify the XML code, basically modifying MainActivity. xml. We can directly view the actual view of XML code in the view. Open Emulator in WINDOWS -- install Virtual Device and download the Nexus4 Android platform library. If the system prompts that BIOS does not support VT-X, You need to manually open it in BIOS-configuration-Virtual Technology. 2A course mentionedMainActivity. java by JavaCode Implementation,File DefinitionMainActivityPurpose and function of LayoutAnd contains the actual code of various methods.

 

Concepts: Android DDMS :You can monitor the activity of devices during debugging on your Android phone. Logcat :Output logs to record and collect all actions during device debugging. Pseudo Code :
Pseudocode: an advanced description language that describes each step of an application. The pseudocode is different from the description. It breaks down the specific content of each behavior in the form of text and code thinking, and can directly rewrite each sentence as the code. Local variable & Global variables:A program file usually contains multiple functions. Variables defined in each function are assigned valid values only when the function is running, and are released after the function is completed. global variables are defined in the sub-function and continuously valid when the function is running. II. Application Design Ideas To design the entire application, you must:  Interface display:Display Quantity, Price, OrderButton to add TextViewAnd Button. Data computing:Based on actual conditions QUANTITY× Unit price calculation actual amount -- Introduce variable settings Button Link:Button settings and TextViewSame, mainly involves clicking Button. Increase or decrease in quantity: When you press the add or subtract button, the value can be increased or decreased in real time. QUANTITYShow updates: In settings ViewWe usually set the initial value. When we need to change the display value, we need to define additional activities. Interface display-- Data computing-- When you need to modify the number, you can directly open JavaFile Modification; it is too troublesome to open each time, and JavaNot open to users. UIUpdate and set actions QuantityVariable. UICan be directly modified for each operation QUANTITYIs automatically displayed through the function. Definition mode :( Datatype)( Variable  Name) = ( Initial  Value)
    int num = 0;

 

Button Link --Set ButtonClick activity mainly involves attributes Android: OnClick=" SubmitOrder"Indicates that when you click ButtonButton JavaFile Search SubmitOrderMethod.
    public void submitOrder(View view) {        displaystatus(1);    }

 

Increase or decrease the number --Similarly, the [+] [-] button can define the method" Increment"" Decrement"Method body code: Quantitty= Quantity+ 1; Displayquantity( Quantity);
    public void increment(View view) {        num++;        display(num);        displayPrice(num * 5);        displaystatus(0);    }
  Display update --Pair Quantity_ Text_ ViewTo be modified, You need to define Displayquantity() Displayprice()
 private void display(int number) {        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);        quantityTextView.setText("" + number);    }private void displayPrice(int number) {        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));    }

 

Status update --In the course video, click ORDERThe program calculates the real-time number. But in real life, click ORDERThe order is usually submitted directly, so you need to modify the quantity to update the amount in real time, and then click ORDERWe expect to return the status" Successfully Ordered"Same DisplaypriceMethod, we define DisplayStatus( Char Status), But not here IntNumber, StringString (a string of characters)
private void displaystatus(int number) {        TextView statusTextView = (TextView) findViewById(R.id.status_text_view);        if (num==0)            statusTextView.setText("Please enter quantity !");        else if (number==0)            statusTextView.setText("Ordering...");        else            statusTextView.setText("Successfully ordered !");    }

 

More improvements: Improvement 1 -- Nested StructureLayout Optimization when we need to move ButtonButton Quantity_ Text_ ViewDamaged the original LinearLayout. (1) Global slave LinearLayoutChange RelativeLayout RelativeLayoutDue to flexible layout settings ViewWe still use the vertical structure. We can use the second method. (2) LinearLayoutNested child LinearLayoutFrom the perspective of structure, the changes made to the nested structure are shown in: StudioIn DesignPanel ComponentTree to see different ViewHierarchy Improvement 2 -- String settingsString StringAnd integer IntThe same variables are defined in the same way: String(Type) Stringname(Custom name) =" Dfadfljaldskfj"(Initial Value) The order status can be updated at the same time during the order process" Successfully Ordered"" Order Failed" Actual Operation:Pair DisplaystatusEnter parameters to modify Int  NumString  Status
displaystatus("Please select beverage !");
At the same time, the character string also has the uniqueness of "connection", and can be connected by the plus sign "+, String+ Int= String     Improvement 3 -- Condition judgmentWhen the number is added or subtracted, 0 is not identified, that is, the number-1. amount-5 is generated. DecrementMethod, we can add the following code
 if (num == 0)            num = 0;        else            num--;
Indicates that when the number is 0, it remains unchanged. If the number is not 0 (positive integer), the number is reduced by 1. Improvement 4 -- Gravity Layout_ GravityBy ViewGroupControl GravityBy ViewThe control involves the layout of the control. You can take the following values: Top-- The widget is placed on the top of the container without changing the widget size. Bottom-- The widget is placed at the bottom of the container without changing the widget size. Left-- The widget is placed on the left of the container without changing the widget size. Right-- The widget is placed on the right of the container without changing the widget size. Center_ Vertical-- The widget is placed in the middle of the vertical direction of the container without changing the widget size. Fill_ Vertical-- If needed, the control is extended to the vertical direction. Center_ Horizontal-- The widget is placed in the middle of the horizontal direction of the container without changing the widget size. Fill_ Horizontal-- If needed, the control is extended horizontally. Center-- The widget is placed in the middle of the container without changing the widget size. Fill-- If needed, the control is extended horizontally and vertically. Start-- The widget is placed at the beginning of the container without changing the widget size. End-- The widget is placed at the end of the container without changing the widget size.



Improvement 5 -- Added a variety of drinks Improvement 5 Is combined with improvement 1 ~ 4 Application, including layout nesting, new variable settings, string display, etc. Layout nestingAt level 1 ViewGroupNested child LinearLayout, Add three drinks Button--" MOCHA"" CAPPUCCINO"" GREEN TEA" Set new variables PriceIn this case, not only the number of drinks Num, The price of drinks also changes with the type, increasing the variable Price, The same as the global variable on different buttons OnClickIn the link method PriceAssign values, DisplayPriceShow (make sure the dynamic amount is displayed)
  public void greenteaPrice(View view) {        price=3;        kind="Green tea";        displayPrice(price*num);    }

 

Enrich order status informationSubmit each time ORDERFeedback to the user about the beverage type + quantity, and the global variable of the beverage type setting Kind, StringType, same PriceValue assignment, quantity of drinks NumEnd Displaystatus(" Ordered"+ Kind+" Cup"+ Num);
  public void greenteaPrice(View view) {        price=3;        kind="Green tea";        displayPrice(price*num);    }

 

The effects of CoffeeOrder after all improvements are as follows:

Statement:

1. The text and pictures in this note are both original and original. For reprinted documents, enter "blog garden-igoslly ".

2, Android development courses in 2017 4 years to participate in Google developer learning, notes original http://www.studyjamscn.com/thread-19854-1-1.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.