今天要来做一个计算BMI的简易App!
activity_main.xml:
使用LinearLayout的vertical进行排版,再将元件一一放入
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高:"
android:textSize="20dp" />
<EditText
android:id="@+id/getHigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>
<TextView
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体重:"
android:textSize="20dp" />
<EditText
android:id="@+id/getWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="@+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnClick"
android:text="送出" />
</LinearLayout>
MainActivity:
按下按钮时判断身高或体重有没有空值,如果都没有空值的话,进行计算与跳页传值的动作
public class MainActivity extends AppCompatActivity {
EditText getHigh,getWeight;
float result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getHigh = findViewById(R.id.getHigh);
getWeight = findViewById(R.id.getWeight);
}
public void btnClick(View view) {
if (!("".equals(getHigh.getText().toString()) || "".equals(getWeight.getText().toString()))){
float high = Float.parseFloat(getHigh.getText().toString());
float weight = Float.parseFloat(getWeight.getText().toString());
float highBMI = (high/100)*(high/100);
result = weight/highBMI;
Intent it = new Intent(MainActivity.this, MainActivity2.class);
Bundle bundle = new Bundle();
bundle.putFloat("BMI", result);
it.putExtras(bundle);
startActivity(it);
finish();
} else {
Toast.makeText(this,"请输入身高或体重", Toast.LENGTH_SHORT).show();
}
}
}
activity_main2.xml:
跟activity_main.xml一样,使用LinearLayout的vertical进行排版,再将元件一一放入
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/console"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="console"/>
<TextView
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"/>
<Button
android:id="@+id/btnBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnBack"
android:text="返回" />
</LinearLayout>
MainActivity2:
显示MainActivity计算过的BMI结果,判断BMI的程度
public class MainActivity2 extends AppCompatActivity {
TextView console,check;
float result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle = getIntent().getExtras();
result = bundle.getFloat("BMI");
NumberFormat nf = NumberFormat.getInstance(); // 数字格式
nf.setMaximumFractionDigits(2); // 限制小数第二位
console = findViewById(R.id.console);
console.setText(nf.format(result));
checkBMI();
}
private void checkBMI() {
check = findViewById(R.id.check);
if (result < 18.5)
check.setText("过轻");
else if (18.5 <= result && result < 24)
check.setText("正常");
else if (24 <= result && result < 27)
check.setText("过重");
else if (27 <= result && result < 30)
check.setText("轻胖");
else if (30 <= result && result < 35)
check.setText("中胖");
else if (result >= 35)
check.setText("重胖");
}
public void btnBack(View view) {
Intent it = new Intent(MainActivity2.this, MainActivity.class);
startActivity(it);
finish();
}
}
谢谢大家愿意花时间阅读,小弟弟我在此鞠躬
视图(检视)模型 / ViewModel 主要用於为View提供资料 ViewModel当中的属性不...
前言 背景是一个如此重要的东西,你能想像萤幕的话棉全都是白底或黑底吗!!当然不行啊!! backgr...
-董事委员会 董事会认为必要时可设立任何委员会。有些委员会通常是法律或法规所要求的,例如审计委员会...
Message in MIDI 书接昨日,在 MIDI 里面有固定的格式在记录声音的讯号,因为实在是...
为什麽需要定位元素? 定位元素可以实现 : 某个元素可以自由地在一个盒子内移动位置,并且压住其他盒子...