教你在APP中嵌入翻译功能,不借助第三方软件
来源:互联网 发布:上海汇纳网络信息科技 编辑:程序博客网 时间:2024/05/14 08:23
对于翻译软件大家都应该使用过,有没有想到将翻译功能直接嵌入到自己的APP中,比如聊天界面,翻译几句话的功能。正好项目由此需求,看了看有道对外提供的接口,原来很简单。
一、效果图
说明:由于使用的是模拟器演示,没有设置输入中文,就只能看到翻译英文。需要说明的是,我没有设置搜索按钮,就通过设置键盘的回车键来搜索了。
说明:这张是手机真机截图,为了看翻译中文的效果。
二、需要在有道上面做的事情
1,打开网址:有道注册key网址
2,填写信息
3,下图是我填的样例
说明:这里的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>
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)); } } } }); }}
说明:这里对键盘的回车键做了搜索功能,网络请求使用OKhttp,数据解析Gson。
3、返回值
说明:其中doctype是指定你希望返回的数据格式;type为固定值;errorCode返回0表示正常请求。
code小生寄语:
源码已上传至github,公众号回复 “demo” 查看链接。本篇是code小生的第二篇原创文章,倍感欣喜。时至今日,2016即将过去,距离程序猿放假回家过年的日子也就一个月,所以这段时间里,code小生决定学习一下2016年好的开源项目,汲取其中的精华,推送给各位朋友,一起学习。
1 0
- 教你在APP中嵌入翻译功能,不借助第三方软件
- 教你在APP中嵌入翻译功能,不借助第三方软件
- 在 Sandboxed Mac App 中嵌入第三方可执行文件
- Android app借助第三方软件打开pdf文档
- 在asp中不借助第三方组件抓取别的站点的页面
- 不借助任何第三方软件,使用笔记本(win7)当路由器供其他无线终端上网
- 在不借助第三方变量下实现两个数交换
- php中不借助第三方交换两个变量的值
- 不借助第三方 Win7搞定无法删除的文件
- 不借助第三方工具,查看SQL的执行计划
- 两数字交换 不借助第三方变量
- java操作word模板不借助第三方软件包
- 不借助第三方jar包实现文件上传
- python 不借助第三方包实现xml转json
- 交换两个数的值,不借助第三方变量.
- 交换两个数的值,不借助第三方变量
- 不借助第三方变量交换两个数值
- 怎么在第三方应用中打开自己的APP
- Java IO一些理解和demo
- sql statement:批量插入
- 深入探索透视投影变换
- ios开发-- URL Schemes 使用详解
- vs2010添加lib库的方法
- 教你在APP中嵌入翻译功能,不借助第三方软件
- SlidingPaneLayout的基本使用
- BlockCanary — 轻松找出Android App界面卡顿元凶
- 游戏设计之我见 —— 关卡设计中的固有印象
- 13.8 Swift构造方法的自动继承
- JAVAScript:mobile端,基于transform-origin和tranform(scale),实现表格缩放
- C# Type调用方法和CreateInstance的区别
- 老生常谈-GCD线程机制总结
- Spark简介