Java初心者が携帯百景投稿用クライアントを作る その2

前回の多量のボタンのアクションの記述を簡単にしたい・・・のある程度の対応が出来たのでメモ

  • strings.xmlにボタンに貼付ける文字列を記述
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, mainActivity!</string>
    <string name="app_name">testEdit</string>
    <string-array name="array01">
    	<item>[[</item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item></item>
    	<item>]]</item>
</string-array>
</resources>
  • レイアウトをmain.xmlに記述*1
<?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">
	<EditText android:text="EditText01" android:id="@+id/EditText01"
		android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
	<LinearLayout android:id="@+id/LinearLayout01"
		android:layout_width="fill_parent" android:layout_height="wrap_content">
		<Button android:text="Button02" android:id="@+id/B0"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button03" android:id="@+id/B1"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button04" android:id="@+id/B2"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button05" android:id="@+id/B3"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button06" android:id="@+id/B4"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button07" android:id="@+id/B5"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button08" android:id="@+id/B6"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button09" android:id="@+id/B7"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button10" android:id="@+id/B8"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	</LinearLayout>
	<LinearLayout android:id="@+id/LinearLayout02"
		android:layout_width="fill_parent" android:layout_height="wrap_content">
		<Button android:text="Button11" android:id="@+id/B9"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button12" android:id="@+id/B10"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
		<Button android:text="Button13" android:id="@+id/B11"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	</LinearLayout>
</LinearLayout>
  • メインとなるmainActivity.java
package com.natu.testedit;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class mainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// コマンドボタン創成
		int len = getResources().getStringArray(R.array.array01).length;
		ArrayList<Button> btns = new ArrayList<Button>();
		for (int i = 0; i < len; i++) {
			int wId = getResources().getIdentifier("B" + String.valueOf(i),
					"id", "com.natu.testedit");
			final Button btn = (Button) findViewById(wId);
			btn.setText(getResources().getStringArray(R.array.array01)[i]);
			btn.setOnClickListener(new View.OnClickListener() {

				public void onClick(View view) {
					EditText txt = (EditText) findViewById(R.id.EditText01);
					int loc = txt.getSelectionStart();
					String ttxt = txt.getText().toString();
					// カーソル位置にボタンの文字を挿入
					String prefix = ttxt.substring(0, loc);
					String suffix = loc < ttxt.length() ? ttxt.substring(loc,
							ttxt.length()) : "";
					txt.setText(prefix + btn.getText() + suffix);
					txt.setSelection(loc + btn.getText().length());
				}
			});
			btns.add(btn);
		}
	}
}

今回の肝はgetIdentifierというメソッド、これを使うことにより指定した条件でコンポーネントを検索してIdを返してくれる
つまり変数でIdを指定できるということ

public int getIdentifier (String name, String defType, String defPackage)

Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Parameters
name The name of the desired resource.
defType Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type.
defPackage Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package.
Returns

* int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

で、このような画面が出来上がる

あとは絵文字の対応が済めばいよいよリリースだが、情報が一画面に納まらないため機能毎にタブで分けたり絵文字ボタンの見せかたをどうするか等の問題が山積みである

今まで出来ているところ

  • カメラアプリやギャラリーの共有機能にアプリを追加する

  • ローカルのコンタクトリストから投稿用アドレスの取得、文字位置等のコマンドをドロップダウンリストで指定

  • 送るボタン押下でメーラアプリが起動


という部分は完成済みでDeveloper Registration Feeも支払い済みだったりはしているので近いうちβ版として登録しても良いかもしれない

*1:本当はこれも省きたい