从另一项活动更改文本 [英] Changing text from another activity

查看:102
本文介绍了从另一项活动更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态地从屏幕的另一部分改变一个TextView的内容?

How to dynamically change the content of a TextView from another part of the screen?

我有画,其中包含一个TextView其次是有几个选项卡的RelativeLayout的一个TabActivity类。在每个标签都是一个独立的意图。从标签意图之一,我想从父TabActvity改变TextView的文本(通过.setText)。

I have a TabActivity class that draws a RelativeLayout that contains a TextView followed by a with several tabs. Within each tab is a separate Intent. From one of the tab intents, I would like to change the text (via .setText) of the TextView from the parent TabActvity.

这可能吗?

推荐答案

是的,它是可能的...

Yes it is possible ...

请您私人的TextView 公共静态的TextView 比你将能够从任何其他Acivity访问,也你可以做的setText()从任何其他活动,但要确保你初始化的TextView ,然后在其他使用对象活动...

Make your private TextView to public static TextView than you will be able to access from any other Acivity and also you can do setText() from any other activity, but make sure you initialize that TextView object before you use in other Activity ...

下面是演示...

这是你的TextView的第一项活动。

This is you first Activity with TextView

public class ActivityWithTextView extends Activity { 

    public static TextView textViewObj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);

            textViewObj = (TextView) findViewById(R.id.textviewid);
    }
}  

现在在第二个......

Now in second one ....

public class AnotherActivity extends Activity {             
        ....
        ....

        ActivityWithTextView.textViewObj.setText("Your text");

        .....
        .....
}  

这篇关于从另一项活动更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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