bmiandroid
發布時間: 2025-04-11 02:15:32
A. 用android怎麼做計算BMI值得程序
應用的操作和原理
目標Android應用的操作過程是這樣的:選擇你的性別,然後輸入你的身高,點查看計算結果的按鈕就在Toast中顯示你的標准體重。力求操作簡單,結果顯示清楚。
標准體重的計算公式:
男性:(身高cm-80)×70%=標准體重
女性:(身高cm-70)×60%=標准體重
應用的源碼
BMIActivity.java:
packagecom.ling.bmi;
importjava.text.DecimalFormat;
importjava.text.NumberFormat;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.RadioButton;
importandroid.widget.Toast;
/*
*@authorling*該程序的功能是用戶選擇自己的性別和輸入自己的身高,然後點擊按鈕,就能在Toast顯示出自己的標准體重
*/
{
/**.*/
privateButtoncountButton;
privateEditTextheighText;
privateRadioButtonmaleBtn,femaleBtn;
Stringsex="";
doubleheight;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//調用創建視圖的函數
creadView();
//調用性別選擇的函數
sexChoose();
//調用Button注冊監聽器的函數
setListener();
}
//響應Button事件的函數
privatevoidsetListener(){
countButton.setOnClickListener(countListner);
}
=newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Toast.makeText(BMIActivity.this,"你是一位"+sexChoose()+" "
+"你的身高為"+Double.parseDouble(heighText.getText().toString())+"cm"
+" 你的標准體重為"+getWeight(sexChoose(),height)+"kg",Toast.LENGTH_LONG)
.show();
}
};
//性別選擇的函數
privateStringsexChoose(){
if(maleBtn.isChecked()){
sex="男性";
}
elseif(femaleBtn.isChecked()){
sex="女性";
}
returnsex;
}
//創建視圖的函數
publicvoidcreadView(){
//txt=(TextView)findViewById(R.id.txt);
countButton=(Button)findViewById(R.id.btn);
heighText=(EditText)findViewById(R.id.etx);
maleBtn=(RadioButton)findViewById(R.id.male);
femaleBtn=(RadioButton)findViewById(R.id.female);
//txt.setBackgroundResource(R.drawable.bg);
}
//標准體重格式化輸出的函數
privateStringformat(doublenum){
NumberFormatformatter=newDecimalFormat("0.00");
Stringstr=formatter.format(num);
returnstr;
}
//得到標准體重的函數
privateStringgetWeight(Stringsex,doubleheight){
height=Double.parseDouble(heighText.getText().toString());
Stringweight="";
if(sex.equals("男性")){
weight=format((height-80)*0.7);
}
else{
weight=format((height-70)*0.6);
}
returnweight;
}
}
熱點內容