【问题标题】:Android Studio textview text isn't changing when I use setText in custom method当我在自定义方法中使用 setText 时,Android Studio textview 文本没有改变
【发布时间】:2015-06-07 17:06:21
【问题描述】:

我正在使用以下来更改符号:

private void updateCurrencySymbol() {
    SharedPreferences sharedPreferences  = PreferenceManager.getDefaultSharedPreferences(this);

    String symbol = sharedPreferences.getString("preferences_currency", "$");
    Toast.makeText(getApplicationContext(),symbol,Toast.LENGTH_LONG).show();
    currencySymbol1 = (TextView) findViewById(R.id.currencySymbol1);
    currencySymbol2 = (TextView) findViewById(R.id.currencySymbol2);
    currencySymbol1.setText(symbol);
    currencySymbol2.setText(symbol);
    //refreshes the activity
    Bundle temp_bundle = new Bundle();
    onSaveInstanceState(temp_bundle);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("bundle", temp_bundle);
    startActivity(intent);
    finish();
}

在 Toast 中,它显示了我从设置中选择的正确符号,但在实际的 TextView 中它永远不会改变。我尝试更改 onCreate() 方法中的文本,它可以工作,更改为我指定的字符串。

【问题讨论】:

  • 你想做什么?将数据传递给另一个活动?
  • 不,这只是一个在不同货币之间进行选择的设置(实际上只有一个符号会改变)。然后我尝试将 TextView 设置为选择的符号并刷新活动(将所有数据保留在那里的字段中)。
  • 每次都得到'$'符号的原因是因为你的共享首选项是空的。在刷新活动之前,您需要先保存数据。看到这个:developer.android.com/training/basics/data-storage/…

标签: java android android-studio textview settext


【解决方案1】:

它不会改变,因为 SharedPreferences 中的数据不会改变。无需重新启动活动。根本不需要。只需 setText() 文本视图。然后保存到 sharedPrefrences 以供以后开始使用。

【讨论】:

  • 即使不重新启动活动,TextView 也不会改变,我将它保存到我在代码的另一部分中的共享首选项中。
  • 如果文本没有改变,那么您没有在其他地方提交更改。你在哪里更改字符串。
  • 问题解决了,原来只有一个符号被提交了,另一个没有,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多