1. 程式人生 > >教你在APP中嵌入翻譯功能,不借助第三方軟體

教你在APP中嵌入翻譯功能,不借助第三方軟體

對於翻譯軟體大家都應該使用過,有沒有想到將翻譯功能直接嵌入到自己的APP中,比如聊天介面,翻譯幾句話的功能。正好專案由此需求,看了看有道對外提供的介面,原來很簡單。

一、效果圖

有道翻譯.gif 
說明:由於使用的是模擬器演示,沒有設定輸入中文,就只能看到翻譯英文。需要說明的是,我沒有設定搜尋按鈕,就通過設定鍵盤的回車鍵來搜尋了。

有道翻譯手機截圖.png

說明:這張是手機真機截圖,為了看翻譯中文的效果。

二、需要在有道上面做的事情

2,填寫資訊

有道翻譯資訊填寫.jpg

3,下圖是我填的樣例

有道翻譯API.jpg

說明:這裡的key和網址我塗掉了,就這樣通過就可以,並不需要在專案中配置什麼資訊。

三、程式碼

1,佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin">
<EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"
android:background="@null" android:hint="請輸入要查詢的內容" android:imeOptions="actionSearch" android:lines="1" android:singleLine="true" />
<TextView android:id="@+id/tv_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="時刻準備給您顯示" /> </RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

2,首頁Java程式碼

package com.example.mjj.useyoudaodemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mjj.baseapp.http.OkHttpUtils;
import com.mjj.baseapp.http.callback.StringCallback;
import com.mjj.baseapp.json.GsonUtil;

import okhttp3.Call;

/**
 * Description:嵌入有道翻譯API
 * <p>
 * Created by Mjj on 2016/12/19.
 */

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.et_input);
        textView = (TextView) findViewById(R.id.tv_main);

        // 利用鍵盤的按鈕搜尋
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隱藏鍵盤
                    ((InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    httpData();
                    return true;
                }
                return false;
            }
        });
    }

    private void httpData() {
        OkHttpUtils.get()
                .url("http://fanyi.youdao.com/openapi.do?")
                .addParams("keyfrom", "UseYouDaoDemo")
                .addParams("key", "829332419")
                .addParams("type", "data")
                .addParams("doctype", "json")
                .addParams("version", "1.1")
                .addParams("q", editText.getText().toString().trim())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(MainActivity.this, "請檢查網路", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        GsonUtil gsonutil = new GsonUtil();
                        TranslateBean bean = gsonutil.getJsonObject(response, TranslateBean.class);
                        if (null != bean) {
                            int errorCode = bean.getErrorCode();
                            if (errorCode == 20) {
                                Toast.makeText(MainActivity.this, "要翻譯的文字過長", Toast.LENGTH_SHORT).show();
                            } else if (errorCode == 40) {
                                Toast.makeText(MainActivity.this, "不支援該語言", Toast.LENGTH_SHORT).show();
                            } else if (errorCode == 0) {
                                textView.setText("");
                                textView.setText(bean.getTranslation().get(0));
                            }
                        }
                    }
                });
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

說明:這裡對鍵盤的回車鍵做了搜尋功能,網路請求使用OKhttp,資料解析Gson。

3、返回值

有道翻譯返回資訊說明.jpg

說明:其中doctype是指定你希望返回的資料格式;type為固定值;errorCode返回0表示正常請求。