如何使用微软翻译API为Android? [英] How to use Microsoft Translation API for android?

查看:221
本文介绍了如何使用微软翻译API为Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我的问题是如何使用微软翻译API为Android?我的问题是我创建的Andr​​oid应用程序,将采取从一个输入的EditText,当一个叫翻译按钮是pressed将文本翻译成另一种语言,翻译后的文本,然后设置到另一个的EditText ..我已经写了一些code,你能告诉我有什么不好code,因为它不工作,它没有采取输入,因此不会产生输出...谢谢...

 包kalex.globaltranslate;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;进口com.memetix.mst.language.Language;
进口com.memetix.mst.translate.Translate;公共类TranslateActivity扩展活动实现OnClickListener {    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        Translate.setClientId(我的身份证);
        Translate.setClientSecret(我的秘密钥匙);
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_translate);
        按钮跨=(按钮)findViewById(R.id.translate);
        Trans.setOnClickListener(本);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.activity_translate,菜单);
        返回true;
    }    公共无效的onClick(视图v){
        //获取输入的文本
        输入的EditText =(EditText上)findViewById(R.id.input);
        输出的EditText =(EditText上)findViewById(R.id.output);                字符串= Input.getText()的toString()。
                //串出;
                尝试{
                    串出= Translate.execute(在,Language.AUTO_DETECT,Language.FRENCH);                Input.setText(输出);
                Output.setText(输出);
                }赶上(例外五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }}
}


解决方案

首先,你可能需要阅读更多的关于Java编程一般。

您的对象实例应该以小写字母来命名。例如的EditText输入输入的EditText

我建议你重新组织你的程序,通过移动

 的EditText输入=(EditText上)findViewById(R.id.input);
输出的EditText =(EditText上)findViewById(R.id.output);

在开始的时候,跨实例化之后。然后,让这些对象的全局变量,这样你就可以在OnClick事件访问它们。

您的onClick方法不检查是通过视图的ID,你可能搬起石头砸自己这样的脚下。这是常见的,使案件switch语句对于具有监听所有视图。
或者 - 你也可以有专门ClickListeners所有元素(效率较低,但仍系统):

 私人OnClickListener translateClick =新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                    // 去做
            }
        };

和它设置为您翻译按钮 - trans.setOnClickListener(translateClick);

Ok, my question is how to use Microsoft translation API for android? My problem is that I am creating an android app that will take input from one EditText, and when a button called Translate is pressed it will translate that text into another language, the translated text is then set to the another EditText... I have written some code, can you tell me what wrong with that code because its not working at all, it is not taking input and hence not producing output... thanks...

package kalex.globaltranslate;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class TranslateActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Translate.setClientId("MY ID");
        Translate.setClientSecret("MY SECRET KEY");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translate);
        Button Trans = (Button)findViewById(R.id.translate);
        Trans.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_translate, menu);
        return true;
    }

    public void onClick(View v) {   
        //get the text entered
        EditText Input = (EditText)findViewById(R.id.input);
        EditText Output = (EditText)findViewById(R.id.output);



                String In =Input.getText().toString();
                //String Out;
                try {
                    String Out = Translate.execute(In, Language.AUTO_DETECT, Language.FRENCH);

                Input.setText(Out);
                Output.setText(Out);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



}
}

解决方案

Firstly, you may want to read a bit more about Java programming in general.

Your object instances should be named with small letters. For example EditText Input should be EditText input.

I recommend that you reorganize your program, by moving

EditText Input = (EditText)findViewById(R.id.input);
EditText Output = (EditText)findViewById(R.id.output);

in the beginning, after the "Trans" instantiation. Then make global variable of those objects, so you can access them in the OnClick event.

Your onClick method doesn't check the id of the view which is passed and you may shoot yourself in the foot like this. It's common to make a switch statement with cases for all views which have listeners. Alternatively - you can also have dedicated ClickListeners for all elements (less efficient, but still systematic):

private OnClickListener translateClick = new OnClickListener() {

            @Override
            public void onClick(View v) {
                    // TODO
            }
        };

and set it to your translate button - trans.setOnClickListener(translateClick);

这篇关于如何使用微软翻译API为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆