◎Android開発準備
1.JDK(Java Development Kit)のセットアップ
jdk-6u25-windows-i586.exeをインストールしました。
2.Android SDK(Software Development Kit)のセットアップ
android-sdk_r10-windows.zipをインストールしました。
3.Eclipse のセットアップ
eclipse-java-helios-SR2-win32.zipをインストールし、
日本語化で、pleiades_1.3.2.zipをインストールしました。
◎補足
Android SDKで、インストール後エミュレータを動作させようとすると、
adb.exe が見つからないとエラーになってしまいます。
そこで、platform-tools にあるadb.exeとAdbWinApi.dllを tools フォルダに
コピーするとエミュレータが正常に動作します。
◎サンプルアプリの作成
Buttonを押したら、TextViewに会社が表示され、editTextに奥進システムが表示し、
Button名を表示に変更するアプリを作成してみましょう。
1.「ファイル」メニュー>新規>プロジェクトを選択する。
2.新規プロジェクトのダイアログが表示する。
AndroidのAndroidプロジェクトを選択し、「次へ」をクリックする。
3.プロジェクト名:hellooku
【内容】
「ワークスペース内に新規プロジェクトを作成」を選択する。
「デフォルト・ロケーションを使用」をチェックする。
「ビルドターゲット」Android2.2をチェックする。
アプリケーション名:Hellooku
パッケージ名:jp.co.okushin.android.hellooku
Create Activity:HellookuActivity
Min SDK Version:8
に設定して「完了」をクリックする。
4.レイアウトの作成
hellooku/res/layout/main.xmlを編集します。
上記ファイルをクリックすると、Graphical Layoutタブが出てきますので、
そこで画面レイアウトを作成します。TextViewを配置して、Plain Text(editText)を配置して、
Buttonを配置します。ここで、main.xmlを保存しますが、
これだけでは、配置した部品にIDが振られていない状態となり、
実行しようとしても実行時にエラーとなります。
そこで、main.xmlを修正した場合は、プロジェクト->プロジェクトのビルドを実行してください。
この操作により、 hellooku/gen/jp.co.okushin.android.hellooku/R.javaのIDが自動で生成されます。
hellooku/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/editText1"> <requestFocus></requestFocus> </EditText> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
5.ソースプログラムの編集
hellooku/src/jp.co.okushin.android.hellooku/HellookuActivity.java
package jp.co.okushin.android.hellooku;
import jp.co.okushin.android.hellooku.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HellookuActivity extends Activity {
TextView textView1;
EditText editText1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1 = (TextView)this.findViewById(R.id.textView1);
editText1 = (EditText)this.findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
button.setText("表示");
textView1.setText("会社");
editText1.setText("奥進システム");
}
});
}
}