Androidアプリを作る 第6回
by
nonylene
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
"OESPJEΞϓϦΛ࡞Δ ,.$OPOZMFOF ը૾ؔɾ+BWB͔ΒͷϨΠΞτɾઃఆ
Slide 2
Slide 2 text
ࣗݾհ w OPOZMFOF ͷʹΕΜ w ,.$ճ w ΧʔϏΟͰ͢ w ϙέϞϯϐΫϩεপ w SPPU
Slide 3
Slide 3 text
ࣗݾհ w OPOZMFOF ͷʹΕΜ w εϚϗΞϓϦͱ͔ w 5XJUUFS(JU)VCFUD w IUUQOPOZMFOFIBUFOBCMPHKQ
Slide 4
Slide 4 text
͜ΜΜ
Slide 5
Slide 5 text
ࠓΔ͜ͱ ը૾ؔ *NBHF7JFX ϨΠΞτGSPN+BWB ઃఆอଘ
Slide 6
Slide 6 text
ը૾ؔ
Slide 7
Slide 7 text
*NBHF7JFX w Drawable Λදࣔ͢ΔͨΊͷView ௨ৗͷ View ͷ background ͰදࣔͰ͖Δ͕ɺେ͖͞มߋͳͲ͕Ͱ͖ͳ͍.
Slide 8
Slide 8 text
"OESPJEͷը૾ͷදݱ • Bitmap w ը૾Λද͢ • Drawable w ʮը໘ʹදࣔ͞ΕΔͷʯΛද͢ w ը૾͔͠Εͳ͍͠ɺͨͩͷ৭͔͠Εͳ͍ w ImageViewഎܠͰදࣔ͞ΕΔͷ͜Ε
Slide 9
Slide 9 text
"OESPJEͷը૾ͷදݱ "OJNBUJPO %SBXBCMF #JUNBQ $PMPS "OJNBUJPO #JUNBQ%SBXBCMF $PMPS%SBXBCMF %SBXBCMF $POWFSU
Slide 10
Slide 10 text
ը૾Λஔ • res/drawable/ ʹ Drawable Λઃஔ͢Δ w ࠓճը૾ 1/('JMF w 9.-Ͱهड़͢Δ͜ͱՄೳ w ϑΝΠϧΛίϐʔ͢ΔPS "OESPJE4UVEJPܦ༝Ͱίϐʔ͢Δ ղ૾ผʹ࠷దͳը૾Λ༻ҙ͢Δ͜ͱͰ͖Δ (drawable-hdpi ͳͲ)
Slide 11
Slide 11 text
ஔྫ
Slide 12
Slide 12 text
ը૾Λදࣔ • android:src • ImageView Ͱදࣔ͢Δ Drawable Λઃఆ
Slide 13
Slide 13 text
ը૾Λදࣔ
Slide 14
Slide 14 text
ը૾Λදࣔ w ิ w android:scaleTypeͰը૾ͷ֦େํ๏ΛબΔ w ৄࡉলུ
Slide 15
Slide 15 text
-BZPVU GSPN+BWB
Slide 16
Slide 16 text
+BWBͰͷϨΠΞτ w 9.-ʹॻ͍͍֤ͯͨView+BWBͷΫϥε • TextView -> android.widget.TextView • EditText -> android.widget.EditText • શͯ View ΫϥεͷࢠΫϥε XML Ͱ android.widget ʹ͋Δ View ಛผʹύοέʔδ໊ΛলུͰ͖͍ͯͨ. ಠࣗͷ View ͷ߹লུͰ͖ͳ͍.
Slide 17
Slide 17 text
7JFXͷຊ࣭+BWB w 7JFXͷॳظԽ7JFXͷίϯετϥΫλͰ։࠵ 9.-Λύʔεʢղੳʣ 7JFXͷॳظԽॲཧΛ࣮ߦ ɾ ྫandorid:text#setText(…)
Slide 18
Slide 18 text
7JFXΛऔಘ
Slide 19
Slide 19 text
7JFXΛऔಘ • View #findViewById(int id) w ActivityViewGroupʹੜ͑ͯΔ w ࢦఆͨ͠idͷViewΛऔಘ
Slide 20
Slide 20 text
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);
Slide 21
Slide 21 text
* 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Λऔಘ
Slide 22
Slide 22 text
* 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 ݻఆͳͷͰΩϟετ͢Δඞཁ͕͋Δ
Slide 23
Slide 23 text
7JFXΛ +BWB͔Βૢ࡞
Slide 24
Slide 24 text
7JFXΛૢ࡞ w 7JFXͷؔΛݺͿ͜ͱͰૢ࡞͕Մೳ editText.setHint(“KMC"); editText.setVisibility(View.GONE); imageView.setImageResource(R.drawable.kirby); // pixel ୯ҐͳͷͰҙ submitButton.setWidth(300); // ॻ͔Εͨจࣈྻ͕ฦͬͯ͘Δ editText.getText();
Slide 25
Slide 25 text
w ͓͢͢Ί͠·ͤΜ w දࣔ͢Δ͚ͩͷίʔυ͕͘ෳࡶʹͳΔ w ࣮ࡍͬͯΔਓ͍Δ͚Ͳʜ w දࣔͷઃఆͱڍಈͷઃఆ͚͍ͨ w 9.-ʹॻ͚Δ෦ॻ͘͜ͱ͕ଟ͍ શ෦+BWBͰॻ͚ศརʁ
Slide 26
Slide 26 text
ΫϦοΫ࣌ͷڍಈ
Slide 27
Slide 27 text
ΫϦοΫ࣌ͷڍಈ • View#setOnClickListener( OnClickListener listener) • ΫϦοΫ͞Εͨ࣌ʹݺΕΔϦεφΛઃఆ͢Δ • View.OnClickListener Λઃఆ • View.OnCliclListener ͱʁ
Slide 28
Slide 28 text
7JFX0O$MJDL-JTUFOFS w ϝιου͕Ұͭઃఆ͞ΕͨΠϯλʔϑΣʔε w ΫϦοΫ࣌ʹonClick͕ݺͼग़͞ΕΔ * View.java (Android SDK) public interface OnClickListener { void onClick(View v); }
Slide 29
Slide 29 text
ΫϦοΫͰτʔετ
Slide 30
Slide 30 text
* MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ
Slide 31
Slide 31 text
* 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ͷແ໊ΦϒδΣΫτ
Slide 32
Slide 32 text
* MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ ΫϦοΫ͢ΔͱݺΕΔؔ
Slide 33
Slide 33 text
* MainActivity.java submitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), “clicked!”, Toast.LENGTH_LONG ).show(); } } ); ϘλϯΫϦοΫͰτʔετɹ τʔετΛදࣔ
Slide 34
Slide 34 text
ϘλϯΫϦοΫͰτʔετɹ ˠ Click
Slide 35
Slide 35 text
ઃఆอଘ
Slide 36
Slide 36 text
ઃఆอଘ w ͍͔ͭ͘ͷํ๏͕͋Δ w 4IBSFE1SFGFSFODF w ࠓճΓ·͢ w 4RMJUF σʔλϕʔε w ֎෦ϥΠϒϥϦ
Slide 37
Slide 37 text
4IBSFE1SFGFSFODF w ઃఆͳͲΛอଘ͢ΔΈ w ΩʔόϦϡʔܗࣜͰอଘ w த9.- w อଘͰ͖ΔܕݶΒΕ͍ͯΔ • int, long, float, boolean, String, Set
Slide 38
Slide 38 text
4IBSFE1SFGFSFODF w ෳͷ9.-ϑΝΠϧʹอଘͰ͖Δ w ϑΝΠϧ͝ͱʹΩʔ໊Ұҙ w ࠓճσϑΥϧτͷ9.-ϑΝΠϧΛ༻ w ϑΝΠϧΛؒҧ͑ͯϋϚΒͳ͍Α͏ʹ w ؒҧ͑ͯެ։͞ΕΔ͜ͱ͕ͳ͍Α͏ʹ w ઃఆʹΑͬͯଞͷΞϓϦͰಡΈऔΕͯ͠·͏
Slide 39
Slide 39 text
4IBSFE1SFGFSFODF • σϑΥϧτͷ SharedPreferences Λऔಘ • ϑΝΠϧ͝ͱͷઃఆ (SharedPreference) Λཧ͢ΔΫϥε SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); Static ϝιου
Slide 40
Slide 40 text
4IBSFE1SFGFSFODFT w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true) .apply();
Slide 41
Slide 41 text
4IBSFE1SFGFSFODFT w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true) .apply(); 4IBSFE1SFGFSFOFT&EJUPSΛऔಘ
Slide 42
Slide 42 text
w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true) .apply(); 4IBSFE1SFGFSFODFT ΩʔͱΛ֨ೲ put~~~ ͷฦΓ editor ࣗͳͷͰ͜ͷΑ͏ʹॻ͚Δ ʢνΣʔϯϝιουͱ͍͏ʣ.
Slide 43
Slide 43 text
w อଘ sharedPreferences.edit() .putInt("hoge", 1) .putString("piyo", "piyoipyo") .putBoolean("foo", true) .apply(); 4IBSFE1SFGFSFODFT ઃఆΛө commit() ಉظతͰ͕͔͔࣌ؒΔͷͰඇਪ
Slide 44
Slide 44 text
4IBSFE1SFGFSFODFT w औΓग़͠ sharedPreferences.getInt("hoge", 0); sharedPreferences.getString("piyo", null); sharedPreferences.getBoolean("foo", false); ΩʔͱσϑΥϧτΛࢦఆ