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条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划