English feliratok

← Resource IDs

Beágyazókód kérése
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Now that you know what
    Android resources are,
  2. and that you can put them
    in different folders.
  3. Let's talk about how you can actually
    access the resources that you saved in
  4. those folders.
  5. When your app is compiled,
    there's a tool in Android called AAPT.
  6. And that generates the R class.
  7. This is simply an R.Java file, and
    I'll show you an example next.
  8. It contains the resource IDs for all
    the resources in your S directory, so
  9. for all the resources that you've added,
    there's an ID to refer to it.
  10. The ID is in a format that is
    based on the resource type, so for
  11. all Java resources which are graphics it
    would follow the format of R.drawable.
  12. For all strings that are resources,
    it would follow the formula
  13. R.string.resource name which
    is hello in this case.
  14. Note there are two ways that
    you can access a resource.
  15. In Java code it follows the format of
    R.resource type.resource name In XML
  16. though, it follows the at notation, so
    it would be at and then the resource
  17. type would be string slash, and
    then the resource name, which is hello.
  18. Here are more examples of different
    resource types and how to access them.
  19. These are just a couple of
    the common resource types,
  20. but there are many others.
  21. You always start with R, a dot,
  22. and then resource type, a dot,
    and then the resource name.
  23. And the name is whatever you assign it.
  24. And then in XML you have @ and
    then the resource type, slash,
  25. and then the name of the resource.
  26. Let's take a moment to find
    all the resource IDs in
  27. our main activity Java file.
  28. I'm going to open up the find
    command in Android Studio.
  29. I'm going to use Cmd+F, or
    you can use Ctrl+F if you're on Windows.
  30. It pops up a search field here.
  31. I'm going to search for
    capital R dot, and
  32. then click on match case, so
    that it only grabs the capital R cases.
  33. The first thing it finds is this
    r.layout.activity_main reference.
  34. This is interesting because we're
    referring to the activity main
  35. layout file.
  36. This is actually how the main activity
    knows to load up this XML file.
  37. Isn't that cool?
  38. Let's look at the next reference.
  39. Okay.
    In the displayQuantity method,
  40. we have a reference to
    R.id.quantity_text_view.
  41. So this is referring to a ViewId.
  42. Specifically, the TextView
    that displays the quantity.
  43. Hopefully some of the puzzle
    pieces are coming together.
  44. By referring to this
    view in the Java code.
  45. We can update it while the app is
    running to show this numberOfCoffees.
  46. Let's go to the next occurance.
  47. OK in the displayMessage
  48. method we see a reference to
    R.id.order_summary_text_view.
  49. Here it seems like we're trying to
    refer to this text view which displays
  50. the order summary, so
    that we can update it with this message.