安卓小项目之刀刀狗翻译

2023-10-31 18:10
文章标签 项目 翻译 刀刀 安卓小

本文主要是介绍安卓小项目之刀刀狗翻译,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

学了一阵安卓,想搞个实用的app玩玩。

项目名称:刀刀狗翻译

api源:有道api

界面如下,略简陋



mainactivity

package com.example.zheng.fangyi;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {private EditText input;private TextView result;String rdline=new String();String read=null;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);input = (EditText) findViewById(R.id.input);result = (TextView) findViewById(R.id.result);result.setMovementMethod(ScrollingMovementMethod.getInstance());Button check = (Button) findViewById(R.id.check);try {check.setOnClickListener(new View.OnClickListener() {@Override
                public void onClick(View v) {new AsyncTask<String,Void,Void>(){@Override
                        protected Void doInBackground(String... params) {try {URL url=new URL(params[0]);try {URLConnection conn=url.openConnection();InputStream is=conn.getInputStream();InputStreamReader isr=new InputStreamReader(is,"utf-8");BufferedReader br=new BufferedReader(isr);String line;while((line=br.readLine())!=null){System.out.println(line);rdline+=line;}br.close();isr.close();is.close();} catch (IOException e) {e.printStackTrace();}} catch (MalformedURLException e) {e.printStackTrace();}return null;}}.execute("http://fanyi.youdao.com/openapi.do?keyfrom=zsdlove&key=5668291&type=data&doctype=xml&version=1.1&q="+input.getText());xmlresolve(rdline);//解析xml
                    rdline="";read="";}});}catch (Exception e) {e.printStackTrace();}}public  void xmlresolve(String rdline)//xml解析
    {try {Document document = DocumentHelper.parseText(rdline);org.dom4j.Element root=document.getRootElement();//获取根节点
            //错误捕获==========
            Element errorCode=root.element("errorCode");if(errorCode.getText().equals("20")){Toast.makeText(getApplicationContext(),"要翻译的文本过长",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("30")){Toast.makeText(getApplicationContext(),"无法进行有效的翻译",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("40")){Toast.makeText(getApplicationContext(),"不支持的语言类型",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("50")){Toast.makeText(getApplicationContext(),"无效的key",Toast.LENGTH_SHORT);}else if(errorCode.getText().equals("60")){Toast.makeText(getApplicationContext(),"请输入正确的英文",Toast.LENGTH_SHORT);}//错误捕获finish=================
            else{org.dom4j.Element tnote=root.element("translation");//获取translation节点
            Element pnote=tnote.element("paragraph");//获取paragraph节点
            //===================
            Element basic =root.element("basic");//basic节点
            Element us=basic.element("us-phonetic");//美式音标
            Element uk=basic.element("uk-phonetic");//uk音标
            Element explains=basic.element("explains");//基本释义
            Element ex=explains.element("ex");read="翻译结果:"+"\r\n"+"常规翻译:"+pnote.getText()+"\r\n"+"美式音标:"+us.getText()+"\r\n"+"英式音标:"+uk.getText()+"\r\n"+"名词解释:"+ex.getText();}} catch (DocumentException e) {e.printStackTrace();}result.setText(read);}
}
========================================================================================================================

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="com.example.zheng.fangyi.MainActivity"><Button
        android:text="查询"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check"
        android:layout_below="@+id/input"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="12dp" /><TextView
        android:text="翻译结果:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="67dp"
        android:id="@+id/result"
        android:layout_alignTop="@+id/check"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" /><TextView
        android:text="zsd出品"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="32dp"
        android:id="@+id/textView" /><EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="请输入"
        android:ems="10"
        android:id="@+id/input"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" /></RelativeLayout>

输入hello

查看效果



这里提一下,对于xml返回文件的解析需要用到dom4j.jar包,可以到官网上去下载。引入项目中的libs目录中就可以了,libs目录找不到的原因是目录查看的方式不对


这篇关于安卓小项目之刀刀狗翻译的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/317185

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

MyCat分库分表的项目实践

《MyCat分库分表的项目实践》分库分表解决大数据量和高并发性能瓶颈,MyCat作为中间件支持分片、读写分离与事务处理,本文就来介绍一下MyCat分库分表的实践,感兴趣的可以了解一下... 目录一、为什么要分库分表?二、分库分表的常见方案三、MyCat简介四、MyCat分库分表深度解析1. 架构原理2. 分

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

在.NET项目中嵌入Python代码的实践指南

《在.NET项目中嵌入Python代码的实践指南》在现代开发中,.NET与Python的协作需求日益增长,从机器学习模型集成到科学计算,从脚本自动化到数据分析,然而,传统的解决方案(如HTTPAPI或... 目录一、CSnakes vs python.NET:为何选择 CSnakes?二、环境准备:从 Py

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca