忍者ブログ
[PR]
×

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



2024/04/20 00:46 |
Android2.2 入力値を表示するアプリその2

前回入力値を表示するアプリを作成しました。

20100611-入力値を表示するアプリ01.png
 

この最初からfalseとなっているのは何でしょう。
main.xmlにおいて、値を指定する部分がandroid:text="@+id/TextView01"とかになっていましたが、その値を取得できないよ、ということのようです。
出力される文字を定義することにします。

まずmain.xmlを編集。
<TextView
	android:text="@string/TextView01"
	android:id="@+id/TextView01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</TextView>
<EditText
	android:text="@string/EditText01"
	android:id="@+id/EditText01"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
</EditText>
<Button
	android:text="@string/Button01"
	android:id="@+id/Button01"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
</Button>
<TextView
	android:text="@string/TextView02"
	android:id="@+id/TextView02"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</TextView>
textの項目が@+idから@stringになりました。
この@stringから始まる項目は、/res/values/strings.xmlで定義する各要素と結びつけられます。

@から始めず直接"テキスト"とか書いても問題無く動くのですが、国際化を考えると予め分けておいたほうが良さそうです。
strings.xmlは言語毎に簡単に切り替えることができるので、日本語を出力する部分を分けておけば、英語対応する際もプログラムは触らず英語用のstrings.xmlを作成するだけで済むのです。

/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="TextView01">名前を入力してください</string>
    <string name="EditText01"></string>
    <string name="Button01">クリックしてください</string>
    <string name="TextView02"></string>
</resources>
予め"hello"と"app_name"が定義されていますが、"hello"はテスト用なので削除してもかまいません。
"app_name"はプロジェクト作成時に指定したアプリケーション名で、実行時に表示されるやつです。

実行時(コンパイル時?)に、"@string/TextView01"の部分が<string name="TextView01">の要素に差し替えられ、"名前を入力してください"が表示されます。

20100611-入力値を表示するアプリ02.png 

以上で、入力した名前を表示するアプリケーションが完成しました。


さて、参考サイトはここでおしまいですが、もう少し続けてみます。
せっかく文言を分離したのにDisplayTextActivity.javaに直接日本語が入っています。
これもstrings.xmlに分離してしまいましょう。

/src/com.example.display.text/DisplayTextActivity.java
	// クリック実装
	private class MyClickAdapter implements OnClickListener {
		public void onClick(View v) {
			Editable value = text.getText();
			String editTextBefore = getString(R.string.editTextBefore);
			String editTextAfter = getString(R.string.editTextAfter);
			display.setText(editTextBefore + value + editTextAfter);
		}
	}

/res/values/strings.xml
    <string name="editTextBefore">こんにちは、</string>
    <string name="editTextAfter">さん</string>

Context.getString()でRの要素を取得できるようです。
ちなみにこちらにthisを付けると、MyClickAdapter.getString()なんてメソッドはないと怒られてしまいます。

以上で、全ての日本語をstrings.xmlに排除することができました。


ということでせっかくだから英語対応してみましょう。

/res/values-en/strings.xmlを作成します。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">DisplayText</string>
    <string name="TextView01">Please insert name</string>
    <string name="EditText01"></string>
    <string name="Button01">Click Here</string>
    <string name="TextView02"></string>
    <string name="editTextBefore">Hello,</string>
    <string name="editTextAfter"></string>
</resources>

完成。

え?これだけ?
はい、これだけです。

本体設定を英語にすると、見事に英語のフォームが表示されました。
英語が出鱈目なのはスルーということでひとつ。

20100611-入力値を表示するアプリ03.png 

実行時に言語ファイルを見る順番として、本体設定が英語の場合はまずvalues-enフォルダを確認し、それが無ければデフォルトのvaluesフォルダを使用します。
同様に本体設定が日本語の場合はまずvalues-jaフォルダを確認することになっています。
今回の場合、本体が英語設定の場合はvalues-enフォルダの中身を利用し、それ以外の言語だった場合はvaluesフォルダを利用することになります。
完全に国際化対応した場合は、valuesフォルダを英語で作成し、日本語をvalues-jaフォルダに入れるとよいでしょう。


で、第二回はまだですか><
PR


2010/06/18 22:39 | Comments(0) | TrackBack() | Android

トラックバック

トラックバックURL:

コメント

コメントを投稿する






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



<<買ったものリスト 2010/06/20 | HOME | ==NOVEL PHILOSOMA== 18>>
忍者ブログ[PR]