Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Androidアプリを作る 第5回

nonylene
December 13, 2016

Androidアプリを作る 第5回

nonylene

December 13, 2016
Tweet

More Decks by nonylene

Other Decks in Technology

Transcript

  1. "OESPJEΞϓϦΛ࡞Δ

    ,.$OPOZMFOF
    "DUJWJUZϨΠΞ΢τ

    View full-size slide

  2. ࣗݾ঺հ
    w OPOZMFOF ͷʹΕΜ

    w ,.$ճ
    w ΧʔϏΟͰ͢
    w ϙέϞϯϐΫϩεྑ͍Ͱ͢
    w SPPU

    View full-size slide

  3. ࣗݾ঺հ
    w OPOZMFOF ͷʹΕΜ

    w εϚϗΞϓϦͱ͔
    w 5XJUUFS(JU)VCFUD
    w IUUQOPOZMFOFIBUFOBCMPHKQ

    View full-size slide

  4. ࠓ೔΍Δ͜ͱ
    "DUJWJUZ
    *OUFOU
    ϨΠΞ΢τ

    View full-size slide

  5. "DUJWJUZ
    w "DUJWJUZ͸ը໘
    w ΞϓϦͷجຊཁૉ
    w ը໘ͳͷͰෳ਺͋Δ͜ͱ͕ଟ͍
    w جຊతʹ"DUJWJUZ্Ͱશ͕ͯߦΘΕΔ

    View full-size slide

  6. "DUJWJUZͷྫ

    View full-size slide

  7. w ΞϓϦΛىಈˠ"DUJWUJZ͕ىಈ
    w ը໘Ҡಈˠผͷ"DUJWJUZ͕ىಈ
    w "DUJWJUZͷதʹ"DUJWJUZ͸࣋ͨͳ͍
    w ৗʹͨͩҰͭͷ"DUJWJUZ͕લ໘ʹग़͍ͯΔ
    w .VMUJ8JOEPXͳͲͷྫ֎΋͋Δ
    ͋Ε΋͜Ε΋"DUJWJUZ

    View full-size slide

  8. "DUJWJUZͷߏ଄

    View full-size slide

  9. * MainActivity.java
    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }
    "DUJWJUZ

    View full-size slide

  10. w 4VQQPSU-JCSBSZ
    w "OESPJE4%,ͷ࠷৽ͷػೳ͕Ұ෦ೖ͍ͬͯΔ
    w ͜ΕͰݹ͍୺຤ʹ΋࠷৽ͷػೳ͕ʂ
    • .*Compat.*
    • .*Support.*
    • ࠓճ͸ AppCompatActivity Λ࢖͏
    ิ଍

    View full-size slide

  11. * MainActivity.java
    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }
    ىಈ͢Δ࣌ʹݺ͹ΕΔ
    ίϯετϥΫλͰ Activity ؔ࿈ͷॳظԽΛ͠Α͏ͱ͢Δͱ·ͩॳظԽ͕ࡁΜͰͳ͍͜ͱ͕͋Δ

    View full-size slide

  12. w onCreate()ىಈʢॳظԽॲཧʣ
    w onStart() ࢝·Δ
    w ྫىಈ͢ΔɾStopޙ·ͨલ໘ʹ໭ͬͨ
    w onStop()ࢭ·ͬͨ
    w ྫऴྃ͢Δɾଞͷ"DUJWJUZ͕લ໘ʹग़ͨ
    w onDestroy() ऴྃ͢Δʢޙย෇͚ʣ
    ϥΠϑαΠΫϧ

    View full-size slide

  13. w ଞʹ΋͍Ζ͍Ζ͋Δ
    w IUUQTEFWFMPQFSBOESPJEDPNSFGFSFODF
    BOESPJEBQQ"DUJWJUZIUNM"DUJWJUZ-JGFDZDMF
    ϥΠϑαΠΫϧ

    View full-size slide

  14. * MainActivity.java
    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    } ϨΠΞ΢τΛදࣔ
    R.layout.activity_main ʹؔͯ͠͸ޙड़

    View full-size slide

  15. * AndroidManifest.xml











    "OESPJE.BOJGFTUYNM

    View full-size slide

  16. ৽͍͠"DUJWJUZΛ࡞੒
    w l1SPKFDUzͷlBQQzҎԼͰӈΫϦοΫ
    ˠ l/FXzl"DUJWJUZzl&NQUZ"DUJWJUZz

    View full-size slide

  17. ৽͍͠"DUJWJUZΛ࡞੒
    w ྫ.BJO"DUJWJUZ
    w Main2Activity.java͕࡞ΒΕΔ
    w ϨΠΞ΢τ͕࡞ΒΕΔ
    w AndroidManifest.xmlʹ

    Main2Activity͕௥Ճ͞ΕΔ

    View full-size slide

  18. *OUFOU
    w ଞͷ"DUJWJUZΛىಈ͢ΔͨΊͷΫϥε
    w "DUJWJUZಉ࢜Ͱ͸ϝϯόม਺ͳͲʹ

    ௚઀ΞΫηε͠ͳ͍
    ˠ ىಈΛࢦࣔ͢Δ*OUFOUʹσʔλΛೖΕͯ౉͢

    w ଞͷ࢖༻๏΋͋Δ ྫ4FSWJDF
    ׂ͕Ѫ

    View full-size slide

  19. *OUFOU
    w ໌ࣔత*OUFOU
    w ىಈ͢Δ"DUJWJUZΛࢦఆ͢Δ
    w ଞͷը໘ʹભҠ͍ͨ࣌͠ͳͲʹ࢖͏

    View full-size slide

  20. ໌ࣔత*OUFOU
    * MainActivity.java
    Intent intent = new Intent(this, Main2Activity.class);
    intent.putExtra("name", “nonylene");
    startActivity(intent);
    .BJO"DUJWJUZΛىಈ
    this ͸ Context Ͱɺ͜͜Ͱ͸ MainActivity ࣗ਎ΛೖΕ͍ͯΔ. 

    (Activity ͸ Context ͷࢠΫϥε)
    Context ͸ΞϓϦͷ؀ڥ৘ใͳͲΛऔಘ͢Δͷʹ࢖ΘΕ͍ͯΔ.

    View full-size slide

  21. * MainActivity.java
    Intent intent = new Intent(this, Main2Activity.class);
    intent.putExtra("name", “nonylene");
    startActivity(intent);
    ໌ࣔత*OUFOU
    lOPOZMFOFzͱ͍͏σʔλΛ֨ೲ

    View full-size slide

  22. * MainActivity.java
    Intent intent = new Intent(this, Main2Activity.class);
    intent.putExtra("name", “nonylene");
    startActivity(intent);
    ໌ࣔత*OUFOU
    *OUFOUΛ౤͛Δ .BJO"DUJWJUZ͕ىಈ

    View full-size slide

  23. *OUFOU
    w ҉໧త*OUFOU
    w ʮڞ༗ʯͱݴΘΕ͍ͯΔ΋ͷ
    w ςΩετΛଞͷΞϓϦͰॲཧ͢Δ౳

    View full-size slide

  24. *OUFOU
    w ҉໧త*OUFOU
    w ىಈ͢Δ"DUJWJUZ͸ࢦఆ͠ͳ͍
    w *OUFOUͷΞΫγϣϯʢछྨʣΛࢦఆ͢Δ
    w ड͚औΓଆͷ"DUJWJUZ͸AndroidManifestʹ

    ड͚औΔΞΫγϣϯɾ63*Λهड़
    w ىಈ͢Δ"DUJWJUZΛϢʔβʔ͕બͿ

    View full-size slide

  25. ҉໧త*OUFOU
    * MainActivity.java
    Uri uri = Uri.parse(“https://www.kmc.gr.jp/");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    6SJΦϒδΣΫτΛ࡞੒

    View full-size slide

  26. ҉໧త*OUFOU
    * MainActivity.java
    Uri uri = Uri.parse(“https://www.kmc.gr.jp/");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    "$5*0/@7*&8ʢӾཡ͢Δʣͱ։͘6SJΛࢦఆ

    View full-size slide

  27. ҉໧త*OUFOU
    *OUFOUΛ౤͛Δ ΞϓϦબ୒ը໘͕ىಈ

    * MainActivity.java
    Uri uri = Uri.parse(“https://www.kmc.gr.jp/");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);

    View full-size slide

  28. ҉໧త*OUFOU

    View full-size slide

  29. "OESPJEͷϨΠΞ΢τ
    w 9.-Ͱఆٛ
    w )5.-ײ֮Ͱॻ͘ͱྑ͍Ͱ͢<ٞ࿦>
    w ը໘ύʔπͷ͜ͱΛ7JFXͱ͍͏

    View full-size slide

  30. งғؾ
    * activity_main.xml

    android:id="@+id/activity_main"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.nonylene.myslack.MainActivity">
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

    View full-size slide

  31. งғؾ
    * activity_main.xml

    android:id="@+id/activity_main"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.nonylene.myslack.MainActivity">
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

    શମͷϨΠΞ΢τ

    View full-size slide

  32. งғؾ
    * activity_main.xml

    android:id="@+id/activity_main"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="net.nonylene.myslack.MainActivity">
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

    5FYUදࣔ

    View full-size slide

  33. ϨΠΞ΢τͷجૅ஌ࣝ

    View full-size slide

  34. "UUSJCVUFT ଐੑ

    w 9.-ͷه๏
    w ཁૉʹରͯ͠ଐੑͷ৘ใΛՃ͑Δ
    w "OESPJEͰ͸“android:”͔Β࢝ΊΔ
    w l໊લۭؒzͱ͍͏
    w *%&Ͱ࢖͏BUUSCVUFT͸“tools:”
    w 4VQQPSU-JCSBSZͰ͸“app:” ͕ଟ͍

    View full-size slide

  35. * activity_main.xml
    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"
    tools:context="net.nonylene.myslack.MainActivity"
    …>
    android:text="Hello World!" …/>

    BOESPJEͱUPPMTΛఆٛ
    "UUSJCVUFT ଐੑ

    View full-size slide

  36. * activity_main.xml
    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"
    tools:context="net.nonylene.myslack.MainActivity"
    …>
    android:text="Hello World!" …/>

    3FMBUJWF-BZPVUʹଐੑΛ௥Ճ
    "UUSJCVUFT ଐੑ

    View full-size slide

  37. 5FYU7JFXʹଐੑΛ௥Ճ
    * activity_main.xml
    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"
    tools:context="net.nonylene.myslack.MainActivity"
    …>
    android:text="Hello World!" …/>

    "UUSJCVUFT ଐੑ

    View full-size slide

  38. *%
    w 7JFXΛଞͷ৔ॴ +BWB΍ଞͷ7JFX
    ͔Βࢀ
    র͢ΔͨΊͷ*%
    w ֤ϨΠΞ΢τ͝ͱʹҰҙ

    View full-size slide

  39. *%
    * activity_main.xml
    android:id="@+id/activity_main"
    …>
    android:id="@+id/text_view"
    android:layout_alignParentTop="@id/activity_main"
    …/>

    ϨΠΞ΢τ*%ͷఆٛ !JE

    View full-size slide

  40. *%
    * activity_main.xml
    android:id="@+id/activity_main"
    …>
    android:id="@+id/text_view"
    android:layout_alignParentTop="@id/activity_main"
    …/>

    ϨΠΞ΢τ*%ͷ࢖༻ !JE

    View full-size slide

  41. 3Ϋϥε
    w +BWB͔ΒϨΠΞ΢τͷ7JFX΍֤छϦιʔε
    Λࢀর͢ΔͨΊͷΫϥε
    w ϑΝΠϧ໊ɾ*%໊౳͔ΒࣗಈͰੜ੒͞ΕΔ

    View full-size slide

  42. 3Ϋϥε
    • ϨΠΞ΢τ layout/activity_main.xml
    → R.layout.activity_main
    • 7JFX@id/text_view
    → R.id.text_view

    • ͦͷଞɺR.string.hoge ΍ R.dimen.hoge ͳͲ

    View full-size slide

  43. * MainActivity.java
    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    } MBZPVUBDUJWJUZ@NBJOYNMΛදࣔ
    3Ϋϥε

    View full-size slide

  44. BQQ@OBNFͱఆٛͨ͠จࣈྻΛऔಘ
    3Ϋϥε
    String appName = context.getString(R.string.app_name);
    ɾ values/strings.xml ʹఆٛ͞Ε͍ͯΔ

    ɾ Ϧιʔε͔Βऔಘ͢Δʹ͸ context ͕ඞཁʹͳΔ

    View full-size slide

  45. EQ
    w ϨΠΞ΢τͷαΠζʹ༻͍Δ୯Ґ
    w ୺຤ͷըૉ਺ʹ߹Θ͍ͤͯΔ
    w EQQYQYQYQYFUDʜ
    w ࠷ۙ͸QYఔ౓

    View full-size slide

  46. TQ
    w จࣈͷେ͖͞ͷ୯Ґ
    w ୺຤ͷϑΥϯτઃఆʹΑͬͯαΠζ͕มΘΔ

    View full-size slide

  47. جຊతͳଐੑ

    View full-size slide

  48. XJEUIIFJHIU
    w 7JFXͷ෯ɾߴ͞΋ଐੑͰࢦఆ͢Δ
    w ෯ android:width
    ɾߴ͞ android:height

    w ඞਢཁૉ

    View full-size slide

  49. XJEUIIFJHIU
    w ࢦఆʹ͸ࡾछྨ͋Δ
    w EQࢦఆ
    • match_parent
    w ਌ͷ7JFXҰഋ·Ͱ޿͕Δ
    • wrap_content
    w ඞཁͳ෼͚ͩ޿͕Δ

    View full-size slide

  50. QBEEJOHNBSHJO
    w ༨നΛઃఆ͢ΔͨΊͷଐੑ
    w શମ5PQ
    #PUUPN
    4UBSU

    &OE
    ͕ઃఆͰ͖Δ
    w ݹ͍୺຤Ͱ͸-FGU3JHIUʹͳΔ
    w ΞϥϏΞޠͳͲٯ޲͖ͷݴޠͰ΋ಉ͡ʹͳΔ

    View full-size slide

  51. QBEEJOHNBSHJO
    • android:padding (Top, Bottom…)
    w ཁૉͷ಺ଆͷ༨ന
    w എܠͳͲ͸༨നʹద༻͞ΕΔ
    w ΫϦοΫ͢ΔΤϦΞ΋޿͕Δ
    w layout_widthͳͲ͸paddingΛؚΉ

    View full-size slide

  52. QBEEJOHNBSHJO
    • android:layout_margin (Top…)
    w ཁૉͷ֎ଆͷ༨ന
    w എܠͳͲ͸༨നʹద༻͞Εͳ͍
    w ΫϦοΫ͢ΔΤϦΞͳͲʹؚ·Εͳ͍
    w layout_widthͳͲ͸paddingΛؚ·ͳ͍

    View full-size slide

  53. QBEEJOHNBSHJO
    Cite: https://www.washington.edu/accesscomputing/webd2/student/unit3/module4/lesson1.html

    View full-size slide

  54. 7JTJCJMJUZ
    • android:visibility
    • ཁૉͷදࣔɾඇදࣔઃఆ
    • VISIBLE: ݟ͑ΔʢσϑΥϧτʣ
    • INVISIBLE: ݟ͑ͳ͍͕εϖʔε͸औΔʢಁ໌ʣ
    • GONE: ݟ͑ͳ͍͠εϖʔε΋औΒͳ͍ʢফڈʣ

    View full-size slide

  55. MBZPVU@HSBWJUZ
    • android:layout_gravity
    w ਌ͷ7JFXʹରͯ͠Ͳͷ৔ॴʹஔ͔͘
    • hoge|piyo ͷΑ͏ʹॎԣͷҐஔΛࢦఆ
    • top / bottom / start / end /
    center_horizontal / center_vertical
    • LinearLayout / FrameLayout ޙड़
    Ͱ࢖͏

    View full-size slide

  56. 7JFX(SPVQ
    w ෳ਺ͷ7JFXΛ಺แͰ͖Δ7JFX
    w .*Layoutͱ͍͏໊લ͕ଟ͍
    w ·ͣ7JFX(SPVQΛઃஔ͠ɺͦͷதʹ7JFX
    ΛೖΕ͍ͯ͘

    View full-size slide

  57. 3FMBUJWF-BZPVU
    w ࢠ7JFXͷ഑ஔΛͦΕͧΕͷ7JFXͱͷ

    Ґஔؔ܎Λఆٛͯ͠࢖͏
    w ͜ͷ7JFXͷ্ʹɾ਌ͷ7JFXͷఈʹFUDʜ
    w Կ΋͠ͳ͍ͱࢠ7JFX͕ࠨ্ʹॏͳ͍ͬͯ͘
    w ΧελϜ͠΍͍͕͢࢖͍উख͸ѱ͍
    w ҰͭҰͭઃఆ͢Δඞཁ͕͋Δ

    View full-size slide

  58. 3FMBUJWF-BZPVU
    android:id=“@+id/activity_main" …>
    android:layout_alignParentTop=“true"
    android:id=“@id/piyo” …/>
    android:layout_below=“@+id/piyo” …/>

    View full-size slide

  59. -JOFBS-BZPVU
    w هड़͞Εͨॱ൪ʹࢠͷ7JFXΛฒ΂Δ
    w “android:orientation”Ͱ޲͖Λࢦఆ
    • vertical / horizontal σϑΥϧτ

    w ΧελϜੑ͸Լ͕Δ͕ศར

    View full-size slide

  60. -JOFBS-BZPVU
    android:orientation=“vertical” …>





    View full-size slide

  61. 'SBNF-BZPVU
    w ॏͶΔ͜ͱʹಛԽͨ͠ϨΠΞ΢τ
    w ࢠ7JFXಉ࢜ͷҐஔؔ܎͸ઃఆͰ͖ͳ͍
    w ਌ͷ7JFX͔ΒͷҐஔͷΈͰࢦఆ
    • android:layout_gravity Λ༻͍Δ
    w ୯७ʹॏͶΔ͚ͩͳΒRelativeΑΓγϯϓϧ

    View full-size slide

  62. 'SBNF-BZPVU

    android:layout_gravity=“top|start”
    …/>
    android:layout_gravity=“bottom|end”
    …/>

    View full-size slide

  63. FrameLayout
    LinearLayout

    (vertical)
    ※֤ TextView ͸ wrap_content, վߦ͸खಈͰૠೖ

    View full-size slide

  64. ͍Ζ͍Ζͳ7JFX

    View full-size slide

  65. ༷ʑͳ7JFX
    w TextViewจࣈΛදࣔ
    w EditTextจࣈೖྗཝ
    w ButtonϘλϯ
    w ImageViewը૾Λදࣔ
    w RecyclerViewϦετදࣔʢޙड़ʣ

    View full-size slide

  66. ͍Ζ͍Ζͳଐੑ
    w 7JFXʹΑ͍ͬͯΖ͍Ζͳଐੑ͕͋Δ
    w ୅දతͳ΋ͷΛ঺հ͠·͢

    View full-size slide

  67. ͍Ζ͍Ζͳଐੑ
    • android:text / textColor / textSize
    w จࣈྻɾจࣈ৭ɾจࣈαΠζΛࢦఆ
    • android:background
    w എܠͷ৭΍ը૾Λࢦఆ
    • android:hint
    w &EJU5FYUʹจࣈΛೖΕΔલͷώϯτΛઃఆ

    View full-size slide

  68. ͍Ζ͍Ζͳଐੑ
    • android:enabled
    w ϘλϯͰΫϦοΫͰ͖Δ͔Ͳ͏͔ͳͲ

    • ଞʹ΋͍Ζ͍Ζ͋Δ
    • ࣮͸ࣗ෼Ͱ΋࡞ΕΔ
    • ࢖͍͍ͨ࣌ʹௐ΂͍͖ͯ·͠ΐ͏

    View full-size slide