简体   繁体   English

如何减少此代码重复

[英]How do I reduce this code duplication

I'm stuck on how I can reduce this code duplication, I'm using the TextToSpeech engine and using locales so the user can select their language. 我一直在努力减少代码重复,我使用的是TextToSpeech引擎并使用语言环境,以便用户可以选择他们的语言。

language is a Spinner. language是微调器。

language.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> parent, View arg1,
        int pos, long id) {
    System.out.println(parent.getItemAtPosition(pos).toString());
    if (parent.getItemAtPosition(pos).toString().equals("UK")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.UK);
                }
            }
            });
    } else if (parent.getItemAtPosition(pos).toString()
        .equals("US")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

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

    } else if (parent.getItemAtPosition(pos).toString()
        .equals("French")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(Locale.FRANCE);
                }
            }
            });

    } else if (parent.getItemAtPosition(pos).toString()
        .equals("Italian")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech
                    .setLanguage(Locale.ITALIAN);
                }
            }
            });

    } else if (parent.getItemAtPosition(pos).toString()
        .equals("German")) {
        textToSpeech = new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech
                    .setLanguage(Locale.GERMAN);
                }
            }
            });

    }

    }

    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
});
}

Extract creation of the TextToSpeech object into a separate function: 将TextToSpeech对象的创建提取到单独的函数中:

private TextToSpeech createTextToSpeech(final Locale loc) {
    return new TextToSpeech(MainActivity.this,  
        new TextToSpeech.OnInitListener() {  

        @Override  
        public void onInit(int status) {  
            if (status != TextToSpeech.ERROR) {  
                setLanguage(loc);  
            }  
        }  
    });  
}

Note that the argument loc must be declared final so that it can be used inside the anonymous class. 请注意,自变量loc必须声明为final以便可以在匿名类中使用。

Usage: 用法:

...
} else if (parent.getItemAtPosition(pos).toString().equals("French")) {   
    textToSpeech = createTextToSpeech(Locale.FRANCE);
} ...

You can create a Map. 您可以创建一个地图。

private static final Map<String, Locale> LOCALES = new LinkedHashMap<String, Locale>() {{
   put("US", Locale.US);
   // many more
}


final Locale locale = LOCALES.get(parent.getItemAtPosition(pos).toString());
if(locale != null)
    textToSpeech = new TextToSpeech(MainActivity.this,
        new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) 
               textToSpeech.setLanguage(locale);
        }
    });

from top of my head, make a Map<String,Locale> where key will be name of country and value will be locale 从我的头顶上,制作一个Map<String,Locale> ,其中键将是国家的名称,值将是语言环境
then just do 然后做

textToSpeech = new TextToSpeech(MainActivity.this,
    new TextToSpeech.OnInitListener() {

    @Override
    public void onInit(int status) {
        if (status != TextToSpeech.ERROR) {
            textToSpeech
              .setLanguage(localeMap.get(parent.getItemAtPosition(pos).toString()));
        }
    }
});
public class TextToSpeechFactory {

private static final Map<String, Locale> LOCALES = new HashMap<String, Locale>() {{
       put("US", Locale.US);
       // many more
    }
};

public static TextToSpeech createInstance(String language){
    Locale l = LOCALES.get(language);
    if(l == null)
        throw new Exception("Languange "+ language + "is not valid!");
    else{
        return new TextToSpeech(MainActivity.this,
            new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                textToSpeech.setLanguage(l);
                }
            }
            });
    }
}

} }

Try to use a enum as 尝试将枚举用作

public enum LANGLIST {
   UK("uk", Locale.UK),
   UK("swe", Locale.SWEIDHS);

   public String lang;
   public Locale loc;
   private LANGLIST(String lang, Locale loc) {
      this.lang = lang;
      this.loc = loc;
   }
}

And then loop through all elements in the enum. 然后遍历枚举中的所有元素。

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

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