Android 当我在自定义方法中使用 setText 时,Studio textview 文本没有改变
Android Studio textview text isn't changing when I use setText in custom method
我正在使用以下方法更改符号:
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() 方法中的文本并且它有效,更改为我指定的字符串。
它不会改变,因为 SharedPreferences 的数据没有改变。无需重新启动activity。根本不需要。只需 setText() 文本视图。然后保存到 sharedPrefrences 以备将来使用。
我正在使用以下方法更改符号:
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() 方法中的文本并且它有效,更改为我指定的字符串。
它不会改变,因为 SharedPreferences 的数据没有改变。无需重新启动activity。根本不需要。只需 setText() 文本视图。然后保存到 sharedPrefrences 以备将来使用。