我在我的 Application 类中创建了一些 getter setter,如下所示:
public class myApp extends Application{
//"Global" variables
private Boolean musicEnabled = true; //Music on or off?
private Boolean soundEnabled = true; //Sound effects on or off?
@Override
public void onCreate() {
// TODO Auto-generated method stub
musicEnabled = true; //Default value for music
soundEnabled = true; //Default value for sound effects
super.onCreate();
}
//Getter and setter for musicEnabled
public Boolean getMusicOption(){
return musicEnabled; //Getter
}
public void setMusicOption(Boolean value){ //Setter
musicEnabled = value;
}
//Getter and setter for soundEnabled
public Boolean getSoundOption(){
return soundEnabled;
}
public void setMusicOptions(Boolean value){
soundEnabled = value;
}
}
然后我得到我的 Activity 类中的值,如下所示:
myApp myAppSettings = (myApp)getApplicationContext();
musicEnabled = myAppSettings.getMusicOption();
soundEnabled = myAppSettings.getSoundOption();
这很好,但我不知道如何从相应的 surfaceView 类中找到它们并使用它们?即开始的课程:
public class mySView extends SurfaceView implements
SurfaceHolder.Callback {
到目前为止,我设法做到这一点的唯一方法是通过创建如下方法将它们传递到我的 surfaceview 类中:
public void initialise(Boolean Sound, Boolean Music){
}
然后像这样从我的 Activity 类中传递这些:
myView.initialise(musicEnabled, soundEnabled).
这可行,但是看起来有点乱,我的意思是我将需要使用我的“myView”类中的设置器来设置这些值,所以........无论如何我可以直接从我的'myView' 类还是我必须从 Activity 类中执行此操作?
谢谢大家