[]string.xmlの間違った使い方

android:onclickにstring.xmlを使うことができた。これを使えば言語設定によってロジックを振り分けることができる。


valuesフォルダの作成

  • values-ja
  • values-fr

string.xmlの設定

それぞれ以下のように設定する

values/string.xml
    <string name="onclick">onClickButton</string>
values-ja/string.xml
    <string name="onclick">onClickButtonJa</string>
values-fr/string.xml
    <string name="onclick">onClickButtonFr</string>

ActivityにonClickXXを追加

    public void onClickButton(View v){
    	text.setText("100$は100$");
    }
    
    public void onClickButtonJa(View v){
    	double en = 100 * 0.76;
    	text.setText("100$は" + en + "円");
    }
    
    public void onClickButtonFr(View v){
    	double eur = 100 * 0.72;
    	text.setText("100$は" + eur + "ユーロ");
    }
    

いまいち使いどころが分からないし、可読性も低くなるので使わないほうがいいね

ソース

package com.example.onclickstr;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class OnClickStringActivity extends Activity {
    
	TextView text;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text = (TextView)findViewById(R.id.text);
    }
    
    public void onClickButton(View v){
    	text.setText("100$は100$");
    }
    
    public void onClickButtonJa(View v){
    	double en = 100 * 0.76;
    	text.setText("100$は" + en + "円");
    }
    
    public void onClickButtonFr(View v){
    	double eur = 100 * 0.72;
    	text.setText("100$は" + eur + "ユーロ");
    }
    
}
<?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:id="@+id/text"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="100$" />
	<Button
		android:id="@+id/button1"
		android:layout_height="wrap_content"
		android:layout_width="match_parent"
		android:text="いくら?"
		android:onClick="@string/onclick"></Button>
</LinearLayout>