基本流程和上一篇一样
地址: https://www.cnblogs.com/yansans/p/18235968
只是再最后加了一句代码
intent.putExtra("name",name.getText().toString());
这句代码用键值对比较好理解,是将从前端传递来的name的值命名为“name”并传递给MainActivity2
MainActivity2的后端代码是
package zy.test1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
//控件
private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Init();
}
private void Init() {
textView = findViewById(R.id.zhanshi);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
textView.setText("用户为:" + name);
}
}
其中最主要的是
Intent intent = getIntent();
String name = intent.getStringExtra("name");
textView.setText("用户为:" + name);
这里的几句
意思是接收上一个界面传来的名为name的值,并存储MainActivity2的前端界面
MainActivity2的前端代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 嗨嗨嗨!!! "
android:textSize="35sp"
app:layout_constraintBottom_toTopOf="@+id/zhanshi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />
<TextView
android:id="@+id/zhanshi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 用户"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.135"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.416" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果
标签:layout,name,parent,app,studio,aandroid,intent,android From: https://www.cnblogs.com/yansans/p/18235987