温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

TextView在Fragment间如何传递数据

发布时间:2024-08-21 10:21:18 来源:亿速云 阅读:84 作者:小樊 栏目:移动开发

要在Fragment之间传递数据,可以通过Bundle来传递数据。以下是一个示例代码:

在发送方Fragment中:

// 创建一个Bundle对象
Bundle bundle = new Bundle();
// 将数据放入Bundle
bundle.putString("key", "value");

// 创建一个接收方Fragment对象
ReceiverFragment receiverFragment = new ReceiverFragment();
receiverFragment.setArguments(bundle);

// 切换到接收方Fragment
getFragmentManager().beginTransaction()
    .replace(R.id.container, receiverFragment)
    .addToBackStack(null)
    .commit();

在接收方Fragment中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // 获取传递过来的数据
    Bundle bundle = getArguments();
    if (bundle != null) {
        String value = bundle.getString("key");
        // 使用数据进行操作
        textView.setText(value);
    }

    return view;
}

通过这种方式,可以在Fragment之间传递数据并在接收方Fragment中获取并使用这些数据。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI