Return to Video

06a q Modifying First Line of Java Code

  • 0:01 - 0:01
    Hooray!
  • 0:01 - 0:04
    You just made your app interactive for
    the user.
  • 0:04 - 0:07
    Before, when you tapped on the button,
    it didn't do anything.
  • 0:07 - 0:08
    But now, with some code changes,
  • 0:08 - 0:12
    when you tap on the button, it actually
    updates something on the screen.
  • 0:12 - 0:12
    Great job!
  • 0:13 - 0:16
    Let's try to dig deeper into
    what you just accomplished.
  • 0:16 - 0:21
    First you added a new android:onClick=
    attribute on the button XML element.
  • 0:21 - 0:23
    This means when
    the button is clicked on,
  • 0:23 - 0:28
    then it will try to perform or
    execute as we say in Java
  • 0:28 - 0:31
    a set of instructions that
    have been named submit order.
  • 0:32 - 0:34
    Okay.
    Going into the main activity
  • 0:34 - 0:35
    file that we gave you.
  • 0:35 - 0:38
    I know there's a lot of weird
    symbols and words on this screen.
  • 0:38 - 0:41
    But all you need to know now
    is that this is a method and
  • 0:41 - 0:43
    its name is submit order.
  • 0:43 - 0:46
    A method can contain
    multiple lines of java code.
  • 0:46 - 0:48
    And this translates into
    a set of instructions
  • 0:48 - 0:51
    that the device can understand and
    perform.
  • 0:51 - 0:54
    If there are multiple lines
    of code in this method,
  • 0:54 - 0:58
    the device would perform the first line
    first and then the second line and so
  • 0:58 - 1:00
    on until the bottom of the method.
  • 1:00 - 1:04
    The reason why we give methods a name is
    so that we can easily refer to it later.
  • 1:04 - 1:08
    For example, in the XML we can refer
    to the method submitOrder now.
  • 1:08 - 1:10
    It's sort of how I have a name,
    you have a name.
  • 1:10 - 1:13
    If you had a pet,
    you would also give it a name so
  • 1:13 - 1:15
    that you can easily refer to it.
  • 1:15 - 1:18
    So in code, if we have a bunch of
    statements that we want to refer to,
  • 1:18 - 1:22
    we can put it inside a method,
    and then give the method a name.
  • 1:22 - 1:26
    So remember in lesson one how we told
    you that XML has very specific syntax
  • 1:26 - 1:30
    rules that determine whether
    something is valid XML or not.
  • 1:30 - 1:35
    Well, Java has similar types of rules
    for determining what is valid Java code.
  • 1:35 - 1:39
    For methods, the statements inside of
    them must be contained within this
  • 1:39 - 1:43
    opening squiggly brace and
    this closing squiggly brace.
  • 1:43 - 1:46
    Up here,
    this is also a method called onCreate.
  • 1:46 - 1:50
    The statements inside this method are
    contained within this opening brace and
  • 1:50 - 1:52
    this closing brace.
  • 1:52 - 1:55
    And, lastly, there's this method,
    which is called display.
  • 1:55 - 1:58
    The code statements inside of it
    are marked off by this opening brace and
  • 1:58 - 2:00
    this closing brace.
  • 2:00 - 2:03
    You also notice that there's also
    an opening and closing brace for
  • 2:03 - 2:04
    the MainActivity.
  • 2:04 - 2:07
    All the functionality
    of the MainActivity
  • 2:07 - 2:10
    is defined within this opening and
    closing brace.
  • 2:10 - 2:13
    So far, this MainActivity
    contains three methods inside it.
  • 2:13 - 2:15
    So after you declare a method,
  • 2:15 - 2:20
    which means defining the method,
    then you can actually use that method.
  • 2:20 - 2:22
    In this example,
    we declare the display method, and
  • 2:22 - 2:26
    then we use it on this line of code,
    where we call it by its name.
  • 2:26 - 2:30
    So this is where we declare the method
    and this is where we call the method.
  • 2:30 - 2:32
    So to summarize,
    when the button is pressed in the UI,
  • 2:32 - 2:35
    then it triggers the submitOrder
    method to be called.
  • 2:35 - 2:39
    Then we execute each line of
    code in the submitOrder method
  • 2:39 - 2:41
    from the top of the method
    to the bottom of the method.
  • 2:41 - 2:45
    In this case, there's only one code
    statement in there, so we just run that.
  • 2:45 - 2:48
    Okay, now let's look at this line
    of code here that says display one.
  • 2:48 - 2:52
    The reason why I picked this line
    of code is because it seems like
  • 2:52 - 2:56
    the number inside the parenthesis is
    what gets displayed on the screen here.
  • 2:57 - 3:00
    So, when I click on this
    order button here, in the UI,
  • 3:00 - 3:04
    then it triggers the submit order method
    to be called in the main activity.
  • 3:04 - 3:07
    Then we perform this line of
    code here that says display 1.
  • 3:07 - 3:10
    And that updates
    the screen to show 1 here.
  • 3:10 - 3:14
    So if I change this to be 2 here,
    what do you think will happen?
  • 3:14 - 3:15
    Let's run the app to find out.
  • 3:16 - 3:18
    And now when I click on this button,
    it shows 2.
  • 3:18 - 3:23
    Clicking on the order button triggers
    the submit order method to be called.
  • 3:23 - 3:26
    And then we perform this line
    of code that says display 2.
  • 3:26 - 3:28
    And that's what updates
    the screen to show 2.
  • 3:28 - 3:29
    Now it's your turn.
  • 3:29 - 3:33
    Can you try changing this number here
    in parenthesis to a different number?
  • 3:33 - 3:37
    You can pick another small number,
    a big number, or even a negative number.
  • 3:37 - 3:40
    Once you've experimented with this
    line of code in the main activity
  • 3:40 - 3:42
    then check this box when you're done.
タイトル:
06a q Modifying First Line of Java Code
概説:

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
UD837 - Android for Beginners
Duration:
03:43

English subtitles

改訂 Compare revisions