以下是我的TextActivity
package com.ds.texar;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
public class TextActivity extends AppCompatActivity {
Context context;
TextToSpeech textToSpeech;
private String textFromMain = "";
ToggleButton languageToggleButton;
private String outputHindiString = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//setting text string received from main activity
final TextView textView = (TextView)findViewById(R.id.mtextview);
textFromMain = getIntent().getStringExtra("mytext");
textView.setText(textFromMain);
context = getApplicationContext();
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener(){
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
}
}
});
languageToggleButton = (ToggleButton) findViewById(R.id.language_toggle_button);
languageToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(languageToggleButton.isChecked()){
try {
Translate.setHttpReferrer("http://android-er.blogspot.com/");
outputHindiString = Translate.execute(textFromMain,
Language.ENGLISH, Language.HINDI);
} catch (Exception ex) {
ex.printStackTrace();
outputHindiString = "Error";
}
textView.setText(outputHindiString);
}
else{
textView.setText(textFromMain);
}
}
});
FloatingActionButton copyText = (FloatingActionButton) findViewById(R.id.copy_text);
copyText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("myText", textFromMain);
clipboard.setPrimaryClip(clip);
Toast.makeText(context, "Your text copied.", Toast.LENGTH_SHORT).show();
// toast.setGravity(Gravity.TOP| Gravity.LEFT, 10, 300);
// toast.show();
}
});
final FloatingActionButton textToSpeechButton = (FloatingActionButton) findViewById(R.id.text_to_speech);
textToSpeechButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), textFromMain, Toast.LENGTH_SHORT).show();
textToSpeech.speak(textFromMain, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}
@Override
public void onPause(){
if(textToSpeech !=null){
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onPause();
}
}
这是布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ds.texar.TextActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/include2"
layout="@layout/content_text" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/text_to_speech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_lock_silent_mode_off" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/copy_text"
android:layout_width="51dp"
android:layout_height="55dp"
android:layout_gravity="top|left"
android:layout_margin="16dp"
android:clickable="true"
app:fabSize="mini"
app:layout_anchor="@+id/include2"
app:layout_anchorGravity="bottom|right"
app:srcCompat="?attr/actionModeCopyDrawable" />
<ToggleButton
android:id="@+id/language_toggle_button"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_margin="16dp"
android:background="@drawable/check"
android:textOff=""
android:textOn=""
android:focusable="false"
android:focusableInTouchMode="false"
app:layout_anchor="@+id/include2"
app:layout_anchorGravity="bottom|center_horizontal" />
</android.support.design.widget.CoordinatorLayout>
这是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ds.texar">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />
<uses-feature android:name="android.hardware.sensor.light" />
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TextActivity"
android:label="@string/title_activity_text"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
错误日志:
Information:Gradle tasks [:app:generateDebugSources,
:app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies,
:app:generateDebugAndroidTestSources, :app:compileDebugSources,
:app:compileDebugUnitTestSources, :app:compileDebugAndroidTestSources]
C:\Users\DELL_PC\Desktop\Texar\app\src\main\java\com\ds\texar\TextActivity.java
Error:(16, 32) error: package com.google.api.translate does not exist
Error:(17, 32) error: package com.google.api.translate does not exist
Error:(58, 29) error: cannot find symbol variable Translate Error:(60,
37) error: cannot find symbol variable Language Error:(60, 55) error:
cannot find symbol variable Language Error:(59, 49) error: cannot find
symbol variable Translate Error:Execution failed for task
':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details. Information:BUILD FAILED Information:Total time: 6.836 secs
Information:7 errors Information:0 warnings Information:See complete
output in console
答案 0 :(得分:1)
从http://code.google.com/p/google-api-translate-java/.
将其导入当前项目文件夹。
将jar导入eclipse
右键点击项目&amp;选择属性。
选择Java Build Path。
选择“库”选项卡。
单击“添加外部JAR”按钮。
找到JAR的路径并添加它。