(案例)实现活动之间的跳转:测BMI指数
创建第一个布局文件,名为activity_result.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".ResultActivity">
<TextView
android:id="@+id/show"
android:text="测一测你的BMI指数?"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/show2"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/back"
android:text="测试"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
效果如图:
在AndroidManifest中,将intent-filter写入到程序名为ResultActivity中,代表为主入口
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication3">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication3">
<activity
android:name=".ResultActivity"
android:exported="true" >
//action指定活动的主入口行为
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:exported="true">
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
</activity>
</application>
</manifest>
第一个布局文件对应程序名为ResultActivity.java,代码如下:
package com.example.myapplication3;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//根据id知道按钮
Button button=findViewById(R.id.back);
//给按钮设置监听事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(ResultActivity.this,DetailActivity.class);
//启动活动,期望在活动销毁时能返回一个结果给上一个活动
startActivityForResult(intent,0x12);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0x12 && resultCode==0x11){
//获取数据
Bundle bundle=data.getExtras();
String text=bundle.getString("data");
double BMI=bundle.getDouble("BMI");
TextView textView=findViewById(R.id.show);
textView.setText("你的BMI指数为:"+BMI+"\n"+text);
}
}
}
建立第二个布局文件,名为activity_detail.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DetailActivity">
<TextView
android:textSize="24sp"
android:text="身高(cm)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:textSize="24sp"
android:id="@+id/height"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:textSize="24sp"
android:text="体重(kg)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:textSize="24sp"
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
//单选组件与单选按钮
<RadioGroup
android:id="@+id/sex"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:layout_margin="30dp"
android:text="男"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="@+id/female"
android:text="女"
android:layout_margin="30dp"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<Button
android:id="@+id/btn"
android:text="计算BMI值"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
效果如下:
第二个布局文件对应程序名为DatailActivity.java,代码如下:
package com.example.myapplication3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
EditText height=findViewById(R.id.height);
EditText weight=findViewById(R.id.weight);
RadioGroup sex=findViewById(R.id.sex);
Button btn=findViewById(R.id.btn);
//给按钮设置监听事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取数据
//ToString的用法
//比如当定义一个int类型后,利用toString()方法就能把int转化为string类型,即把整数类型转换为了字符串。
// 举例即int a= 10;转换后就变成了a=“10”。这里的10不再是整数,他有了双引号,变成了字符串。
String heightText=height.getText().toString();
String weightText=weight.getText().toString();
String sexText="";
for(int i=0;i<sex.getChildCount();i++){
//获取每一个单选按钮
RadioButton radioButton=(RadioButton)sex.getChildAt(i);
if(radioButton.isChecked()){
sexText=radioButton.getText().toString();
break;
}
}
//计算BMI
//Integer是基本数据类型,必须实例化才能使用
double heightData=new Integer(heightText);
heightData=heightData/100;
double weightData=new Integer(weightText);
double BMI=weightData/(heightData*heightData);
String text="";
//使用switch和if条件语句
switch(sexText){
case "男":
if(BMI<20){
text="过轻!";
}else if(BMI<25 && BMI>=20){
text="很棒哦!";
}else if(BMI<30 && BMI>=25){
text="超重!";
}else if(BMI<35 && BMI>=30){
text="有点肥胖!";
}else if(BMI>=35){
text="严重肥胖哦!";
}
//break作用:终止某个case跳出switch语句
break;
case "女":
if(BMI<19){
text="过轻!";
}else if(BMI<24 && BMI>=19){
text="很棒哦!";
}else if(BMI<29 && BMI>=24){
text="超重!";
}else if(BMI<34 && BMI>=29){
text="有点肥胖!";
}else if(BMI>=34){
text="严重肥胖哦!";
}
break;
}
//将结论返回到第一个活动
/*Bundle为字符串与某组件对象建立映射关系(即键对应值)的组件
与Intent配合使用,在不同的活动之间传递数据
*/
Intent intent=getIntent();
Bundle bundle=new Bundle();
bundle.putDouble("BMI",BMI);
bundle.putCharSequence("data",text);
intent.putExtras(bundle);
setResult(0x11,intent);
//关闭当前活动
finish();
}
});
}
}
注:toString方法的用法:
当点击文本名为计算BMI值的button按钮时,出现如下效果:
当点击测试时,自动返回第一张图,如图: