【发布时间】: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