2012-08-12 27 views
0

我有一个活动,有计数按钮加号和减号,使它增加或减少editText中的值,一个非负号。这样int值被setTextColor属性设置为颜色。该值通过共享偏好持续存在。我的问题是如何在再次输入应用程序时在editText中保留“彩色”绿色或红色值?如何在edittext中保留文本的颜色?

回答

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)); 
相关问题