如何设置公共字符串的文本

时间:2015-08-23 12:44:03

标签: java android string settext yandex-api

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Translate.main(null);
      TextView textElement = (TextView) findViewById(R.id.this_is_id_name);
      textElement.setText(message);
      //leave this line to assign a specific text
    }
    // Separate Class calling Translate
    public final class Translate extends YandexTranslatorAPI {
      public final static String message = com.mohsen.transl.Translate.translation";
      //prevent instantiation
      private Translate(){};
      public static void main(String[] args) {
        try {
          setKey(ApiKeys.YANDEX_API_KEY);
          String translation = Translate.execute("The quick brown fox jumps over the lazy dog.", Language.ENGLISH, Language.SPANISH);
          System.out.println("Translation: " + translation);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

我正在尝试调用translate方法并将字符串转换返回值存储到公共字符串消息中,并使用MainActivity Class中的main oncreate进行显示。

我认为这是最简单的形式。请帮忙。 enter image description here

1 个答案:

答案 0 :(得分:2)

class Translate extends YandexTranslatorAPI {
    public static String message;
    //prevent instantiation
    private Translate() {
    }
    public static void main(String[] args) {
        try {
            setKey(ApiKeys.YANDEX_API_KEY);
            String translation = Translate.execute("The quick brown fox jumps over the lazy dog.", Language.ENGLISH, Language.SPANISH);
            message = translation;
            System.out.println("Translation: " + translation);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

和你的onCreate()方法

textElement.setText(Translate.message);