Android:activity数据传递,详细讲解+超多图~

向下一个Activity传递数据

1、创建Activity

2、SendActivity发送数据,ReceiveActivity接收数据

3、manifest清单文件设置(SendActivity为主界面,如果是直接创建class文件的话记得注册!仿造框一就可以了,没有注册会导致跳转崩溃哦)

4、打开布局文件

activity_receive.xml添加TextView,作为数据显示的位置

activity_send.xml增加一个TextView跟一个Button,TextView是数据内容,点击Button进行跳转,并且获取TextView的文本内容

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SendActivity">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World"
        android:textSize="26sp" />

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送数据" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveActivity">

    <TextView
        android:id="@+id/tv_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="26sp" />

</LinearLayout>

5、SendActivity.java进行的操作

1)绑定控件ID

2)监听按钮

3)初始化意图(Intent)并且定义从当前活动跳转到目标活动

4)初始化Bundle

5)使用putString把数据放入Bundle当中

6)把Bundle放入意图

7)开启跳转

代码:

public class SendActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_content;
    private Button btn_send_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        tv_content = findViewById(R.id.tv_content);
        btn_send_date = findViewById(R.id.btn_send_data);
        btn_send_date.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this,ReceiveActivity.class);
        Bundle bundle=new Bundle();
        //传递的数据自己定义,我这边传递的数据是id为tv_content的文本内容
        bundle.putString("request_content",tv_content.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

小技巧:监听控件的时候,输入this提醒错误

移到报错这个位置,Alt+回车,选择第二个,as自动补全需要的代码,不用每次自己敲

 6、ReceiveActivity.java进行的操作

1)绑定控件ID

2)调用getExtras方法,获取数据

3)获取key为request_content的数据,key要去SendActivit命名一样,才能获取到数据

4)将获取到的数据,格式化为字符串

5)把获取到的数据写入到TextView当中

代码:

public class ReceiveActivity extends AppCompatActivity {

    private TextView tv_receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_receive_acitivity);
        tv_receive = findViewById(R.id.tv_receive);
        Bundle bundle=getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        Log.e("Tag",request_content);
        String decs =String.format(request_content);
        tv_receive.setText(decs);
    }
}

效果图:





 向上一个Activity传递数据

先传一下代码,后面有时间了再补充一下解释~

startActivityForResult();弃用了,用ActivityResultLauncher回传数据

public class RequestActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_request;
    private TextView tv_response;
    private Button btn_request;
    private static final String mRequest = "哈喽哈喽在干嘛!";
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);
        tv_request = findViewById(R.id.tv_request);
        btn_request = findViewById(R.id.btn_request);
        tv_response = findViewById(R.id.tv_response);
        btn_request.setOnClickListener(this);
        tv_request.setText("待发送的信息为:" + mRequest);
        btn_request.setOnClickListener(this);
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null) {
                Intent intent =result.getData();
                if (result != null && result.getResultCode() == Activity.RESULT_OK) {
                    Bundle bundle = intent.getExtras();
                    String response_content = bundle.getString("response_content");
                    String decs = String.format(response_content);
                    tv_response.setText("收到回复信息:"+decs);
                }
            }

        });
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, ResponseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_content", mRequest);
        intent.putExtras(bundle);
        intentActivityResultLauncher.launch(intent);
    }
}
public class ResponseActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_response;
    private TextView tv_request;
    private Button btn_response;
    private static final String mResponse="我在学习呢";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_response);
        tv_response = findViewById(R.id.tv_response);
        tv_request = findViewById(R.id.tv_request);
        btn_response = findViewById(R.id.btn_response);
        tv_response.setText("待应答的消息:"+mResponse);
        Bundle bundle=getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        String decs=String.format(request_content);
        tv_request.setText("收到信息:"+decs);
        btn_response.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_content",mResponse);
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }
}
<?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:gravity="center"
    android:orientation="vertical"
    tools:context=".RequestActivity">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送请求数据"
        android:textSize="26sp" />

    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".ResponseActivity">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回应答数据"
        android:textSize="26sp" />


    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/m0_58241002/article/details/124962223