简体   繁体   English

将数据从活动传递到非活动类而不离开当前活动?

[英]Passing data from activity to non-activity class without leaving current activity?

I have an tts non-UI class and activity class.Now in the activity class i have a button from which user can either either male or female voice depending on the selection i want to pass on the string to tts class and once tts class receives the string based on the string received it sets the float variable to particular value and that float variable is then used in setPitch() method of tts. 我有一个tts非UI类和活动类。现在在活动类中,我有一个按钮,用户可以根据选择将男声或女声从该按钮传递给tts类,一旦tts类收到基于收到的字符串的字符串,它将float变量设置为特定值,然后将该float变量用于tts的setPitch()方法中。 So i was wondering if there is any way to pass on the string to this tts class.I know how to pass string between activities but here i want to pass data from my activity class to an non activity class without leaving my activity class.Thank You. 所以我想知道是否有任何方法可以将字符串传递给此tts类。我知道如何在活动之间传递字符串,但是在这里我想不离开活动类就将数据从活动类传递给非活动类。您。

My TextSpeaker(texttospeech) class is as follows: public class TextSpeaker implements OnInitListener{ 我的TextSpeaker(texttospeech)类如下:公共类TextSpeaker实现OnInitListener {

TextToSpeech tts;
public TextSpeaker(Context c){
    tts = new TextToSpeech(c,this);
}

@Override
public void onInit(int status) {
    if(status != TextToSpeech.ERROR){
        tts.setLanguage(Locale.US);
tts.setPitch(1);
    }

}
}

The easiest solution will be to make a Global class and define all the variables which you want to share between different activities and classes there as 'static'. 最简单的解决方案是创建一个Global类,并将要在不同活动和类之间共享的所有变量定义为“静态”。 Then simply use Global.varName 然后只需使用Global.varName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM