1.介绍
2.相关属性
(1)启动Activity
(2)Intent介绍
(3)关闭Activity
3.多个页面之间传递数据(页面1向页面2传递数据,单向传递数据)
(1)相关属性
注意:data为str的代号
(2)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=".MainActivity"> <TextView
android:id="@+id/tv_weight"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="这是标准体重计算界面"
android:textSize="30dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别" /> <RadioGroup
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"> <RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" /> <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <TextView
android:id="@+id/tv_userheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入您的身高" /> <EditText
android:id="@+id/et_userheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"> <Button
android:id="@+id/bt_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" />
</LinearLayout> </LinearLayout>
第二个界面
<?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=".MainActivity"> <TextView
android:id="@+id/textView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="这是第二个界面"
android:textSize="30dp" /> <Button
android:id="@+id/bt_goback"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" /> <EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" /> <EditText
android:id="@+id/et_weightValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>
(3)java后台代码
界面1
package com.lucky.test31activity; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton; public class MainActivity extends AppCompatActivity {
Button bt_calculate;
EditText et_height;
RadioButton radioButton;
String gender; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_calculate=findViewById(R.id.bt_calculate);
et_height=findViewById(R.id.et_userheight);
radioButton=findViewById(R.id.radioButton); bt_calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实现页面之间的跳转
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//若页面之间要传递数据,可以用putExtra()方法,若不需要传递数据则下面两句可以不写
//toString()一定不能少
if(radioButton.isChecked()){
gender="男";
}else {
gender="女";
}
intent.putExtra("003",gender);
intent.putExtra("004",Integer.parseInt(et_height.getText().toString()));
startActivity(intent); //进行页面跳转
}
});
}
}
界面2
package com.lucky.test31activity; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class SecondActivity extends AppCompatActivity {
Button button1;
EditText et_message;
EditText et_userweight;
int userheight;
double userweight;
String gender; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button1=findViewById(R.id.bt_goback);
et_message=findViewById(R.id.et_message);
et_userweight=findViewById(R.id.et_weightValue);
Intent intent=getIntent();//获取Intent
String name=intent.getStringExtra("001");
String pwd=intent.getStringExtra("002");
gender=intent.getStringExtra("003");
userheight=intent.getIntExtra("004",0);
if(gender.equals("男")){
userweight=(userheight-80)*0.7;
}else {
userweight=(userheight-70)*0.6; }
et_message.setText("用户名为:"+name+"\n密码为:"+pwd);
et_userweight.setText("您的体重为:"+userweight); button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();//关闭页面
}
});
}
}
4.页面之间传递数据(页面1与页面2双向传递数据)
(1)介绍
(2) java 后台代码
主界面
package com.lucky.test32activity2; import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends AppCompatActivity {
EditText page1output;
EditText frompage2;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1output=findViewById(R.id.editText_page1output);
frompage2=findViewById(R.id.editText_frompage2);
button1=findViewById(R.id.button_page1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,page2Activity.class);//实例化intent
intent.putExtra("001",page1output.getText().toString()); //设置所传递的数据
startActivityForResult(intent,0x01); //参数2为请求码
}
}); } @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//判断请求码与结果码是否一致
if(requestCode==0x01&&resultCode==0x02){
frompage2.setText(data.getStringExtra("002"));
}
}
}
第二界面
package com.lucky.test32activity2; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class page2Activity extends AppCompatActivity { EditText page1input;
EditText page2return;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
page1input=findViewById(R.id.editText_page1input);
page2return=findViewById(R.id.editText_page2return);
button2=findViewById(R.id.button_page2);
final Intent intent=getIntent();
page1input.setText(intent.getStringExtra("001"));
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=new Intent(); //实例化intent
intent1.putExtra("002",page2return.getText().toString()); //设置数据,toString()不能省略
setResult(0x02,intent1); //从第二界面返回第一界面
finish(); //关闭页面
}
}); }
}
5.效果图
动态1对应工程名:test31,动态2对应工程名:test32