2012-10-05 83 views
1

在我的应用程序中,我想要一个设置页面,您可以在其中将应用程序中按钮的颜色设置为绿色,蓝色或红色。我可以使用SharedPreferences来做到这一点吗?如果是这样,假设我在我的共享首选项中将颜色保存为“BUTTON_COLOR”。我怎样才能回想起我的活动中设置按钮颜色的设置?多谢你们。Android:如何从设置中设置按钮颜色

回答

2

在应用程序中创建按钮的任何位置,您都必须检查SharedPreference的值,并适当设置按钮颜色。

保存偏好:

PreferenceManager.getDefaultSharedPreferences(activity).edit().putInt("COLOR",color); 

再次读出来(在哪里getInt()的第二个参数是色彩的默认值):

PreferenceManager.getDefaultSharedPreferences(activity).getInt("COLOR",Color.BLACK); 

欲了解更多信息,请参阅: http://developer.android.com/reference/android/content/SharedPreferences.html

1

Android SDK提供SharedPreferences类到setget应用程序首选项。

这些首选项适用于少量数据,并且适用于所有数据类型(包括String)的方法。

卸载应用程序时会删除首选项。或者,如果用户转到他们的设备设置,找到应用程序并选择“清除缓存”按钮。

可以设置首选项是这样的:

SharedPreferences get = getSharedPreferences("MyApp", Context.MODE_PRIVATE); 
SharedPreferences.Editor set = get.edit(); 

set.putInt("BUTTON_COLOR", 0xFF000000); 
set.commit(); // You must call the commit method to set any preferences that you've assigned. 

而且你可以检索他们这样说:

get.getInt("BUTTON_COLOR", 0xFF000000); // A preference will never return null. You set a default value as the second parameter.