BMIの計算(多言語対応) | tototan

BMIの計算(多言語対応)

自作iPhoneアプリ一覧
※ 環境:Mac Lion,Eclipse 3.7(日本語バージョン)
※ xmlファイルはタグが多すぎて、入力文字数の制限を超えてしまうので、画像ファイルにしました。

以下の仕様書に基づいてアプリケーションを作成します。
1.アプリケーション名は、PracticalExercises7Bとすること。 
2.体重、身長を小数点で入力できるUIをXMLファイルで実装すること。 
3.体重と身長から、BMIを計算し、小数点第一位まで画面上に表示すること。 
4. BMIより、下表に対応した体格の診断結果をユーザーに通知すること。 BMI値の範囲 体格の傾向  18.5未満 痩せている  18.5 以上 25.0 未満 標準  25.0 以上 30.0 未満 太っている  30.0 以上 肥満 
5.計算を開始する、または、表示されている数値をクリア(初期化)するイベントを実装すること。
6.不正な値が入力された場合など、異常が発生した場合には、異常をユーザに通知すること。 
7.BMIの計算は、下式を用いて算出する。身長の単位は、m(メートル)を用いて計算を行う。    BMI値 = [体重(kg)] / ([身長(m)]×[身長(m)]) 
8.診断結果別に背景と診断結果表示文字列の色を変更する。 
9. 日本語と英語の多言語対応とする。
10. Androidのバージョンは、2.1を元に作成する。 

作業手順
1. 新規プロジェクトの作成
プロジェクト名: PracticalExersixes7
パッケージ名: hoge.practicalexersixes7(hogeはリバースドメインなどのユニークな文字列)
ビルド・ターゲット: Android 1.6

2. strings.xmlの編集
PracticalExercises7 → res → valuesの デフォルト(英語用)のstrings.xmlを編集 $tototan-2

【strings.xml】デフォルト(英語用)
$tototan-stringEN
3. 日本語用string.xmlを作成
  
resを右クリック → 新規 → その他 $tototan-3_1   
Android XML Fileを選択して次へ $tototan-3_2   
ファイル名に【strings.xml】と入力 → The destination file already existsという警告が表示されるが無視して次へ $tototan-3_3   
左のリストから【語Language】を選択して【->】をクリック   
言語テキストボックスへ【ja】と入力して完了 $tototan-3_4   
resの下に【values-ja】フォルダーができてその中に【strings.xml】がある事を確認   
strings.xmlを編集

【strings.xml】日本語用
$tototan-stringJP

5. layout.xmlを作成

  resの下のlayoutを右クリック【Android XML Lyaout File】を選択して次へ

  ルート要素のリストから【LinearLayout】を選択してファイル名に【layout】(何でもいい)と入力
【layout.xml】
$tototan-layout_1 $tototan-layout_2 $tototan-layout_3

6. javaファイルの編集

package com.me.tototan;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.graphics.Color;

import android.media.AudioManager;

import android.media.ToneGenerator;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.Toast;

import java.text.DecimalFormat;


import net.npaka.PracticalExercises7.R;


public class PracticalExercises7Activity extends Activity implements View.OnClickListener {

private float bmi;

private Button calcButton;

private Button  clearButton;

private EditText heightEt;

private EditText weightEt;

private TextView bmiTv;

private TextView resultTv;

private LinearLayout liLayout; // 背景画像切替用

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout); // レイアウトの指定

        

        // コンポーネントの関連付け

        calcButton = (Button)this.findViewById(R.id.button1);

        calcButton.setOnClickListener(this);

        clearButton = (Button)this.findViewById(R.id.button2);

        clearButton.setOnClickListener(this);

        heightEt = (EditText)this.findViewById(R.id.editText1);

        weightEt = (EditText)this.findViewById(R.id.editText2);

        bmiTv = (TextView)this.findViewById(R.id.result_view);

        resultTv = (TextView)this.findViewById(R.id.info_view);

        liLayout = (LinearLayout)this.findViewById(R.id.layout);

        

        Toast.makeText(this, R.string.message, Toast.LENGTH_LONG).show(); // トーストの表示

        

    }

   

    // ダイアログの表示

    private static void showDialog(final Activity activity,String title,String text) {

    AlertDialog.Builder ad = new AlertDialog.Builder(activity);

    ad.setTitle(title);

    ad.setMessage(text);

    ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dailog,int whichButton) {

    activity.setResult(Activity.RESULT_OK);

    }

    });

    ad.create();

    ad.show();

    }

    

    // ボタンクリックイベントの処理

    public void onClick(View view) {

    float height;

    float weight;

    

    // ボタンタップ時の効果音の作成

    ToneGenerator toneGenerator = new ToneGenerator(

            AudioManager.STREAM_SYSTEM,

            ToneGenerator.MAX_VOLUME);

    toneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);

    toneGenerator.stopTone();

    

    // 計算ボタンを押した時の処理

    if (view == calcButton) {

    try {

    height = Float.parseFloat(heightEt.getText().toString());

    weight = Float.parseFloat(weightEt.getText().toString());

    bmi = weight/((height/100)*(height/100));

    DecimalFormat df = new DecimalFormat("000.0"); // 小数点以下第一位でフォーマット

    String formattedBmi = df.format(bmi);

    bmiTv.setText(formattedBmi);

    resultTv.setText(hantei(bmi));

    } catch (NumberFormatException e) {

    showDialog(this,"",getString(R.string.warn_message_3)); // 例外発生時のダイアログの表示

    }

    }

    

    // クリアボタンを押した時の処理

    else if (view == clearButton) {

    bmiTv.setText("000.0");

resultTv.setTextColor(Color.MAGENTA);

resultTv.setTextSize(20);

    resultTv.setText(getString(R.string.info_message_1));

    heightEt.setText(null);

    weightEt.setText(null);

    liLayout.setBackgroundResource(R.drawable.beach);

    }

    }

    

    // BMI の判定

public String hantei(float value) {

if (value < 18.5) {

resultTv.setTextColor(Color.RED); // 文字色を指定

resultTv.setTextSize(30); // テキストサイズを指定

liLayout.setBackgroundResource(R.drawable.beach); // 背景画像を指定

return getString(R.string.bmi_categories_under_weight); // 判定結果を返す

} else if (value < 25.0) {

resultTv.setTextColor(Color.GREEN);

resultTv.setTextSize(30);

liLayout.setBackgroundResource(R.drawable.sky);

return getString(R.string.bmi_categories_normal_weight);

} else if (value < 30.0) {

resultTv.setTextColor(Color.RED);

resultTv.setTextSize(30);

liLayout.setBackgroundResource(R.drawable.bg1);

return getString(R.string.bmi_categories_over_weight);

} else {

resultTv.setTextColor(Color.RED);

resultTv.setTextSize(30);

liLayout.setBackgroundResource(R.drawable.bg1);

return getString(R.string.bmi_categories_obesity_weight);

}

}

}