AndroidBMI程序设计
原创BMI接口布局实现代码
简单使用垂直排列的线性布局Text View和EditText文本大小、颜色、位置等都是简单定义的Edit Text控制的一部分id定义,方便MainActivity.Java中调用!
Java函数实现运行代码
调用获得xml计算了布局文件的一系列属性、输入高度和权重,并给出了结果。set到控件BIM在线,并使用Toast获取简短提示!
package com.example.bmiapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private EditText heightText,weightText,resText;
// private TextView resText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
heightText = (EditText) findViewById(R.id.sg);
weightText = (EditText) findViewById(R.id.tz);
resText = (EditText) findViewById(R.id.BMI);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取身高体重
String height = heightText.getText().toString();
String weight = weightText.getText().toString();
double result = 0, heightNum = 0, weightNum = 0;
if(!height.isEmpty()&&!weight.isEmpty()) {
heightNum = Double.parseDouble(height);
weightNum = Double.parseDouble(weight);
result = weightNum / (heightNum*heightNum);
TextView BMI = (TextView) findViewById(R.id.BMI);
BMI.setText("Your BMI is " + result);
}
if (result <= 18.4) {
Toast.makeText(MainActivity.this, "你的体重有点轻", Toast.LENGTH_SHORT).show();
} else if (result<= 23.9 && result> 18.5) {
Toast.makeText(MainActivity.this, "你的体重正常", Toast.LENGTH_SHORT).show();
} else if (result<= 27.9 && result> 24) {
Toast.makeText(MainActivity.this, "你有点超重", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "你的体重有点胖哦", Toast.LENGTH_SHORT).show();
}
}
});
}
}
这篇文章是原创的。如果有什么不懂或者不懂的地方,可以在评论区留言。如果你看到它,你将能够第一次回答它。如果您需要打包源代码或图片资源,可以将其留在评论区。QQ邮箱,如果这个简单的代码能对你有所帮助,我希望你能用你的小手给我一些鼓励。我希望你和我今年能在学习编程的路上收获满满。来吧
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除