Android-----------跳转与数据传递
界面一Java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class AActivity extends AppCompatActivity { private Button btn_a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); btn_a=findViewById(R.id.btn_a); btn_a.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(AActivity.this,BActivity.class); Bundle bundle=new Bundle(); bundle.putString("name","zhangsan"); bundle.putInt("number",20); intent.putExtras(bundle); startActivity(intent); } }); } }
界面一XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_a" android:layout_marginTop="200dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到下一界面" android:layout_gravity="center"/> <TextView android:layout_marginTop="50dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击跳转到下一界面 这里会传递两个数:zhangsan,20" android:textSize="20dp" /> </LinearLayout>
界面二JAVA
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class BActivity extends AppCompatActivity { private TextView tv_b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); tv_b=findViewById(R.id.tv_b); Bundle bundle=getIntent().getExtras(); String name = bundle.getString("name"); int number = bundle.getInt("number"); tv_b.setText(name+","+number); } }
界面二XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_b" android:layout_marginTop="200dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#000000" android:textSize="20dp" /> </LinearLayout>