一.Activity跳转
1.选择Empty Views Activity创建项目
2.创建OtherActivity3.修改activity_main.xml
<Button
android:id="@+id/btnJumpOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到other页面"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
4.修改activity_other.xml
<TextView
android:text="Other页面"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Other"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
5.页面跳转实现
public class MainActivity extends AppCompatActivity {
private Button btnJumpOther;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnJumpOther=findViewById(R.id.btnJumpOther);
btnJumpOther.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
}
}
二.Activity带参跳转
带参跳转则需要使用 Intent 的 putExtra(键,值)方法来放入参数,然后在OtherActivity中使用 getIntent().get类型Extra(键)来取得值
MAinActivity按钮的点击事件
只比前面多了一行代码intent.putExtra("key", "我是参数");
public class MainActivity extends AppCompatActivity {
private Button btnJumpOther;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnJumpOther=findViewById(R.id.btnJumpOther);
btnJumpOther.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("key","我是参数");
startActivity(intent);
}
});
}
}
OtherActivity中的代码
public class OtherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
TextView Other=findViewById(R.id.Other);
Intent intent = getIntent();
String value = intent.getStringExtra("key");
Other.setText(value);
}
}
三.Activity带参返回
activity_main.xml
<Button
android:id="@+id/btnJumpMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到other页面"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:text="Hello World"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/receiver"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintVertical_bias="0.427"/>
activity_other.xml
<Button
android:id="@+id/btnJumpMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到Main页面"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/receiver"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.444" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.498"/>
MainActivity中的代码
public class MainActivity extends AppCompatActivity {
// 定义请求码,不同界面的跳转请求码要求不同,为了识别是哪个控件传递数据
public final static int sendUser = 1;
private TextView receiver;
private Button btnJumpOther;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnJumpOther=findViewById(R.id.btnJumpMain);
receiver=findViewById(R.id.receiver);
btnJumpOther.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("key","接收到数据了,现在通知OtherActivity");
// 启动意图(意图,请求码(int)) 请求码最好使用 final static定义 方便识别
startActivityForResult(intent,sendUser);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 如果请求码为 sendUser 返回码 为 RESULT_OK RESULT_OK为系统自定义的int值为 -1
if(requestCode==sendUser&&resultCode==RESULT_OK){
// 在TextView中设置返回信息
receiver.setText(data.getStringExtra("key"));
}
}
}
OtherActivity中的代码
public class OtherActivity extends AppCompatActivity {
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
TextView Other=findViewById(R.id.receiver);
intent = getIntent();
String value = intent.getStringExtra("key");
Other.setText(value);
Button btnJumpMain = findViewById(R.id.btnJumpMain);
btnJumpMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 发送返回数据 key可自己定义,但前后必须一样
intent.putExtra("key","接收到数据了,现在通知MainActivity");
// RESULT_OK 系统定义的int 值 为-1
setResult(RESULT_OK,intent);
// 关闭当前 Activity
finish();
}
});
}
}