从活动到片段的文本视图的字符串 [英] String from Activity to Fragment's Text View

查看:65
本文介绍了从活动到片段的文本视图的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个蓝牙读取例程,它为我提供了一个我想打印"到片段的textview中的String.该字符串是实时更新的,因此它不应是一次性事件.

I have a bluetooth reading routine, and it gives me a String that I would like to "print" into a fragment's textview. The string is updated in real time, so it shall not be an onetime event.

有趣的是,我可以将TextView文本从活动"传递到片段的TextView.然后,将字符串放入Activity TextView,然后从Activity TextView到Fragment Textview.

Funny is that I can pass a TextView text from the Activity to the fragment's TextView. Then I put the String into the Activity TextView, and then from the Activity TextView to the Fragment Textview.

活动文本视图为0dpx0dp,因此没有人看到它.

The Activity Textview is 0dpx0dp so nobody will see it.

如何将字符串直接从MainActivity传递到片段的TextView?

How could I pass the String directly from the MainActivity to the Fragment's TextView?

这是在活动中:

        textAmanheceMenos.setText(dadosTratadosB);

这在片段中:

        TextAmanheceMenos = (TextView) getActivity().findViewById(R.id.textAmanheceMenos);

        String MSG = TextAmanheceMenos.getText().toString();
        TextAmanheceEm.setText(MSG);

这正在工作.但是我必须在活动中创建该幽灵textview.必须删除活动中的"textAmanheceMenos"文本视图.字符串"dadosTratadosB"必须直接进入"TextAmanheceEm.setText(dadosTratadosB)".但这是行不通的.上面写着无法解析符号"

This is working. But I had to create that ghost textview in the activity. The "textAmanheceMenos" textview in the activity must be eliminated. The String "dadosTratadosB" must go directly into the "TextAmanheceEm.setText(dadosTratadosB)". But that doesn't work. It says "cannot resolve symbol"

即使我创建:

 DadosTratadosB = getActivity().findViewById(dadosTratadosB); 

我该如何使其发挥作用?

How could I make it work guys?

谢谢!

推荐答案

创建一个Intent以传递数据.请参见此处

Create an Intent to pass data. See here

尝试下面的代码从真实数据即将到来的Activity中传递值

Try below code for passing value from Activity where real data is coming

private void forwardActivityResult(Fragment f, int requestCode, int resultCode, Intent data) {
    if (f != null) {
            f.onActivityResult(requestCode, resultCode, data);              
        }
    }
}

fragment内,您可以从Intent data获取值,并使用data.getStringExtra

Inside fragment you can get value from Intent data and set it on TextView using data.getStringExtra

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);

    }

这篇关于从活动到片段的文本视图的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆