Curie-87 2013-08-01 02:15 采纳率: 100%
浏览 2595
已采纳

不加载一个activity,如何从这个activity中获取 TextView?

在Activity A中有一个TextView。程序中使用Shared Preferences来把一个TextView 保存在Activity C中。
当加载 Activity A 或者 B时,不去Activity C如何从Activity C中获取TextView?
Activity A 现在用下面的方法来获取 TextView

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

只有另一个 activity 使用 putExtra 来获取的话才能执行。

  • 写回答

2条回答 默认 最新

  • hxn_217 2013-08-02 06:06
    关注

    你可以在 Activity C 中使用 SharedPreference:

    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
    
            }
        });
    

    在 Activity A onCreate 中:

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(ActivityA.this);
    setmsg.setText(pref.getString("idid", "null"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?