[問題] 剛入門BMI問題
如題 因為剛入門 到手機執行都會錯誤 可是卻可以執行
先附上程式碼
package com.example.oaa;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class oaa extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Listen for button clicks
Button button = (Button)findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText)findViewById(R.id.height);
EditText fieldweight = (EditText)findViewById(R.id.weight);
double height =
Double.parseDouble(fieldheight.getText().toString())/100;
double weight =
Double.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);
TextView result = (TextView)findViewById(R.id.result);
result.setText("Your BMI is "+nf.format(BMI));
//Give health advice
TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
if(BMI>25){
fieldsuggest.setText(R.string.advice_heavy);
}else if(BMI<20){
fieldsuggest.setText(R.string.advice_light);
}else{
fieldsuggest.setText(R.string.advice_average);
}
}
};
}
另外還有XML的檔案
<?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/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="身高 (cm)" />
<EditText
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<TextView
android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="體重 (kg)" />
<EditText
android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="計算 BMI 值"
/>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
煩請高手幫解決 謝謝!!!
--
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.61.163.55
※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1404725770.A.307.html
→
07/07 17:56, , 1F
07/07 17:56, 1F
→
07/07 17:56, , 2F
07/07 17:56, 2F
→
07/07 18:03, , 3F
07/07 18:03, 3F
→
07/07 18:03, , 4F
07/07 18:03, 4F
→
07/07 18:03, , 5F
07/07 18:03, 5F
→
07/07 18:04, , 6F
07/07 18:04, 6F
→
07/07 18:04, , 7F
07/07 18:04, 7F
→
07/07 18:15, , 8F
07/07 18:15, 8F
→
07/07 18:16, , 9F
07/07 18:16, 9F
→
07/07 18:17, , 10F
07/07 18:17, 10F
→
07/07 18:28, , 11F
07/07 18:28, 11F
→
07/07 18:34, , 12F
07/07 18:34, 12F
→
07/07 18:51, , 13F
07/07 18:51, 13F
→
07/07 19:12, , 14F
07/07 19:12, 14F
→
07/07 19:13, , 15F
07/07 19:13, 15F
※ 編輯: ms0561188 (210.61.163.55), 07/07/2014 19:16:17
※ 編輯: ms0561188 (210.61.163.55), 07/07/2014 19:21:31
→
07/07 22:39, , 16F
07/07 22:39, 16F
→
07/07 23:07, , 17F
07/07 23:07, 17F
→
07/07 23:07, , 18F
07/07 23:07, 18F
→
07/08 11:59, , 19F
07/08 11:59, 19F
→
07/08 11:59, , 20F
07/08 11:59, 20F