如何在Android的surface view类中使用共享首选项字符串值
我在这个应用程序中实现了一个游戏应用程序。我在“首选项”活动中使用共享首选项如何在Android的surface view类中使用共享首选项字符串值,android,surfaceview,sharedpreferences,Android,Surfaceview,Sharedpreferences,我在这个应用程序中实现了一个游戏应用程序。我在“首选项”活动中使用共享首选项 spinnerTheme.setOnItemSelectedListener(new OnItemSelectedListener(){ SharedPreferences prefs1 = getSharedPreferences("TipCalcPreferenceDatabase", 0); Editor e = prefs1.edit(); public void onItemSele
spinnerTheme.setOnItemSelectedListener(new OnItemSelectedListener(){
SharedPreferences prefs1 = getSharedPreferences("TipCalcPreferenceDatabase", 0);
Editor e = prefs1.edit();
public void onItemSelected(AdapterView parent,View v,int position,long id)
{
prefs = getSharedPreferences("TipCalcPreferenceDatabase", 0);
Editor e = prefs.edit();
String str;
if(position==0)
{
str= String.valueOf((R.drawable.image1));
e.putString("TipCalcPropertyName", str);
}
if(position==1)
{
str= String.valueOf((R.drawable.image2));
e.putString("TipCalcPropertyName", str);
}
else if(position==2)
{
str= String.valueOf((R.drawable.ballon_background));
e.putString("TipCalcPropertyName", str);
}
e.commit();
/* Preferences current Theme update START*/
String mySetting = prefs.getString("TipCalcPropertyName", "");
LinearLayout ln=(LinearLayout) findViewById(R.id.LinearLayout);
ln.setBackgroundResource((int) Double.parseDouble(mySetting) );
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
xml文件下面是表面视图
<com.softwares.bird.BirdGame xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_absolute"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000" android:orientation="horizontal">
</com.softwares.bird.BirdGame>
此共享优先级值表示如何传递曲面视图类
提出有价值的建议再过几天我就会对这个问题产生兴趣
提前感谢我实际上遇到了这个问题,因为我的应用程序想要存储一个更复杂的对象,所以我这样做了,我将对象序列化,然后将其存储到一个文件中。然后,我可以从任何活动中检查此文件并获取完整对象。不需要使用prefs和bundle的字符串键垃圾
/**
* @param act - current activity calling the function
* @return false if global Object not set and the dat file is not there or empty
*/
public static void setInFile(Activity act, Object_VO obj) {
((Object) act.getApplication()).setObjectState(obj);
String ser = SerializeObject.objectToString(obj);
if (ser!= null && !ser.equalsIgnoreCase("")) {
SerializeObject.WriteSettings(act, ser, "android.dat");
} else {
SerializeObject.WriteSettings(act, "", "android.dat");
}
}
要序列化和反序列化,您需要这样做:
/**
* Take an object and serialize and then save it to preferences
*
*/
public class SerializeObject {
/**
* Create a String from the Object using Base64 encoding
* @param object - any Object that is Serializable
* @return - Base64 encoded string.
*/
public static String objectToString(Serializable object) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
new ObjectOutputStream(out).writeObject(object);
byte[] data = out.toByteArray();
out.close();
out = new ByteArrayOutputStream();
Base64OutputStream b64 = new Base64OutputStream(out,0);
b64.write(data);
b64.close();
out.close();
return new String(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Creates a generic object that needs to be cast to its proper object
* from a Base64 ecoded string.
*
* @param encodedObject
* @return
*/
public static Object stringToObject(String encodedObject) {
try {
return new ObjectInputStream(new Base64InputStream(
new ByteArrayInputStream(encodedObject.getBytes()), 0)).readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Save serialized settings to a file
* @param context
* @param data
*/
public static void WriteSettings(Context context, String data, String filename){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
//Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
// Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
if(osw!=null)
osw.close();
if (fOut != null)
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Read data from file and put it into a string
* @param context
* @param filename - fully qualified string name
* @return
*/
public static String ReadSettings(Context context, String filename){
StringBuffer dataBuffer = new StringBuffer();
try{
// open the file for reading
InputStream instream = context.openFileInput(filename);
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String newLine;
// read every line of the file into the line-variable, on line at the time
while (( newLine = buffreader.readLine()) != null) {
// do something with the settings from the file
dataBuffer.append(newLine);
}
// close the file again
instream.close();
}
} catch (java.io.FileNotFoundException f) {
// do something if the myfilename.txt does not exits
Log.e("FileNot Found in ReadSettings", "filename = " + filename);
} catch (IOException e) {
Log.e("IO Error in ReadSettings", "filename = " + filename);
}
return dataBuffer.toString();
}
}
最后,您需要创建可以引用的全局对象状态
package com.utils;
/**
* Global class to hold the application state of the object
*
*/
public class ObjectState extends Application {
private Object_VO obj;
public Object_VO getObjectState() {
return this.obj;
}
public void setObjectState(Object_VO o) {
this.obj = o;
}
}
哦,是的,您需要更新您的AndroidManifest.xml文件以包含对ObjectState的引用,因为它引用的应用程序如下所示
<application android:name="com.utils.ObjectState" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<application android:name="com.utils.ObjectState" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>