0

我有一个活动,它有计数按钮加减,这样它会增加或减少editText中的值,一个非负数。这样 int 值通过 setTextColor 属性设置为颜色。该值通过 Shared Preference 保持不变。我的问题是当再次进入应用程序时如何在 editText 中保持“彩色”绿色或红色值?

4

1 回答 1

1

假设您的 EditText 名为mEditText. 首先设置一个 SharedPreferences 对象:

mSharedPreferences = getSharedPreferences("Preferences File Name", MODE_PRIVATE);

保存颜色(可能在 onDestroy() 或颜色改变时):

SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt("Text Color", mEditText.getTextColors().getDefaultColor());
editor.commit();

如果没有保存数据(可能在 onCreate() 中)读取保存的值,默认值为黑色:

mEditText.setTextColor(mSharedPreferences.getInt("Text Color", 0xff000000));
于 2012-08-12T19:36:56.823 回答