运行效果图:
点击计算
点击查看详情
重要知识点:
- 两个activity的跳转;
btn3.setOnClickListener(new myListener());
class myListener implements CompoundButton.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,resultActivity.class);
MainActivity.this.startActivity(intent);
}
}
第二个activity的返回按钮
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(resultActivity.this,MainActivity.class);
startActivity(intent);
}
});
- putExtra()函数传参;getStringExtra()函数接收参数;
- TextView中setText()显示信息;
完整代码:
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btn1,btn2,btn3;
private RadioButton boy,girl;
private EditText heightText,weightText;
private TextView resText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//以下为变量与控件的绑定
btn1 = (Button) findViewById(R.id.b1);
heightText = (EditText) findViewById(R.id.H);
weightText = (EditText) findViewById(R.id.W);
btn2 = (Button) findViewById(R.id.b2);
boy = findViewById(R.id.man);
girl = findViewById(R.id.woman);
btn3 = (Button) findViewById(R.id.go);
btn3.setOnClickListener(new myListener());
//button2的点击响应,作用为清空所有输入
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
heightText.setText("");
weightText.setText("");
}
});
//button2的点击事件响应,计算BMI的值,结果用Toast显示
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//得到身高体重
String height = heightText.getText().toString();
String weight = weightText.getText().toString();
String msg = "";
String bmi = "";
double result = 0, heightNum = 0, weightNum = 0;
if (height.isEmpty() || weight.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入身高体重", Toast.LENGTH_SHORT).show();
}
if (!height.isEmpty() && !weight.isEmpty()) {
heightNum = Double.parseDouble(height);
weightNum = Double.parseDouble(weight);
DecimalFormat df = new DecimalFormat("#.00");
result = 10000 * weightNum / (heightNum * heightNum);
bmi = df.format(result);
System.out.println(bmi);
}
Log.i("bmi",bmi);
msg = "您的bmi值是:".toString();
if (boy.isChecked()) {//如果选择的是男性
if (result <= 20) {
Toast.makeText(MainActivity.this, "您的体重过轻,吃吃吃!", Toast.LENGTH_SHORT).show();
} else if (result <= 25 && result > 20) {
Toast.makeText(MainActivity.this, "您的体重适中,继续保持!", Toast.LENGTH_SHORT).show();
} else if (result <= 30 && result > 25) {
Toast.makeText(MainActivity.this, "您的体重超重,注意减肥哦!", Toast.LENGTH_SHORT).show();
} else if (result < 35 && result > 30) {
Toast.makeText(MainActivity.this, "你的体重肥胖,马上开始减肥吧!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "您的体重严重肥胖,注意注意锻炼节食!", Toast.LENGTH_SHORT).show();
}
} else//选择的是女性
{
if (result <= 19) {
Toast.makeText(MainActivity.this, "您的体重过轻,吃吃吃!", Toast.LENGTH_SHORT).show();
} else if (result <= 24 && result > 19) {
Toast.makeText(MainActivity.this, "您的体重适中,继续保持!", Toast.LENGTH_SHORT).show();
} else if (result <= 29 && result > 24) {
Toast.makeText(MainActivity.this, "您的体重超重,注意减肥哦!", Toast.LENGTH_SHORT).show();
} else if (result < 34 && result > 29) {
Toast.makeText(MainActivity.this, "你的体重肥胖,马上开始减肥吧!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "您的体重严重肥胖,注意注意锻炼节食!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
//查看详情页面的跳转以及信息的处理
class myListener implements CompoundButton.OnClickListener{
@Override
public void onClick(View v) {
//得到身高体重
String height = heightText.getText().toString();
String weight = weightText.getText().toString();
String msg = "";
String bmi = "";
double result = 0, heightNum = 0, weightNum = 0;
if (!height.isEmpty() && !weight.isEmpty()) {
heightNum = Double.parseDouble(height);
weightNum = Double.parseDouble(weight);
DecimalFormat df = new DecimalFormat("#.00");
result = 10000 * weightNum / (heightNum * heightNum);
bmi = df.format(result);
System.out.println(bmi);
} if (height.isEmpty() || weight.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入身高体重", Toast.LENGTH_SHORT).show();
}
Log.i("bmi",bmi);
msg = "您的bmi值是:".toString();
if (boy.isChecked()) {//如果选择的是男性
if (result <= 20) {
msg = msg +bmi +"\n" +"您的体重过轻,吃吃吃!".toString();
} else if (result <= 25 && result > 20) {
msg+=bmi +"\n" +"您的体重适中,继续保持!".toString();
} else if (result <= 30 && result > 25) {
msg+=bmi +"\n" +"您的体重超重,注意减肥哦!".toString();
} else if (result < 35 && result > 30) {
msg+=bmi +"\n" +"你的体重肥胖,马上开始减肥吧!".toString();
} else {
msg+=bmi +"\n" +"您的体重严重肥胖,注意注意锻炼节食!".toString();
}
} else//选择的是女性
{
if (result <= 19) {
msg+=bmi +"\n" +"您的体重过轻,吃吃吃!".toString();
} else if (result <= 24 && result > 19) {
msg+=bmi +"\n" +"您的体重适中,继续保持!".toString();
} else if (result <= 29 && result > 24) {
msg+=bmi +"\n" +"您的体重超重,注意减肥哦!".toString();
} else if (result < 34 && result > 29) {
msg+=bmi +"\n" +"你的体重肥胖,马上开始减肥吧!".toString();
} else {
msg+=bmi +"\n" +"您的体重严重肥胖,注意注意锻炼节食!".toString();
}
}
Intent intent = new Intent();
intent.setClass(MainActivity.this,resultActivity.class);
intent.putExtra("msg",msg);//将提示信息传递到result_activity
MainActivity.this.startActivity(intent);
}
}
}
resultActivity:
public class resultActivity extends AppCompatActivity {
TextView textView;
private Button btn_back;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_result);
textView = (TextView)findViewById(R.id.bmi_result);
Intent intent = getIntent();
String msg = intent.getStringExtra("msg");
textView.setText(msg);
btn_back = (Button)findViewById(R.id.back);
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(resultActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高(cm)"
android:textSize="40dp"/>
<EditText
android:id="@+id/H"
android:layout_width="200dp"
android:layout_height="match_parent"
/>
></LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体重(kg)"
android:textSize="40dp"/>
<EditText
android:id="@+id/W"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
></LinearLayout>
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="90dp"
android:layout_marginTop="15dp">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="30dp"
/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="女"
android:textSize="30dp"
/>
></RadioGroup>
<Button
android:layout_marginTop="17dp"
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算"
android:textSize="17dp"
/>
<Button
android:id="@+id/b2"
android:layout_marginTop="17dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重置"
android:textSize="17dp"
/>
>
<Button
android:id="@+id/go"
android:layout_marginTop="77dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看详情"/>
layout_result.xml:
<TextView
android:layout_marginTop="99dp"
android:textSize="27dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bmi_result"
/>
<Button
android:id="@+id/back"
android:layout_marginTop="157dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"
/>