一、传递基本类型数据
(一)编辑MainActivity
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;
public class MainActivity extends AppCompatActivity {
Button btn_submit;
EditText name_edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_submit = (Button) findViewById(R.id.btn_submit);
name_edit = (EditText) findViewById(R.id.name_edit);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent intent = new Intent();
intent.setClass(MainActivity.this,InfoActivity.class);
String name = name_edit.getText().toString();
intent.putExtra("name",name);
startActivity(intent);
}
});
}
}
(二)编辑InfoActivity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class InfoActivity extends AppCompatActivity {
TextView infoshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
infoshow = (TextView) findViewById(R.id.infoshow);
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
infoshow.setText(name);
}
}
(三)布局
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
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">
<EditText
android:id="@+id/name_edit"
android:layout_width="match_parent"
android:layout_height="43dp"
tools:layout_editor_absoluteX="156dp"
tools:layout_editor_absoluteY="64dp"
android:hint=" 姓名"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="268dp"
android:layout_y="209dp"
android:text="查看"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="123dp"
android:layout_weight="1"/>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?