我有一个MainActivity,它创建了MyOwnTextToSpeech对象,它负责一些tts的东西。它还必须操纵一些UI视图(例如,在某些TextView中设置文本)。我该如何实现UI的这种操作?将MainActivity的引用传递给MyOwnTextToSpeech(以及其他对象)并在MainActivity中准备一些公共方法来操作UI或者我应该为所有视图(TextView等)创建getter来访问它们是不错的主意?或者也许有一种方法不传递对MainActivity的引用(我在某处读到,因为一些内存泄漏的东西,传递对Activities的引用不好)?
更新
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyOwnTextToSpeech myTts = new MyOwnTextToSpeech(context);
}
}
我的班级负责tts。
public class MyOwnTextToSpeech implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
public MyOwnTextToSpeech (Context ctx) {
tts = new TextToSpeech(ctx, this);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
}
@Override
public void onError(String utteranceId) {
}
@Override
public void onDone(final String utteranceId) {
// here I want some UI update stuff
}
});
int result = tts.setLanguage(new Locale("pl", "PL"));
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}