Androidアプリを作る 第6回

9b6ca173883528d3bd047ca8f20b0bce?s=47 nonylene
December 26, 2016

Androidアプリを作る 第6回

9b6ca173883528d3bd047ca8f20b0bce?s=128

nonylene

December 26, 2016
Tweet

Transcript

  1. "OESPJEΞϓϦΛ࡞Δ  ,.$OPOZMFOF ը૾ؔ܎ɾ+BWB͔ΒͷϨΠΞ΢τɾઃఆ஋

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

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

    IUUQOPOZMFOFIBUFOBCMPHKQ
  4. ͜Μ͹Μ͸

  5. ࠓ೔΍Δ͜ͱ  ը૾ؔ܎ *NBHF7JFX   ϨΠΞ΢τGSPN+BWB  ઃఆ஋อଘ

  6. ը૾ؔ܎

  7. *NBHF7JFX w Drawable Λදࣔ͢ΔͨΊͷView ௨ৗͷ View ͷ background Ͱ΋දࣔͰ͖Δ͕ɺେ͖͞มߋͳͲ͕Ͱ͖ͳ͍.

  8. "OESPJEͷը૾ͷදݱ • Bitmap w ը૾Λද͢ • Drawable w ʮը໘ʹදࣔ͞ΕΔ΋ͷʯΛද͢ w

    ը૾͔΋͠Εͳ͍͠ɺͨͩͷ৭͔΋͠Εͳ͍ w ImageView΍എܠͰදࣔ͞ΕΔͷ͸͜Ε
  9. "OESPJEͷը૾ͷදݱ "OJNBUJPO
 %SBXBCMF #JUNBQ $PMPS "OJNBUJPO #JUNBQ%SBXBCMF $PMPS%SBXBCMF %SBXBCMF $POWFSU

  10. ը૾Λ഑ஔ • res/drawable/ ʹ Drawable Λઃஔ͢Δ w ࠓճ͸ը૾ 1/('JMF 

    w 9.-Ͱهड़͢Δ͜ͱ΋Մೳ w ௚઀ϑΝΠϧΛίϐʔ͢ΔPS
 "OESPJE4UVEJPܦ༝Ͱίϐʔ͢Δ ղ૾౓ผʹ࠷దͳը૾Λ༻ҙ͢Δ͜ͱ΋Ͱ͖Δ (drawable-hdpi ͳͲ)
  11. ഑ஔྫ

  12. ը૾Λදࣔ • android:src • ImageView Ͱදࣔ͢Δ Drawable Λઃఆ <ImageView android:layout_width="100dp"

    android:layout_height="100dp" android:src="@drawable/kirby" />
  13. ը૾Λදࣔ

  14. ը૾Λදࣔ w ิ଍ w android:scaleTypeͰը૾ͷ֦େํ๏Λબ΂Δ w ৄࡉ͸লུ

  15. -BZPVU
 GSPN+BWB

  16. +BWBͰͷϨΠΞ΢τ w 9.-ʹॻ͍͍֤ͯͨView͸+BWBͷΫϥε • TextView -> android.widget.TextView • EditText ->

    android.widget.EditText
 • શͯ View ΫϥεͷࢠΫϥε XML Ͱ͸ android.widget ʹ͋Δ View ͸ಛผʹύοέʔδ໊ΛলུͰ͖͍ͯͨ.
 ಠࣗͷ View ͷ৔߹লུͰ͖ͳ͍.
  17. 7JFXͷຊ࣭͸+BWB w 7JFXͷॳظԽ͸7JFXͷίϯετϥΫλͰ։࠵ 9.-Λύʔεʢղੳʣ 7JFXͷॳظԽॲཧΛ࣮ߦ ɾ ྫandorid:text#setText(…)

  18. 7JFXΛऔಘ

  19. 7JFXΛऔಘ • View #findViewById(int id) w Activity΍ViewGroupʹੜ͑ͯΔ w ࢦఆͨ͠idͷViewΛऔಘ

  20. 7JFXΛऔಘ * MainActivity.java // setContentView લʹߦ͏ͱ View ͕औಘͰ͖ͳ͍ setContentView(R.layout.activity_main); EditText

    editText = (EditText) findViewById(R.id.post_edit); Button submitButton = (Button) findViewById(R.id.post_button);
  21. * MainActivity.java // setContentView લʹߦ͏ͱ View ͕औಘͰ͖ͳ͍ setContentView(R.layout.activity_main); EditText editText

    = (EditText) findViewById(R.id.post_edit); Button submitButton = (Button) findViewById(R.id.post_button); 7JFXΛऔಘ *%͕FEJU@UFYUͷ7JFXΛऔಘ
  22. * MainActivity.java // setContentView લʹߦ͏ͱ View ͕औಘͰ͖ͳ͍ setContentView(R.layout.activity_main); EditText editText

    = (EditText) findViewById(R.id.post_edit); Button submitButton = (Button) findViewById(R.id.post_button); 7JFXΛऔಘ &EJU5FYUʹΩϟετͯ͠୅ೖ findViewById ͷฦΓ஋͸ View ݻఆͳͷͰΩϟετ͢Δඞཁ͕͋Δ
  23. 7JFXΛ
 +BWB͔Βૢ࡞

  24. 7JFXΛૢ࡞ w 7JFXͷؔ਺ΛݺͿ͜ͱͰૢ࡞͕Մೳ editText.setHint(“KMC"); editText.setVisibility(View.GONE); imageView.setImageResource(R.drawable.kirby); // pixel ୯ҐͳͷͰ஫ҙ submitButton.setWidth(300);

    // ॻ͔Εͨจࣈྻ͕ฦͬͯ͘Δ editText.getText();
  25. w ͓͢͢Ί͠·ͤΜ w දࣔ͢Δ͚ͩͷίʔυ͕௕͘ෳࡶʹͳΔ w ࣮ࡍ΍ͬͯΔਓ΋͍Δ͚Ͳʜ w දࣔͷઃఆͱڍಈͷઃఆ͸෼͚͍ͨ
 w 9.-ʹॻ͚Δ෦෼͸ॻ͘͜ͱ͕ଟ͍

    શ෦+BWBͰॻ͚͹ศརʁ
  26. ΫϦοΫ࣌ͷڍಈ

  27. ΫϦοΫ࣌ͷڍಈ • View#setOnClickListener(
 OnClickListener listener) • ΫϦοΫ͞Εͨ࣌ʹݺ͹ΕΔϦεφΛઃఆ͢Δ • View.OnClickListener Λઃఆ


    • View.OnCliclListener ͱ͸ʁ
  28. 7JFX0O$MJDL-JTUFOFS w ϝιου͕Ұͭઃఆ͞ΕͨΠϯλʔϑΣʔε w ΫϦοΫ࣌ʹonClick͕ݺͼग़͞ΕΔ * View.java (Android SDK) public

    interface OnClickListener { void onClick(View v); }
  29. ΫϦοΫͰτʔετ

  30. * MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View

    v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ
  31. * MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View

    v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ 0O$MJDL-JTUFOFSͷແ໊ΦϒδΣΫτ
  32. * MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View

    v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ ΫϦοΫ͢Δͱݺ͹ΕΔؔ਺
  33. * MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View

    v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ τʔετΛදࣔ
  34. ϘλϯΫϦοΫͰτʔετɹ ˠ Click

  35. ઃఆ஋อଘ

  36. ઃఆ஋อଘ w ͍͔ͭ͘ͷํ๏͕͋Δ w 4IBSFE1SFGFSFODF w ࠓճ΍Γ·͢ w 4RMJUF σʔλϕʔε

     w ֎෦ϥΠϒϥϦ
  37. 4IBSFE1SFGFSFODF w ઃఆ஋ͳͲΛอଘ͢Δ࢓૊Έ w ΩʔόϦϡʔܗࣜͰอଘ w த਎͸9.- w อଘͰ͖Δܕ͸ݶΒΕ͍ͯΔ •

    int, long, float, boolean, String, Set<String>
  38. 4IBSFE1SFGFSFODF w ෳ਺ͷ9.-ϑΝΠϧʹอଘͰ͖Δ w ϑΝΠϧ͝ͱʹΩʔ໊͸Ұҙ w ࠓճ͸σϑΥϧτͷ9.-ϑΝΠϧΛ࢖༻ w ϑΝΠϧΛؒҧ͑ͯϋϚΒͳ͍Α͏ʹ w

    ؒҧ͑ͯެ։͞ΕΔ͜ͱ͕ͳ͍Α͏ʹ w ઃఆʹΑͬͯ͸ଞͷΞϓϦͰ΋ಡΈऔΕͯ͠·͏
  39. 4IBSFE1SFGFSFODF • σϑΥϧτͷ SharedPreferences Λऔಘ • ϑΝΠϧ͝ͱͷઃఆ (SharedPreference) 
 Λ؅ཧ͢ΔΫϥε

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); Static ϝιου
  40. 4IBSFE1SFGFSFODFT w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true)

    .apply();
  41. 4IBSFE1SFGFSFODFT w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true)

    .apply(); 4IBSFE1SFGFSFOFT&EJUPSΛऔಘ
  42. w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true) .apply();

    4IBSFE1SFGFSFODFT Ωʔͱ஋Λ֨ೲ put~~~ ͷฦΓ஋͸ editor ࣗ਎ͳͷͰ͜ͷΑ͏ʹॻ͚Δ ʢνΣʔϯϝιουͱ͍͏ʣ.
  43. w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true) .apply();

    4IBSFE1SFGFSFODFT ઃఆΛ൓ө commit() ͸ಉظతͰ͕͔͔࣌ؒΔͷͰඇਪ঑
  44. 4IBSFE1SFGFSFODFT w औΓग़͠ sharedPreferences.getInt("hoge", 0); sharedPreferences.getString("piyo", null); sharedPreferences.getBoolean("foo", false); ΩʔͱσϑΥϧτ஋Λࢦఆ