Android 百度翻译API(详细步骤+源码)

本文展示了如何在Android应用中集成百度翻译API,包括布局设计、控件初始化、事件监听、输入框和语言选择监听、翻译功能实现。通过实例代码详细解释了每个步骤,包括设置状态栏颜色、初始化组件、输入监听、翻译点击事件处理以及异步请求翻译内容。
摘要由CSDN通过智能技术生成

<androidx.appcompat.widget.Toolbar

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

android:background=“#FFF”

android:elevation=“3dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:text=“在线翻译”

android:textColor=“#000”

android:textSize=“18sp” />

</androidx.appcompat.widget.Toolbar>

再运行一下:

在这里插入图片描述

嗯,效果喜人,看上去就是辣么滴顺眼。然后我贴上完整的布局代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.andr oid.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:fitsSystemWindows=“true”

android:orientation=“vertical”

tools:context=“.MainActivity”>

<androidx.appcompat.widget.Toolbar

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

android:background=“#FFF”

android:elevation=“3dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:text=“在线翻译”

android:textColor=“#000”

android:textSize=“18sp” />

</androidx.appcompat.widget.Toolbar>

<ScrollView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:overScrollMode=“never”>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

android:padding=“20dp”>

<LinearLayout

android:id=“@+id/before_lay”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:background=“#FFF”

android:foreground=“@drawable/stroke_gray”>

<org.angmarch.views.NiceSpinner

android:id=“@+id/sp_language”

android:layout_width=“match_parent”

android:layout_height=“48dp”

app:arrowTint=“#000”

app:textTint=“#000” />

<LinearLayout

android:id=“@+id/after_lay”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:gravity=“center_vertical”

android:orientation=“horizontal”

android:visibility=“gone”>

<androidx.cardview.widget.CardView

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:background=“#FFF”

app:cardCornerRadius=“5dp”>

<TextView

android:id=“@+id/tv_from”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:background=“@drawable/stroke_black_radus”

android:gravity=“center”

android:textColor=“#000”

android:textSize=“14sp” />

</androidx.cardview.widget.CardView>

<ImageView

android:layout_width=“30dp”

android:layout_height=“30dp”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“10dp”

android:src=“@mipmap/icon_change” />

<androidx.cardview.widget.CardView

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:background=“#FFF”

app:cardCornerRadius=“5dp”>

<TextView

android:id=“@+id/tv_to”

android:layout_width=“match_parent”

android:layout_height=“40dp”

android:background=“@drawable/stroke_black_radus”

android:gravity=“center”

android:textColor=“#000”

android:textSize=“14sp” />

</androidx.cardview.

  • 16
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值