第一个界面布局文件
<?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="com.example.hzx.intenttoclass.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到第二Activity"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第一个界面"
/>
</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"
tools:context="com.example.hzx.intenttoclass.Main2Activity"
android:orientation="vertical">
<TextView
android:id="@+id/show_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
第一个Activity
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Person person = new Person();
person.setName("Tom");
person.setAge(20);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("person_data",person);
startActivity(intent);
}
});
}
}
第二个Activity
public class Main2Activity extends AppCompatActivity {
private TextView show_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Person person = (Person)getIntent().getSerializableExtra("person_data");
show_text = (TextView)findViewById(R.id.show_text);
show_text.setText("name:"+person.getName()+"\n"+"age:"+person.getAge());
}
}
展示效果
当点击Button时会跳转到的第二个Activity,且会传入对象
学习完这张后可以在学习
https://www.jianshu.com/p/c0082e17843a