如何从另一个活动的TextView没有来自或到活动? [英] How to get TextView from another Activity without coming from or going to that Activity?

查看:99
本文介绍了如何从另一个活动的TextView没有来自或到活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在活动A一个TextView,并且是用户花费大部分时间上的应用程序。我的应用程序使用共享preferences保存在活动C.一个TextView
我如何从活动C时的TextView而不去活动c当我打开活动A和/或B.
我知道我可以从活动Ç得到TextView的意图,但我认为只有工作,如果我是从活动Ç对此我不来了。

I have a TextView in Activity A and that is where the user spends most of the time on the app. My app uses Shared Preferences to save a TextView in Activity C. How do I get the TextView from Activity C without going to activity C when I load Activity A and/or B. I know I can get the TextView from Activity C with intent but I think that only works if I'm coming from Activity C which I am not.

活动当前获得的TextView的这样

Activity A currently get's the TextView this way

Intent id = getIntent();
if (id.getCharSequenceExtra("idid") != null) {
final TextView setmsg = (TextView)findViewById(R.id.loginid2);
setmsg.setText(id.getCharSequenceExtra("idid"));                

}

但如果另一个活动中使用putExtra得到它有这仅适用。

But this only works if another Activity used putExtra to get it there.

推荐答案

从这个问题,我的理解是,

From the question, what I understood is,

每当我加载活动A / B,其文本将显示从活动C.价值难道不是吗?

Whenever I load Activity A / B , its Text will show the value from Activity C. Isn't it?

当然,我们可以使用共享preference。试试这个:

Of course we can use SharedPreference. try this:

活动内部C - >

inside Activity C ->

textview.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ActivityC.this);
SharedPreferences.Editor editor = pref.edit();
    editor.putString("idid", ""+s);
    editor.commit();

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

而现在,内部活动A的onCreate - >

And now, inside Activity A onCreate ->

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ActivityA.this);
setmsg.setText(pref.getString("idid", "null"));

这就是它。

这篇关于如何从另一个活动的TextView没有来自或到活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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