Androidアプリを作る 第5回

9b6ca173883528d3bd047ca8f20b0bce?s=47 nonylene
December 13, 2016

Androidアプリを作る 第5回

9b6ca173883528d3bd047ca8f20b0bce?s=128

nonylene

December 13, 2016
Tweet

Transcript

  1. "OESPJEΞϓϦΛ࡞Δ  ,.$OPOZMFOF "DUJWJUZϨΠΞ΢τ

  2. ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ  w ,.$ճ w ΧʔϏΟͰ͢ w

    ϙέϞϯϐΫϩεྑ͍Ͱ͢ w SPPU
  3. ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ  w εϚϗΞϓϦͱ͔ w 5XJUUFS(JU)VCFUD w

    IUUQOPOZMFOFIBUFOBCMPHKQ
  4. ͜Μ͹Μ͸

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

  6. "DUJWJUZ

  7. ֓આ

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

  9. "DUJWJUZͷྫ

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

    ͋Ε΋͜Ε΋"DUJWJUZ
  11. "DUJWJUZͷߏ଄

  12. * MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected

    void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } "DUJWJUZ
  13. w 4VQQPSU-JCSBSZ w "OESPJE4%,ͷ࠷৽ͷػೳ͕Ұ෦ೖ͍ͬͯΔ w ͜ΕͰݹ͍୺຤ʹ΋࠷৽ͷػೳ͕ʂ • .*Compat.* • .*Support.*

    • ࠓճ͸ AppCompatActivity Λ࢖͏ ิ଍
  14. * MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected

    void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } ىಈ͢Δ࣌ʹݺ͹ΕΔ ίϯετϥΫλͰ Activity ؔ࿈ͷॳظԽΛ͠Α͏ͱ͢Δͱ·ͩॳظԽ͕ࡁΜͰͳ͍͜ͱ͕͋Δ
  15. w onCreate()ىಈʢॳظԽॲཧʣ w onStart() ࢝·Δ w ྫىಈ͢ΔɾStopޙ·ͨલ໘ʹ໭ͬͨ w onStop()ࢭ·ͬͨ w

    ྫऴྃ͢Δɾଞͷ"DUJWJUZ͕લ໘ʹग़ͨ w onDestroy() ऴྃ͢Δʢޙย෇͚ʣ ϥΠϑαΠΫϧ
  16. w ଞʹ΋͍Ζ͍Ζ͋Δ w IUUQTEFWFMPQFSBOESPJEDPNSFGFSFODF BOESPJEBQQ"DUJWJUZIUNM"DUJWJUZ-JGFDZDMF ϥΠϑαΠΫϧ

  17. * 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 ʹؔͯ͠͸ޙड़
  18. * AndroidManifest.xml <?xml versi…?> <manifest package=“net.nonylene.myslack" …> <application …> <activity

    android:name=“.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> "OESPJE.BOJGFTUYNM
  19. ৽͍͠"DUJWJUZΛ࡞੒ w l1SPKFDUzͷlBQQzҎԼͰӈΫϦοΫ ˠ l/FXzl"DUJWJUZzl&NQUZ"DUJWJUZz

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

  21. *OUFOU

  22. *OUFOU w ଞͷ"DUJWJUZΛىಈ͢ΔͨΊͷΫϥε w "DUJWJUZಉ࢜Ͱ͸ϝϯόม਺ͳͲʹ
 ௚઀ΞΫηε͠ͳ͍ ˠ ىಈΛࢦࣔ͢Δ*OUFOUʹσʔλΛೖΕͯ౉͢
 w ଞͷ࢖༻๏΋͋Δ

    ྫ4FSWJDF ׂ͕Ѫ
  23. *OUFOU w ໌ࣔత*OUFOU w ىಈ͢Δ"DUJWJUZΛࢦఆ͢Δ w ଞͷը໘ʹભҠ͍ͨ࣌͠ͳͲʹ࢖͏

  24. ໌ࣔత*OUFOU * MainActivity.java Intent intent = new Intent(this, Main2Activity.class); intent.putExtra("name",

    “nonylene"); startActivity(intent); .BJO"DUJWJUZΛىಈ this ͸ Context Ͱɺ͜͜Ͱ͸ MainActivity ࣗ਎ΛೖΕ͍ͯΔ. 
 (Activity ͸ Context ͷࢠΫϥε) Context ͸ΞϓϦͷ؀ڥ৘ใͳͲΛऔಘ͢Δͷʹ࢖ΘΕ͍ͯΔ.
  25. * MainActivity.java Intent intent = new Intent(this, Main2Activity.class); intent.putExtra("name", “nonylene");

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

    startActivity(intent); ໌ࣔత*OUFOU *OUFOUΛ౤͛Δ .BJO"DUJWJUZ͕ىಈ
  27. *OUFOU w ҉໧త*OUFOU w ʮڞ༗ʯͱݴΘΕ͍ͯΔ΋ͷ w ςΩετΛଞͷΞϓϦͰॲཧ͢Δ౳

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

    w ىಈ͢Δ"DUJWJUZΛϢʔβʔ͕બͿ
  29. ҉໧త*OUFOU * MainActivity.java Uri uri = Uri.parse(“https://www.kmc.gr.jp/"); Intent intent =

    new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); 6SJΦϒδΣΫτΛ࡞੒
  30. ҉໧త*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Λࢦఆ
  31. ҉໧త*OUFOU *OUFOUΛ౤͛Δ ΞϓϦબ୒ը໘͕ىಈ * MainActivity.java Uri uri = Uri.parse(“https://www.kmc.gr.jp/"); Intent

    intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
  32. ҉໧త*OUFOU →

  33. ϨΠΞ΢τ

  34. ֓આ

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

  36. งғؾ * activity_main.xml <?xml version="1.0" encoding=“utf-8"?> <RelativeLayout 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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> </RelativeLayout>
  37. งғؾ * activity_main.xml <?xml version="1.0" encoding=“utf-8"?> <RelativeLayout 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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> </RelativeLayout> શମͷϨΠΞ΢τ
  38. งғؾ * activity_main.xml <?xml version="1.0" encoding=“utf-8"?> <RelativeLayout 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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> </RelativeLayout> 5FYUදࣔ
  39. งғؾ

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

  41. "UUSJCVUFT ଐੑ w 9.-ͷه๏ w ཁૉʹରͯ͠ଐੑͷ৘ใΛՃ͑Δ w "OESPJEͰ͸“android:”͔Β࢝ΊΔ w l໊લۭؒzͱ͍͏

    w *%&Ͱ࢖͏BUUSCVUFT͸“tools:” w 4VQQPSU-JCSBSZͰ͸“app:” ͕ଟ͍
  42. * activity_main.xml <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" tools:context="net.nonylene.myslack.MainActivity" …> <TextView

    android:text="Hello World!" …/> </RelativeLayout> BOESPJEͱUPPMTΛఆٛ "UUSJCVUFT ଐੑ
  43. * activity_main.xml <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" tools:context="net.nonylene.myslack.MainActivity" …> <TextView

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

    <TextView android:text="Hello World!" …/> </RelativeLayout> "UUSJCVUFT ଐੑ
  45. *% w 7JFXΛଞͷ৔ॴ +BWB΍ଞͷ7JFX ͔Βࢀ র͢ΔͨΊͷ*% w ֤ϨΠΞ΢τ͝ͱʹҰҙ

  46. *% * activity_main.xml <RelativeLayout android:id="@+id/activity_main" …> <TextView android:id="@+id/text_view" android:layout_alignParentTop="@id/activity_main" …/>

    </RelativeLayout> ϨΠΞ΢τ*%ͷఆٛ ! JE
  47. *% * activity_main.xml <RelativeLayout android:id="@+id/activity_main" …> <TextView android:id="@+id/text_view" android:layout_alignParentTop="@id/activity_main" …/>

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

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


    • ͦͷଞɺR.string.hoge ΍ R.dimen.hoge ͳͲ
  50. * MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected

    void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } MBZPVUBDUJWJUZ@NBJOYNMΛදࣔ 3Ϋϥε
  51. BQQ@OBNFͱఆٛͨ͠จࣈྻΛऔಘ 3Ϋϥε String appName = context.getString(R.string.app_name); ɾ values/strings.xml ʹఆٛ͞Ε͍ͯΔ
 ɾ

    Ϧιʔε͔Βऔಘ͢Δʹ͸ context ͕ඞཁʹͳΔ
  52. EQ w ϨΠΞ΢τͷαΠζʹ༻͍Δ୯Ґ w ୺຤ͷըૉ਺ʹ߹Θ͍ͤͯΔ w EQQYQYQYQYFUDʜ w ࠷ۙ͸QYఔ౓

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

  54. جຊతͳଐੑ

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

    ඞਢཁૉ
  56. XJEUIIFJHIU w ࢦఆʹ͸ࡾछྨ͋Δ w EQࢦఆ • match_parent w ਌ͷ7JFXҰഋ·Ͱ޿͕Δ •

    wrap_content w ඞཁͳ෼͚ͩ޿͕Δ
  57. XJEUIIFJHIU

  58. QBEEJOHNBSHJO w ༨നΛઃఆ͢ΔͨΊͷଐੑ w શମ5PQ #PUUPN 4UBSU  &OE ͕ઃఆͰ͖Δ

    w ݹ͍୺຤Ͱ͸-FGU3JHIUʹͳΔ w ΞϥϏΞޠͳͲٯ޲͖ͷݴޠͰ΋ಉ͡ʹͳΔ
  59. QBEEJOHNBSHJO • android:padding (Top, Bottom…) w ཁૉͷ಺ଆͷ༨ന w എܠͳͲ͸༨നʹద༻͞ΕΔ w

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

    w layout_widthͳͲ͸paddingΛؚ·ͳ͍
  61. QBEEJOHNBSHJO Cite: https://www.washington.edu/accesscomputing/webd2/student/unit3/module4/lesson1.html

  62. 7JTJCJMJUZ • android:visibility • ཁૉͷදࣔɾඇදࣔઃఆ • VISIBLE: ݟ͑ΔʢσϑΥϧτʣ • INVISIBLE:

    ݟ͑ͳ͍͕εϖʔε͸औΔʢಁ໌ʣ • GONE: ݟ͑ͳ͍͠εϖʔε΋औΒͳ͍ʢফڈʣ
  63. MBZPVU@HSBWJUZ • android:layout_gravity w ਌ͷ7JFXʹରͯ͠Ͳͷ৔ॴʹஔ͔͘ • hoge|piyo ͷΑ͏ʹॎԣͷҐஔΛࢦఆ • top

    / bottom / start / end / center_horizontal / center_vertical • LinearLayout / FrameLayout ޙड़ Ͱ࢖͏
  64. 7JFX(SPVQ

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

  66. 3FMBUJWF-BZPVU w ࢠ7JFXͷ഑ஔΛͦΕͧΕͷ7JFXͱͷ
 Ґஔؔ܎Λఆٛͯ͠࢖͏ w ͜ͷ7JFXͷ্ʹɾ਌ͷ7JFXͷఈʹFUDʜ w Կ΋͠ͳ͍ͱࢠ7JFX͕ࠨ্ʹॏͳ͍ͬͯ͘ w ΧελϜ͠΍͍͕͢࢖͍উख͸ѱ͍

    w ҰͭҰͭઃఆ͢Δඞཁ͕͋Δ
  67. 3FMBUJWF-BZPVU <RelativeLayout android:id=“@+id/activity_main" …> <TextView android:layout_alignParentTop=“true" android:id=“@id/piyo” …/> <TextView android:layout_below=“@+id/piyo”

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

     w ΧελϜੑ͸Լ͕Δ͕ศར
  69. -JOFBS-BZPVU <LinearLayout android:orientation=“vertical” …> <TextView …/> <TextView …/> <ImageView …/>

    <LinearLayout …/> </LinearLayout>
  70. 'SBNF-BZPVU w ॏͶΔ͜ͱʹಛԽͨ͠ϨΠΞ΢τ w ࢠ7JFXಉ࢜ͷҐஔؔ܎͸ઃఆͰ͖ͳ͍ w ਌ͷ7JFX͔ΒͷҐஔͷΈͰࢦఆ • android:layout_gravity Λ༻͍Δ

    w ୯७ʹॏͶΔ͚ͩͳΒRelativeΑΓγϯϓϧ
  71. 'SBNF-BZPVU <FrameLayout …> <TextView android:layout_gravity=“top|start” …/> <RelativeLayout android:layout_gravity=“bottom|end” …/> </FrameLayout>

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

  73. ͍Ζ͍Ζͳ7JFX

  74. ༷ʑͳ7JFX w TextViewจࣈΛදࣔ w EditTextจࣈೖྗཝ w ButtonϘλϯ w ImageViewը૾Λදࣔ w

    RecyclerViewϦετදࣔʢޙड़ʣ
  75. ͍Ζ͍Ζͳଐੑ w 7JFXʹΑ͍ͬͯΖ͍Ζͳଐੑ͕͋Δ w ୅දతͳ΋ͷΛ঺հ͠·͢

  76. ͍Ζ͍Ζͳଐੑ • android:text / textColor / textSize w จࣈྻɾจࣈ৭ɾจࣈαΠζΛࢦఆ •

    android:background w എܠͷ৭΍ը૾Λࢦఆ • android:hint w &EJU5FYUʹจࣈΛೖΕΔલͷώϯτΛઃఆ
  77. ͍Ζ͍Ζͳଐੑ • android:enabled w ϘλϯͰΫϦοΫͰ͖Δ͔Ͳ͏͔ͳͲ
 • ଞʹ΋͍Ζ͍Ζ͋Δ • ࣮͸ࣗ෼Ͱ΋࡞ΕΔ •

    ࢖͍͍ͨ࣌ʹௐ΂͍͖ͯ·͠ΐ͏