LECTURE 2 ʸ»úɽ¼¨¤À¤±¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó (Activity/TextView)

¡û ¤Ï¤¸¤á¤Ë

º£²ó¤Ï¡¢½é¤á¤Æ¤Î Android ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤òºîÀ®¤·¤¿¤¤¤È»×¤¤¤Þ¤¹¡£¤È¤¤¤Ã¤Æ¤â¡¢android create project ¥³¥Þ¥ó¥É¤Ç¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Æ¡¢²¿¤â¼ê¤òÆþ¤ì¤º¤Ë¥Ó¥ë¥É¤¹¤ë¤À¤±¤Ç¤¹¤¬¡Ä

¥Ç¥Õ¥©¥ë¥È¤ÇºîÀ®¤µ¤ì¤ë¥Õ¥¡¥¤¥ë¤òį¤á¤Æ¡¢Android ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÎÊ·°Ïµ¤¤òÄϤߤޤ·¤ç¤¦¡ª

¡û android create project ¥³¥Þ¥ó¥É¤Ç¥×¥í¥¸¥§¥¯¥ÈºîÀ®

SDK ¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ë¤ÈÍøÍѲÄǽ¤È¤Ê¤ë android create project ¥³¥Þ¥ó¥É¤ò»È¤Ã¤Æ¡¢¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤¹¡£LECTURE 1 ³«È¯´Ä¶­¤òÍÑ°Õ¤¹¤ë ¤Ç½ñ¤¤¤¿Íͤˡ¢android create project ¥³¥Þ¥ó¥É¤Ï²¼µ­¤ÎÍͤˤ·¤Æ»È¤¤¤Þ¤¹¡£

android create project --target <target-id> --name ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾ \
  --path ¥×¥í¥¸¥§¥¯¥È¥Õ¥¡¥¤¥ëºîÀ®¥Ñ¥¹ --activity Activity̾ --package Activity¤¬Â°¤¹¤ë¥Ñ¥Ã¥±¡¼¥¸Ì¾

target-id ¤Ë»ØÄê²Äǽ¤ÊÃÍ¤Ï android list targets ¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤·¤Æɽ¼¨¤µ¤ì¤ëÃͤΤ¤¤º¤ì¤«¤ò»ØÄꤷ¤Þ¤¹¡£¼«Ê¬¤Î´Ä¶­(Linux¾å)¤Ç¤Ï²¼µ­¤ÎÍͤËɽ¼¨¤µ¤ì¤Þ¤¹¡£

android list targets
Available Android targets:
  ----------
  id: 1 or "android-17"
       Name: Android 4.2.2
       Type: Platform
       API level: 17
       Revision: 2
       Skins: WVGA800 (default), WXGA720, WXGA800-7in, WSVGA, HVGA, WQVGA400, WQVGA432, WXGA800, WVGA854, QVGA
       ABIs : armeabi-v7a

android create project ¥³¥Þ¥ó¥É¤Î target-id ¤Ë¤Ï¡¢ËÜ¥µ¥¤¥È¤Ç¤Ï¾åµ­¤Î 1 ¤ò»ØÄꤹ¤ë¤³¤È¤Ë¤·¤Þ¤¹¡£

¤½¤Î¾¡¢ËÜ¥µ¥ó¥×¥ë¤Ç¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾¤ò FirstApp¡¢Activity¡ÊUI ¤ò»ý¤Ä Android ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î¥á¥¤¥ó¥¯¥é¥¹¡Ë̾¤ò MainActivity¡¢¥Ñ¥Ã¥±¡¼¥¸Ì¾¤ò riolab.sample ¤È¤·¤Æ¡¢¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤·¤¿¡£

android create project --target 1 --name FirstApp --path . --activity MainActivity --package riolab.sample

¼Â¹Ô¤¬½ª¤ï¤ë¤È¡¢¸½ºßºî¶È¤·¤Æ¤¤¤ë¥Ç¥£¥ì¥¯¥È¥ê¤Ë°Ê²¼¤Î¥Õ¥¡¥¤¥ë¥Ä¥ê¡¼¤¬ºîÀ®¤µ¤ì¤Æ¤¤¤ë¤È»×¤¤¤Þ¤¹¡£

  • ºî¶È¾ì½ê/
    • AndroidManifest.xml
    • ant.properties
    • bin/
    • build.xml
    • libs/
    • local.properties
    • proguard-project.txt
    • project.properties
    • res/
      • drawable-hdpi/ic_launcher.png
      • drawable-ldpi/ic_launcher.png
      • drawable-mdpi/ic_launcher.png
      • drawable-xhdpi/ic_launcher.png
      • layout/main.xml
      • values/strings.xml
    • src/riolab/sample/MainActivity.java

¡û ¥Ó¥ë¥É¤ª¤è¤Ó¥¨¥ß¥å¥ì¡¼¥¿¤Ç¼Â¹Ô

ºîÀ®¤·¤¿¥×¥í¥¸¥§¥¯¥È¤Î¥È¥Ã¥×¥Ç¥£¥ì¥¯¥È¥ê¡Êbuild.xml¤¬¤¢¤ë¾ì½ê¡Ë¤Ë°ÜÆ°¤·¡¢ant debug ¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£bin °Ê²¼¤Ë FirstApp-debug.apk ¤¬ºîÀ®¤µ¤ì¤Þ¤¹¡£¤³¤ì¤¬¡¢¥¨¥ß¥å¥ì¡¼¥¿¤ËžÁ÷¤¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥Ñ¥Ã¥±¡¼¥¸¥Õ¥¡¥¤¥ë¤Ç¤¹¡£LECTURE 1 ³«È¯´Ä¶­¤òÍÑ°Õ¤¹¤ë ¤Ë½¾¤Ã¤Æ¥¨¥ß¥å¥ì¡¼¥¿¤òµ¯Æ°¤·¤¿¾õÂ֤ǡ¢°Ê²¼¤ÎÍͤˤ·¤Æ¥¨¥ß¥å¥ì¡¼¥¿¤ËžÁ÷¤·¤Þ¤¹¡£

adb install bin/FirstApp-debug.apk

¤½¤ì¤Ç¤Ï¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£

HOME ²èÌ̲¼Éô¤ÎÀִݤǰϤó¤ÀÉôʬ¤ò¥¯¥ê¥Ã¥¯
¥¢¥×¥ê¥±¡¼¥·¥ç¥ó°ìÍ÷¤òÁªÂò

¥¢¥×¥ê¥±¡¼¥·¥ç¥ó°ìÍ÷²èÌÌ¡ÊMainActivity ¤¬ android create project ¥³¥Þ¥ó¥É¤Çºî¤Ã¤¿¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¡Ë
¥¢¥×¥ê¥±¡¼¥·¥ç¥ó°ìÍ÷²èÌÌ

FirstApp ¤Î¼Â¹Ô²èÌÌ
FirstApp ¤Î¼Â¹Ô²èÌÌ

¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÈÀâÌÀ

¤³¤³¤Þ¤Ç¤ÇºîÀ®¤·¤¿¥×¥í¥¸¥§¥¯¥ÈÆâ¤Î¥Õ¥¡¥¤¥ë¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£002firstapp.zip ¤«¤é¥À¥¦¥ó¥í¡¼¥É¤Ç¤­¤Þ¤¹¡£¤Þ¤¢¡¢¥À¥¦¥ó¥í¡¼¥É¤¹¤ëɬÍפϤʤ¤¤È»×¤¤¤Þ¤¹¤¬¡Ä¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò ¸Ç¤á¤¿ zip ¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£

¤Ê¤ª¡¢¥½¡¼¥¹¥³¡¼¥É¤Ï SDK Tools 22.0.4 / ¥×¥é¥Ã¥È¥Õ¥©¡¼¥à Android 4.2 ¤Ç¥³¥ó¥Ñ¥¤¥ë²Äǽ¤Ç¤¹¡£local.properties Æâ¤Ë sdk.dir ÊÑ¿ô¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤Þ¤¹¤¬¡¢¤³¤³¤Ï´Ä¶­°Í¸¤Ë¤Ê¤ê¤Þ¤¹¤Î¤Ç¡¢¤ª»È¤¤¤Î´Ä¶­¤Ë¹ç¤ï¤»¤ÆÊѹ¹¤·¤Æ¤¯¤À¤µ¤¤¡£

  • MainActivity.java
  • main.xml
  • strings.xml

¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Þ¤¹¡£Â¾¤Î¥Õ¥¡¥¤¥ë¤Ë¤Ä¤¤¤Æ¤Ï¡¢º£¸åÊÔ½¸¤¹¤ëɬÍפ¬À¸¤¸¤¿ºÝ¤ËÀâÌÀ¤¹¤ë¤Ä¤â¤ê¤Ç¤¹¡£

  • MainActivity.java
        1	package riolab.sample;
        2	
        3	import android.app.Activity;
        4	import android.os.Bundle;
        5	
        6	public class MainActivity extends Activity
        7	{
        8	    /** Called when the activity is first created. */
        9	    @Override
       10	    public void onCreate(Bundle savedInstanceState)
       11	    {
       12	        super.onCreate(savedInstanceState);
       13	        setContentView(R.layout.main);
       14	    }
       15	}
    
    

    ËÜ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î¥á¥¤¥ó¥¯¥é¥¹¤Ç¤¹¡£

    • 6 ¹ÔÌÜ

      android.app.Activity ¤ò·Ñ¾µ¤·¤Æ¤¤¤Þ¤¹¡£Activity ¤Ï¥æ¡¼¥¶¤¬¼Â¹Ô¤Ç¤­¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤òɽ¤·¤Æ¤¤¤Þ¤¹¡£Ê£»¨¤Ê¥é¥¤¥Õ¥µ¥¤¥¯¥ë¤ò»ý¤Á¡¢UI ¥¤¥Ù¥ó¥È¤ò½èÍý¤·¤¿¤êÊ£»¨¤Ê½èÍý¤ò¹Ô¤Ã¤¿¤ê¤·¤Þ¤¹¡£android.view.Window ¥¯¥é¥¹¤ò»È¤Ã¤Æ²èÌ̤òÉÁ²è¤·¤Þ¤¹¡Ê¼ÂºÝ¤Ë¤Ï¡¢²èÌ̾å¤Î¶ë·ÁÎΰè¤òɽ¤¹ android.view.View ¥¯¥é¥¹¤¬¡¢¼«Ê¬¤¬Ã´Åö¤¹¤ë¶ë·ÁÎΰè¤ÎÉÁ²è¤ò¹Ô¤¤¤Þ¤¹¡Ë¡£

    • 8 ¡Á 14 ¹ÔÌÜ (onCreate())

      Activity ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤¬À¸À®¤µ¤ì¤ëºÝ¤Ë¡¢É¬¤º¼Â¹Ô¤µ¤ì¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£UI ¤Î¹½ÃÛÅù¡¢°ìÅÙ¤·¤«¹Ô¤ï¤Ê¤¤½é´ü½èÍý¤ò¤³¤³¤Ç¹Ô¤¤¤Þ¤¹¡£º£¸å¡¢Activity ¤Î¥é¥¤¥Õ¥µ¥¤¥¯¥ë¤Ë¤Ä¤¤¤ÆÀâÌÀ¤¹¤ëºÝ¤ËºÆÅÙÀâÌÀ¤·¤¿¤¤¤È»×¤¤¤Þ¤¹¡£

      Activity#setContentView() ¤Ï¡¢²èÌ̾å¤Î¶ë·ÁÎΰè¤òÉÁ²è¤¹¤ë View¡Ê¤â¤·¤¯¤Ï¥µ¥Ö¥¯¥é¥¹¡Ë¤ò»ØÄꤹ¤ë¥á¥½¥Ã¥É¤Ç¤¹¡£

      public void Activity#setContentView(int layoutResID);
      public void Activity#setContentView(android.view.View view);
      public void Activity#setContentView(
        android.view.View view, android.view.ViewGroup.LayoutParams params);
      
      

      View ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òľÀÜ»ØÄꤹ¤ë¾¤Ë¡¢¥ê¥½¡¼¥¹ID ¤ò»ØÄꤹ¤ë¤³¤È¤Ç UI ¥ì¥¤¥¢¥¦¥È¤ò¥ê¥½¡¼¥¹ÄêµÁ¤«¤éÆɤ߹þ¤Þ¤»¤ë¤³¤È¤â½ÐÍè¤Þ¤¹¡£ËÜ¥µ¥ó¥×¥ë¤Ç¤Ï¥ê¥½¡¼¥¹ID ¤ò»ØÄꤷ¤Æ¤¤¤Þ¤¹¡£¥ê¥½¡¼¥¹ID ¤Ï SDK ¤¬¼«Æ°À¸À®¤·¤¿ R.java ¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤Æ¡¢ËÜ¥µ¥ó¥×¥ë¤Ç¤Ï layout.main ¤ò»ØÄꤷ¤Æ¤¤¤Þ¤¹¡£¤³¤ì¤Ï¥Õ¥¡¥¤¥ë¥Ä¥ê¡¼¤Î res/layout/main.xml ¤ËÂбþ¤·¤Æ¤¤¤Þ¤¹¡£

  • main.xml
        1	<?xml version="1.0" encoding="utf-8"?>
        2	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        3	    android:orientation="vertical"
        4	    android:layout_width="fill_parent"
        5	    android:layout_height="fill_parent"
        6	    >
        7	<TextView
        8	    android:layout_width="fill_parent"
        9	    android:layout_height="wrap_content"
       10	    android:text="Hello World, MainActivity"
       11	    />
       12	</LinearLayout>
       13	
    
      

    UI ¤Î¥ì¥¤¥¢¥¦¥È¤òµ­½Ò¤·¤¿¥Õ¥¡¥¤¥ë¤Ç¤¹¡£res/layout °Ê²¼¤Ë¤Ï¡¢UI ¥ì¥¤¥¢¥¦¥È¤òµ­½Ò¤¹¤ë xml ¥Õ¥¡¥¤¥ë¤ò³ÊǼ¤·¤Þ¤¹¡£Ì¾¾Î¤ÏÆä˵¬Äꤵ¤ì¤Æ¤¤¤Þ¤»¤ó¡£¥Ó¥ë¥É»þ¤ËưŪ¤Ëǧ¼±¤µ¤ì¡¢R.java ¤Î layout ¥¯¥é¥¹¤Ë¥Õ¥¡¥¤¥ë̾¤ËÂбþ¤·¤¿¥á¥ó¥Ð¤¬ÄêµÁ¤µ¤ì¤Þ¤¹¡£

    ËÜ¥µ¥ó¥×¥ë¤Î main.xml ¤ò´Êñ¤ËÀâÌÀ¤¹¤ë¤È¡¢UI ¾å¤ÎÉôÉÊÇÛÃÖ¤ò´ÉÍý¤¹¤ë¥ì¥¤¥¢¥¦¥ÈÍѤΥ¯¥é¥¹¤È¤·¤Æ android.widget.LinearLayout ¤ò»È¤¤¡¢¤½¤Î¾å¤Ëʸ»úÎó¤ò°·¤¦¤¿¤á¤Î¥¦¥£¥¸¥§¥Ã¥È¤Ç¤¢¤ë android.widget.TextView ¤òºÜ¤»¤Æ¤¤¤Þ¤¹¡£¤Þ¤¿¡¢TextView ¤Î text °À­¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢"Hello World, MainActivity" ¤È¤¤¤¦Ê¸»úÎ󤬽é´üɽ¼¨¤µ¤ì¤Þ¤¹¡£

    ¥ì¥¤¥¢¥¦¥ÈÍѤΥ¯¥é¥¹¤Ë¤Ä¤¤¤Æ¤Ï¡¢º£¸å¤É¤³¤«¤Ç¤â¤¦¾¯¤·¾Ü¤·¤¯¤ä¤ê¤¿¤¤¤È»×¤¤¤Þ¤¹¡£

  • strings.xml
        1	<?xml version="1.0" encoding="utf-8"?>
        2	<resources>
        3	    <string name="app_name">MainActivity</string>
        4	</resources>
    
      

    ʸ»úÎó¥ê¥½¡¼¥¹¤òÄêµÁ¤·¤¿¥Õ¥¡¥¤¥ë¤Ç¤¹¡£res/values °Ê²¼¤Ë¤Ï¡¢¿§¡¹¤Ê¥ê¥½¡¼¥¹¤òÄêµÁ¤¹¤ë¥Õ¥¡¥¤¥ë¤¬ÃÖ¤±¤ë¤Î¤Ç¤¹¤¬¡¢¤½¤ÎÃæ¤Ç¤â strings.xml ¤Ïʸ»úÎó¥ê¥½¡¼¥¹ÄêµÁ¤Î¤¿¤á¤Ë»È¤¤¤Þ¤¹¡£ËÜ¥µ¥ó¥×¥ë¤Î strings.xml ¤Ç¤Ï¡¢"MainActivity" ¤È¤¤¤¦Ê¸»úÎó¤ò "app_name" ¤È¤¤¤¦¥ê¥½¡¼¥¹Ì¾¤ÇÄêµÁ¤·¤Æ¤¤¤Þ¤¹¡£

°Ê¾å¤Ç²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£¼¡²ó¤Ï¡¢Android SDK ¤Ç°·¤¦¥ê¥½¡¼¥¹¤Ë¤Ä¤¤¤Æ½ñ¤­¤¿¤¤¤È»×¤¤¤Þ¤¹¡£