教你在APP中嵌入翻译功能,不借助第三方软件

对于翻译软件大家都应该使用过,有没有想到将翻译功能直接嵌入到自己的APP中,比如聊天界面,翻译几句话的功能。正好项目由此需求,看了看有道对外提供的接口,原来很简单。

一、效果图

有道翻译.gif 
说明:由于使用的是模拟器演示,没有设置输入中文,就只能看到翻译英文。需要说明的是,我没有设置搜索按钮,就通过设置键盘的回车键来搜索了。

有道翻译手机截图.png

说明:这张是手机真机截图,为了看翻译中文的效果。

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

1,打开网址:有道注册key网址

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表示正常请求。

autojs打包成apk的插件 Auto.js使用JavaScript作为脚本语言,目前使用Rhino 1.7.7.2作为脚本引擎,支持ES5与部分ES6特性。 因为Auto.js是基于JavaScript的,学习Auto.js的API之前建议先学习JavaScript的基本语法和内置对象,可以使用程前面的两个JavaScript程链接来学习。 如果您想要使用TypeScript来开发,目前已经有开发者公布了一个可以把使用TypeScript进行Auto.js开发的工具,参见Auto.js DevTools。 如果想要在电脑而是手机上开发Auto.js,可以使用VS Code以及相应的Auto.js插件使得在 电脑上编辑的脚本能推送到手机运行,参见Auto.js-VSCode-Extension。 本文档的章节大致上是以模块来分的,总体上可以分成"自动操作"类模块(控件操作、触摸模拟、按键模拟等)和其他类模块(设备、应用、界面等)。 "自动操作"的部分又可以大致分为基于控件和基于坐标的操作。基于坐标的操作是传统按键精灵、触摸精灵等脚本软件采用的方式,通过屏幕坐标来点击、长按指定位置模拟操作,从而到达目的。例如click(100, 200), press(100, 200, 500)等。这种方式在游戏类脚本中比较有可行性,结合找图找色、坐标放缩功能也能达到较好的兼容性。但是,这种方式对一般软件脚本却难以达到想要的效果,而且这种方式需要安卓7.0版本以上或者root权限才能执行。所以对于一般软件脚本(例如批量添加联系人、自动提取短信验证码等等),我们采用基于控件的模拟操作方式,结合通知事情、按键事情等达成更好的工作流。这些部分的文档参见基于控件的操作和基于坐标的操作。 其他部分主要包括: app: 应用。启动应用,卸载应用,使用应用查看、编辑文件、访问网页,发送应用间广播等。 console: 控制台。记录运行的日志、错误、信息等。 device: 设备。获取设备屏幕宽高、系统版本等信息,控制设备音量、亮度等。 engines: 脚本引擎。用于启动其他脚本。 events: 事件与监听。按键监听,通知监听,触摸监听等。 floaty: 悬浮窗。用于显示自定义的悬浮窗。 files: 文件系统。文件创建、获取信息、读写。 http: HTTP。发送HTTP请求,例如GET, POST等。 images, colors: 图片和图色处理。截图,剪切图片,找图找色,读取保存图片等。 keys: 按键模拟。比如音量键、Home键模拟等。 shell: Shell命令。 threads: 多线程支持。 ui: UI界面。用于显示自定义的UI界面,和用户交互。 除此之外,Auto.js内置了对Promise。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值