码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 关于 registerForActivityResult()的使用方法,不能说详细,只能说略懂得例子


    目录

    1.情况说明

    2.registerForActivityResult()的使用方法

    3.例子

    在第一个activity中

    在第二个activity中

    验证

    验证第二次在 textview界面中,显示数据来源(代码自己编写)


    也可直接下载进行操作:

    第一个:基础跳转示例

    (2条消息) 基础跳转示例-基础跳转示例资源-CSDN文库

    第二个:使用跳转实例

    使用跳转实例-使用跳转实例资源-CSDN文库

    1.情况说明

    startActivityForResult();函数过时

    使用了

     registerForActivityResult()进行了代替

    2.registerForActivityResult()的使用方法

    数据来源

    (2条消息) registerForActivityResult()的使用方法例子_发狂先生的博客-CSDN博客_registerforactivityresult用法

    registerForActivityResult(ActivityResultContracts.TakeVideo())始终为空 - 我爱学习网 (5axxw.com)

    registerForActivityResult 解决 startActivityForResult(Intent!, Int): Unit is deprecated. Deprecated in Java - Android - 大象笔记 (sunzhongwei.com)

    https://blog.csdn.net/weixin_44618862/article/details/98209369

    过时的OnActivityResult替代品-registerForActivityResult源码解析 - 未知用户的博客 (7449.github.io)

    其中最为详细的在

    (2条消息) registerForActivityResult()的使用方法例子_发狂先生的博客-CSDN博客_registerforactivityresult用法

    registerForActivityResult 解决 startActivityForResult(Intent!, Int): Unit is deprecated. Deprecated in Java - Android - 大象笔记 (sunzhongwei.com)

    3.例子

    数据来源

    (2条消息) registerForActivityResult()的使用方法例子_发狂先生的博客-CSDN博客_registerforactivityresult用法

    然后在Studio编写如下(自己加了一些)

     

    在第一个activity中

    在1中得java

    1. package com.example.test1;
    2. import androidx.activity.result.ActivityResultLauncher;
    3. import androidx.activity.result.contract.ActivityResultContracts;
    4. import androidx.appcompat.app.AppCompatActivity;
    5. import android.app.Activity;
    6. import android.content.Intent;
    7. import android.os.Bundle;
    8. import android.provider.MediaStore;
    9. import android.util.Log;
    10. import android.view.View;
    11. import android.widget.Button;
    12. import android.widget.TextView;
    13. public class ChuangShuFrist extends AppCompatActivity {
    14. TextView text_message;
    15. @Override
    16. protected void onCreate(Bundle savedInstanceState) {
    17. super.onCreate(savedInstanceState);
    18. setContentView(R.layout.chuangshufrist);
    19. text_message = (TextView) findViewById(R.id.cshi1);
    20. Button button_transfer1 = findViewById(R.id.cshibu11);
    21. //按钮1
    22. ActivityResultLauncher requestDataLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
    23. result -> {
    24. Log.d("FirstActivity", result.getData().getStringExtra("data_return"));
    25. //tag表示标签信息,第二个参数表示要打印的信息,在这里表示可以在日志中看到该方法的执行
    26. });
    27. button_transfer1.setOnClickListener(new View.OnClickListener() {
    28. @Override
    29. public void onClick(View v) {
    30. Intent intent=new Intent(ChuangShuFrist.this,ChuangShuSecond.class);
    31. requestDataLauncher.launch(intent);//用的是requestDataLauncher
    32. }
    33. });
    34. }
    35. }

    在1得xml中

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent">
    7. <TextView
    8. android:id="@+id/cshi1"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:text="TextView"
    12. android:textSize="20sp"
    13. app:layout_constraintBottom_toBottomOf="parent"
    14. app:layout_constraintEnd_toEndOf="parent"
    15. app:layout_constraintHorizontal_bias="0.498"
    16. app:layout_constraintStart_toStartOf="parent"
    17. app:layout_constraintTop_toTopOf="parent"
    18. app:layout_constraintVertical_bias="0.289" />
    19. <Button
    20. android:id="@+id/cshibu11"
    21. android:layout_width="wrap_content"
    22. android:layout_height="wrap_content"
    23. android:text="Button"
    24. app:layout_constraintBottom_toBottomOf="parent"
    25. app:layout_constraintEnd_toStartOf="@+id/cshibu12"
    26. app:layout_constraintHorizontal_bias="0.228"
    27. app:layout_constraintStart_toStartOf="parent"
    28. app:layout_constraintTop_toTopOf="parent"
    29. app:layout_constraintVertical_bias="0.415" />
    30. <Button
    31. android:id="@+id/cshibu12"
    32. android:layout_width="wrap_content"
    33. android:layout_height="wrap_content"
    34. android:layout_marginEnd="24dp"
    35. android:text="Button"
    36. app:layout_constraintBottom_toBottomOf="parent"
    37. app:layout_constraintEnd_toEndOf="parent"
    38. app:layout_constraintTop_toTopOf="parent"
    39. app:layout_constraintVertical_bias="0.415" />
    40. </androidx.constraintlayout.widget.ConstraintLayout>

    如下图

    在第二个activity中

    在2中得java

    1. package com.example.test1;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.Button;
    7. public class ChuangShuSecond extends AppCompatActivity {
    8. @Override
    9. protected void onCreate(Bundle savedInstanceState) {
    10. super.onCreate(savedInstanceState);
    11. setContentView(R.layout.chuangshusecond);
    12. Button button3=findViewById(R.id.but2);
    13. button3.setOnClickListener(new View.OnClickListener() {
    14. @Override
    15. public void onClick(View v) {
    16. Intent intent=new Intent();
    17. intent.putExtra("data_return","Hello FirstActivity");
    18. setResult(RESULT_OK,intent);
    19. finish();
    20. }
    21. });
    22. }
    23. }

    在2得xml中

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent">
    7. <Button
    8. android:id="@+id/but2"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:text="Button"
    12. app:layout_constraintBottom_toBottomOf="parent"
    13. app:layout_constraintEnd_toEndOf="parent"
    14. app:layout_constraintStart_toStartOf="parent"
    15. app:layout_constraintTop_toTopOf="parent"
    16. app:layout_constraintVertical_bias="0.475" />
    17. </androidx.constraintlayout.widget.ConstraintLayout>

    如下图

    验证

    QQ录屏20221206140057

    验证第二次在 textview界面中,显示数据来源(代码自己编写)

    31

  • 相关阅读:
    Fritzing软件绘制Arduino面包板接线图传感器模块库文件216
    C语言进阶 -- 回调函数以及qsort函数的使用
    天津理工大学计算机考研资料汇总
    android系统耗时关键字
    「学习笔记」Garsia-Wachs 算法
    鸢尾花数据种类预测、分析与处理、scikit-learn数据集使用、seaborn作图及数据集的划分
    CSP-J-2016-海港
    python之获取文件os.path模块
    CSDN前面加空格
    网站服务器怎么部署
  • 原文地址:https://blog.csdn.net/weixin_49492286/article/details/128201633
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号