Java 如何在我的 Android 应用程序中使用 Google 翻译 Api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22829657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:10:04  来源:igfitidea点击:

How to use Google translate Api in my android app

javaandroideclipse

提问by user3463989

I am making an android app for language translation in real time...I used recognizer intent to get voice input from user after which it gives me a list of options of what the user spoke.Now I want to translate it another language using google translate api but I don't know how to use it. Code for what I have done till now is.Also if you can tell me how it can be done that instead of giving me options of what I spoke , it selects one on its own and then use google translate api on that....

我正在制作一个用于实时语言翻译的 android 应用程序......我使用识别器意图从用户那里获取语音输入,然后它为我提供了用户所说内容的选项列表。现在我想使用谷歌将它翻译成另一种语言翻译 api 但我不知道如何使用它。到目前为止,我所做的代码是。此外,如果您能告诉我如何做到这一点,而不是给我选择我所说的内容,它会自行选择一个,然后在其上使用 google translate api....

package com.example.testing;

import java.util.ArrayList;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Voice extends Activity implements OnClickListener{

    ListView lv;
    static final int check=1111;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice);
        lv=(ListView)findViewById(R.id.lvVoiceReturn);
        Button b=(Button)findViewById(R.id.bVoice);
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Up");
        startActivityForResult(i, check);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if(requestCode == check && resultCode==RESULT_OK){

            ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }


}

采纳答案by Rachita Nanda

Here is the documentation for https://developers.google.com/translate/

这是https://developers.google.com/translate/的文档

Also, refer to thisdemo project on the same

另外,请参考演示项目

Bing also provides a translation API

Bing 还提供了翻译 API

Check thisGoogle translation API example .

检查Google 翻译 API 示例。

Hope this helps.

希望这可以帮助。