標題:
BMICalc
[打印本頁]
作者:
ray
時間:
2012-10-24 19:53
標題:
BMICalc
<RadioGroup
android:id="@+id/sexSel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/selMale"
android:text="@string/male_lbl" />
<RadioButton
android:id="@+id/selFemale"
android:text="@string/female_lbl" />
</RadioGroup>
複製代碼
作者:
ray
時間:
2012-10-24 22:42
package ray.work.bmicalc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.buttonCalc);
//View btn = findViewById(R.id.buttonCalc);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
複製代碼
作者:
ray
時間:
2012-10-26 18:56
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/height_lbl" />
<EditText
android:id="@+id/heightText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/weight_lbl" />
<EditText
android:id="@+id/weightText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/sex_lbl" />
<RadioGroup
android:id="@+id/sexSel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/selMale">
<RadioButton
android:id="@+id/selMale"
android:text="@string/male_lbl" />
<RadioButton
android:id="@+id/selFemale"
android:text="@string/female_lbl" />
</RadioGroup>
<Button
android:id="@+id/buttonCalc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/calc_lbl" />
<TextView
android:id="@+id/suggestionStr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-10-26 18:59
<resources>
<string name="app_name">BMICalc</string>
<string name="height_lbl">請輸入你的身高:</string>
<string name="weight_lbl">請輸入你的體重:</string>
<string name="sex_lbl">請選擇你的性別:</string>
<string name="male_lbl">男生</string>
<string name="female_lbl">女生</string>
<string name="calc_lbl">計算!!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
複製代碼
作者:
ray
時間:
2012-10-26 21:21
package ray.work.bmicalc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button btn = (Button)findViewById(R.id.buttonCalc);
//View btn = findViewById(R.id.buttonCalc);
//btn.setOnClickListener(this);
//btn = (Button)findViewById(R.id.buttonSugg);
//btn.setOnClickListener(this);
findViewById(R.id.buttonCalc).setOnClickListener(this);
findViewById(R.id.buttonSugg).setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v)
{
// TODO Auto-generated method stub
//Toast.makeText(this,"hello",Toast.LENGTH_SHORT).show();
try
{
float h = Float.parseFloat(((EditText)this.findViewById(R.id.heightText)).getText().toString());
float w = Float.parseFloat(((EditText)this.findViewById(R.id.weightText)).getText().toString());
boolean s = ((RadioButton)this.findViewById(R.id.selMale)).isChecked();
BMIManager myBMI = new BMIManager(h,w,s);
if(v.getId() == R.id.buttonCalc)
{
((TextView)this.findViewById(R.id.suggestionStr)).setText("BMI="+myBMI.calculateBMI());
}
if(v.getId() == R.id.buttonSugg)
{
((TextView)this.findViewById(R.id.suggestionStr)).setText(myBMI.suggestion());
}
}
catch(Exception e)
{
Toast.makeText(this,this.getResources().getString(R.string.errInput),Toast.LENGTH_SHORT).show();
}
}
}
複製代碼
作者:
ray
時間:
2012-10-31 20:03
String suggestion(float mBMI)
{
String mSuggestion = "";
if(mBMI >= 35)
{
if(msgProvider != null)
mSuggestion = msgProvider.getResources().getString(R.string.suggMale3);
else
mSuggestion = "這麼肥!會不會太扯了你~";
}
else if(mBMI >= 30 && mBMI <35)
{
if(msgProvider != null)
mSuggestion = msgProvider.getResources().getString(R.string.suggMale2);
else
mSuggestion = "再不減肥我怕你坐飛機要買2張票了";
}
else if(mBMI >= 27 && mBMI <30)
{
if(msgProvider != null)
mSuggestion = msgProvider.getResources().getString(R.string.suggMale1);
else
mSuggestion = "少吃點吧!胖子!";
}
else if(mBMI >= 24 && mBMI <27)
{
if(msgProvider != null)
mSuggestion = msgProvider.getResources().getString(R.string.sugg3);
else
mSuggestion = "過重";
}
else if(mBMI >= 18.5 && mBMI <24)
{
if(msgProvider != null)
mSuggestion = msgProvider.getResources().getString(R.string.sugg2);
else
mSuggestion = "標準";
}
else if(mBMI < 18.5)
{
if(msgProvider != null)
mSuggestion = msgProvider.getResources().getString(R.string.sugg1);
else
mSuggestion = "過輕";
}
return mSuggestion;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2