忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/05/08 20:29 |
Android2.2 名刺管理アプリケーションを作ってみる3日目その2
http://codezine.jp/article/trackback/4670

前回の続き。
これまでずっとアクティビティひとつで完結するアプリしか作っていませんでした。
複数アクティビティに挑戦してみます。

まずはビュー。
/res/layout/regist.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:stretchColumns="1"
	android:shrinkColumns="1">
	<!-- 名前 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/person_name"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/personNameText"
			android:layout_span="2"
			android:hint="@string/required"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>

	<!-- 会社 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/company_name"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/companyNameText"
			android:layout_span="2"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>

	<!-- 部署 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/organization_name"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/organizationNameText"
			android:layout_span="2"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>

	<!-- 役職 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/position_name"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/positionNameText"
			android:layout_span="2"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>

	<!-- 郵便番号 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/zip_code"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_gravity="center_vertical" />

		<EditText
			android:id="@+id/zipCodeText"
			android:minWidth="100dip"
			android:inputType="phone"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>

	<!-- 住所 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/address"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/addressText"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<ImageButton
			android:id="@+id/mapButton"
			android:src="@drawable/map"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</TableRow>

	<!-- 電話1 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/tel1"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/tel1Text"
			android:layout_span="2"
			android:inputType="phone"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"/>
	</TableRow>

	<!-- 電話2 -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/tel2"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/tel2Text"
			android:layout_span="2"
			android:inputType="phone"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"/>
	</TableRow>

	<!-- メール -->
	<TableRow
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<TextView
			android:text="@string/mail"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText
			android:id="@+id/mailText"
			android:layout_span="2"
			android:inputType="textEmailAddress"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"  />
	</TableRow>
</TableLayout>
当初自力でやろうと思ってたんだがめんどくさすぎたので結局コピペになった。
もう少しどうにかならんのか。
ビューはデフォルトでは全体を<LinearLayout>で囲まれますが、これを<TableLayout>に変更します。
LinearLayoutは一行にひとつの要素を記述する、まさにメニュー用途向けのレイアウトですが、逆に複雑なレイアウトは表示できません。

TableLayoutはHTMLの<table>とほぼ同じで、<tr>に相当する<TableRow>で行を指定し、中に各部品を配置していきます。
android:layout_spanはcolspanです。
rowspanは無いみたい?
android:inputTypeは入力文字種を指定できます。
"phone"であれば数値、"textPassword"であればパスワード等。
これを設定するとソフトウェアキーボードも自動的に変わったりします。
"textPersonName"とか"textPostalAddress"とかまであるみたいなんだがどう使うんだそれ。
 
android:hintは未入力時に表示されるヒントです。
カーソルを合わせると削除されます。
HTMLだとこの機能はJavaScriptで実装するしかないのですが、Androidでは予め用意されています。

ビューに配置されている@stringを設定。
 
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BizCard</string>
    <string name="person_name">名前</string>
    <string name="company_name">会社</string>
    <string name="organization_name">組織</string>
    <string name="position_name">役職</string>
    <string name="zip_code">郵便番号</string>
    <string name="address">住所</string>
    <string name="tel1">電話1</string>
    <string name="tel2">電話2</string>
    <string name="mail">メール</string>
    <string name="required">必須</string>
    <string name="map">地図</string>
    <string name="map_satellite">衛星写真</string>
    <string name="current_location_on">現在地を中央に表示</string>
    <string name="current_location_off">現在地を中央に表示</string>
</resources>

住所のところに&lt;ImageButton android:src="@drawable/map"&gt;というのがありますが、これは画像ファイルです。

/res/drawableフォルダに画像を登録し、そのファイル名を指定することで画像表示が行えます。
拡張子は指定しないようです。
map.pngとmap.jpgがあったらどうするんだろうと思ったが登録できないみたい。

デフォルトではdrawable-hdpi、drawable-mdpi、drawable-ldpiといったフォルダがありますが、高解像度用、中解像度用、低解像度用の画像を入れておくと自動で出し分けされます。
drawableフォルダは該当解像度の画像が見つからなかった場合のデフォルトになります。

ようやく素材が完成したので次はアクティビティを作成します。

/src/com.example.bizcard/RegistActivity.java
package com.example.bizcard;
import android.app.Activity;
import android.os.Bundle;

public class RegistActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist);
	}
}
 
例によってpackageやimportはIDEが勝手にやってくれます。
ListActivityと違うところは、最後のsetContentViewのターゲットがR.layout.registになっています。
これによって/res/layout/regist.xmlがビューとして使用されることになります。

最後にAndroidManifest.xmlを記述します。
いくらアクティビティを作成しても、AndroidManifest.xmlに記述しないかぎり無かったもの扱いになるようです。

<application>内に今回作成したアクティビティを追加します。

/AndroidManifest.xml
<activity android:name=".RegistActivity"
		  android:label="@string/app_name">
	<intent-filter>
		<action android:name="android.intent.action.MAIN" />
	</intent-filter>
</activity>

<intent-filter>って何?というところですが、外部アプリから起動される際の関連付けの設定などを行うようです。
中でも特に、<action android:name="android.intent.action.MAIN" />を設定したアクティビティは、ランチャーから直接起動したときに実行されるアクティビティになります。

ところで現在ListActivityとRegistActivityの両方に<action android:name="android.intent.action.MAIN" />が設定される事態となっておりますが、この場合どちらが優先されるかはコンパイル時に決められます。
「実行」→「実行構成」→「LaunchAction」から指定を行えます。
ここをRegistActivityにし、実行すると無事入力画面が表示されました。


Androidの記事
PR


2010/07/12 22:55 | Comments(0) | TrackBack() | Android

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<Android2.2 名刺管理アプリケーションを作ってみる3日目その3 | HOME | 買ったものリスト 2010/07/11>>
忍者ブログ[PR]