【问题标题】:Android changing the TextView from another activityAndroid 从另一个活动更改 TextView
【发布时间】:2014-06-08 22:32:17
【问题描述】:

我有两个活动,其中主要活动有一个 TextView,可以在第二个活动的 sharedPreference 中进行更改。当用户想要更改或“保存”字符串时,它会将其保存在 SP 文件中,然后返回到主活动。但是,TextView 不会更改为新的并显示旧的。该应用需要重新启动才能运行。

我的目标是在系统 onResume 上使用活动生命周期,但这根本没有拾取新字符串。

我在问如何在保存/从第二个活动返回时更改 TextView。 有问题的行是: checkboxmessage = (sp.getString("checkboxdescr", ""));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    //
    //  sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
    sp = getSharedPreferences("contactapp", 0);
    // named preferences - get the default ones and finish with it


    //SET TEXTS


    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    TextView tv = (TextView) findViewById(R.id.sexycheckbox);
    tv.setText(checkboxtext);

    CheckBox cb = (CheckBox) findViewById(R.id.sexycheckbox);

    ////TOPCLEAR
    TextView tt = (TextView)findViewById(R.id.toptext2);
    tt.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            EditText et = (EditText)findViewById(R.id.edit_name);
                    EditText et2 = (EditText)findViewById(R.id.edit_number);
                    et.setText("");
                    et2.setText("");
                    CheckBox sexycheck;
                     sexycheck = (CheckBox)findViewById(R.id.sexycheckbox);
                    if (sexycheck.isChecked()) {
                        sexycheck.setChecked(false);
                    }
        }
    });

}

protected void onResume(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    sp = getSharedPreferences("contactapp", 0);

    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    TextView tv1 = (TextView) findViewById(R.id.sexycheckbox);
    tv1.setText(checkboxtext);
}

【问题讨论】:

    标签: android android-intent sharedpreferences android-lifecycle


    【解决方案1】:

    像这样更改你的代码,看看它是否有效:

    @Override
    protected void onResume() {
    
        super.onResume();
    
        tv1.setText("new Text test");
    }
    

    【讨论】:

    • 我确实尝试过,在第二个活动中保存字符串后,它仍然保持不变。
    • 我无法覆盖该方法。 @Override 给我一个错误:mainactivity 类型的 onResume(bundle) 方法必须覆盖或实现超类型方法。基本上复制你的代码,但也有 TextView tv1 = (TextView) findViewById(R.id.sexycheckbox);找到文本视图。
    • 在没有捆绑的情况下使用 onResume()
    • 我明白了。我只需要将我的代码更改为上面的代码并从 SP 中提取字符串来代替“新文本测试”。非常感谢
    【解决方案2】:

    即使你复制粘贴你仍然需要知道你在应对什么。

    替换这些行

    super.onCreate(savedInstanceState); //You call wrong parent method here
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); //you call this in onCreate already
    setContentView(R.layout.activity_main); //you call this in onCreate already
    
    sp = getSharedPreferences("contactapp", 0); //you call this in onCreate already
    

    super.onResume(savedInstanceState);
    

    在 onResume() 方法中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多