android--跳转与数据传递

java代码

 1 package com.example.myapplication;
 2   
 3 import androidx.appcompat.app.AppCompatActivity;
 4   
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9   
10 public class AActivity extends AppCompatActivity {
11     private Button btn_a;
12   
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_a);
17         btn_a=findViewById(R.id.btn_a);
18         btn_a.setOnClickListener(new View.OnClickListener() {
19             @Override
20             public void onClick(View view) {
21                 Intent intent=new Intent(AActivity.this,BActivity.class);
22                 Bundle bundle=new Bundle();
23                 bundle.putString("name","zhangsan");
24                 bundle.putInt("number",20);
25                 intent.putExtras(bundle);
26                 startActivity(intent);
27             }
28         });
29     }
30 }

界面一 xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6   <Button
 7       android:id="@+id/btn_a"
 8       android:layout_marginTop="200dp"
 9       android:layout_width="wrap_content"
10       android:layout_height="wrap_content"
11       android:text="跳转到下一界面"
12       android:layout_gravity="center"/>
13   <TextView
14       android:layout_marginTop="50dp"
15       android:layout_width="match_parent"
16       android:layout_height="wrap_content"
17       android:text="点击跳转到下一界面 这里会传递两个数:zhangsan,20"
18       android:textSize="20dp"
19       />
20 </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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3   
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7   
 8     <TextView
 9         android:id="@+id/tv_b"
10         android:layout_marginTop="200dp"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:gravity="center"
14         android:textColor="#000000"
15         android:textSize="20dp"
16         />
17   
18 </LinearLayout>

 

posted @ 2020-12-20 17:15  刘显栋  阅读(96)  评论(0编辑  收藏  举报