|
開発スタイル
- 開発環境の全体像
- Eclipse 上のプラグインを使用し、Java 言語で開発
- PC 上では、QEMU ARM 仮想環境で動作
- Activity を中心にロジックを記述する
- ファイル構成
- XML で記述
- 表示する文字列の定義
- 画面のレイアウト
- アプリケーションの属性
- Java で記述
ボタンを作成してみる with XML
プロジェクトの作成
- [File]→[New Project]→[Android]でプロジェクトを作成する
Item | Setting |
---|
Project name | SampleButton | Build Target | Android 2.2 | Application name | SampleButton | Package name | jp.example.samplebutton | Create Activity | SampleButtonActivity |
- CPU 負荷の問題で、ここからWindowsに変わったのは内緒...P4-3.2GHz では厳しいらしい
マニフェストファイル
- アプリケーションのヘッダーみたいなもの
- ランチャーに表示するアイコン・名前・version 値などの設定
- 開始するクラスを指定
- インターネットへのアクセス制限
- 自動で作成された後、殆どの場合変更必要なし
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| |
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.example.samplebutton"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SampleButtonActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
|
表示文字列の定義ファイル
- 固定値の定義ができる
- デフォルトで作成されるのは strings.xml だが、色や配列の定義もできる
- C言語で言うところの、#define みたいなもの
ボタンで使用するラベルを追加してみる
- strings.xml を開く
- [Add]ボタンをクリックして、[String] をクリック
- [Name] と [Value] を設定する (今回は Name=button_label, Value=Sanachan)
- 下の[strings.xml]タブを選択し、手動で変更してもOK
0
1
2
3
4
5
| |
<resources>
<string name="hello">Hello World, SampleButtonActivity!</string>
<string name="app_name">SampleButton</string>
<string name="button_label">Sanachan</string>
</resources>
|
View構成(レイアウト)ファイル
- 画面レイアウトを設定する
- オブジェクトの配置や、デフォルト文字の指定ができる
- プログラムからアクセスできるように、ID の追加をお忘れなく
ボタンのレイアウトを追加する
- main.xml を開く
- [アウトライン] ウィンドウの +ボタンをクリック
- [Button]を選択して追加する
- 下部の [main.xml] タブを選択し、値を編集する
項目 | 値 | 備考 |
---|
android:id | "@+id/button_id" | プログラムからアクセスできるようにする | android:layout_width | "wrap_content" | オブジェクトを表示するために必要な最小サイズ | android:layout_height | "wrap_content" | オブジェクトを表示するために必要な最小サイズ | android:text | "@string/button_label" | strings.xml で追加したラベルを指定 |
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| |
<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"
/>
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label"
/>
</LinearLayout>
|
アプリケーションロジック
- 上記までの手順を踏んで実行すると、右図のように表示される
- ただ表示されるだけで、ボタンを押しても何も起こらない
インスタンスを取り出す
- SampleButtonActivity.java を開いて、下記を追加する
- 今回は Activity クラスの findViewByld メソッドを使用する
1
2
3
4
5
6
7
| -
|
-
|
|
|
|
| public class SampleButtonActivity extends Activity {
・・・
public void onCreate(Bundle savedInstanceState) {
・・・
Button button1 = (Button)findViewById(R.id.button_id);
・・・
|
リスナーを登録する
- 取り出したインスタンスに onClickListener を登録する
- 今回は、"Sanachan" と "Clicked." をトグルさせるようにする
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
!
|
|
-
-
|
-
|
-
|
|
|
!
!
!
|
| ・・・
ClickListener listener = new ClickListener();
button1.setOnClickListener(listener);
}
class ClickListener implements OnClickListener {
public void onClick(View v) {
Button button1 = (Button)v;
if ((++toggle % 2) != 0){
button1.setText("Clicked.");
}else{
button1.setText(R.string.button_label);
}
}
}
・・・
|
完成品
- ボタンをクリックする毎に、文字列が更新される
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
-
|
|
|
-
|
|
|
|
|
|
|
!
|
-
-
|
-
|
-
|
!
!
!
!
| package jp.example.samplebutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SampleButtonActivity extends Activity {
private int toggle = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button_id);
ClickListener listener = new ClickListener();
button1.setOnClickListener(listener);
}
class ClickListener implements OnClickListener {
public void onClick(View v) {
Button button1 = (Button)v;
if ((++toggle % 2) != 0){
button1.setText("Clicked.");
}else{
button1.setText(R.string.button_label);
}
}
}
}
|
ボタンを作成してみる without XML
- 上記のように、XML を使用せずにコードを書いても同じことが実現できる
- ただし、オブジェクトとロジックが分かれていないので見にくい
(人それぞれとは思うが...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
!
|
-
|
!
|
-
-
-
|
-
|
!
!
|
!
!
| package jp.example.samplebutton2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class SampleButtonActivity extends Activity implements OnClickListener {
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private Button button1;
private int toggle = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout buttonLayout = new LinearLayout(this);
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
setContentView(buttonLayout);
button1 = new Button(this);
button1.setText("Sanachan");
button1.setOnClickListener(this);
buttonLayout.addView(button1, createParam(WC, WC));
}
private LinearLayout.LayoutParams createParam(int w, int h){
return new LinearLayout.LayoutParams(w, h);
}
public void onClick(View v) {
if (v == button1){
if ((++toggle % 2) != 0) {
button1.setText("Clicked.");
}else{
button1.setText("Sanachan");
}
}
}
}
|
まとめ
- 上記の流れと構成を理解すれば、ある程度のサンプルを読むことができる
- ボタン作成を例に、電卓くらいは作れるはず
|
|