首页

android 实时翻译例子

java

2020-7-26

package com.baidu.translate;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.baidu.translate.utils.TranslateUtils;

public class MainActivity extends Activity {

	protected static final int SUCCESS = 0;
	protected static final int ERROR = 1;
	private Spinner sp_src;
	private Spinner sp_des;
	private EditText et_src;
	private EditText et_des;
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				et_des.setText((String) msg.obj);
				break;
			case ERROR:
				Toast.makeText(getApplicationContext(), "翻译出错", 0).show();
				break;
			default:
				break;
			}
		};
	};
	private static Map<String,String> map;
	static{
		 map = new HashMap<String, String>();
		 map.put("自动检测", "auto");
		 map.put("粤语", "yue");
		 map.put("中文","zh");
		 map.put("英语", "en");
	 }
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//得到下拉框
		sp_src = (Spinner) findViewById(R.id.sp_src);
		sp_des = (Spinner) findViewById(R.id.sp_des);
		//
		et_src = (EditText) findViewById(R.id.et_src);
		et_des = (EditText) findViewById(R.id.et_des);
		
	}
	
	public void translate(View v){
		//得到输入的语言,得到输入的文本,得到目标语言
		String language = sp_src.getSelectedItem().toString();
		if(!map.containsKey(language)){
			Toast.makeText(this, "不存在该语言", 0).show();
			return;
		}
		final String srcLanguage =  map.get(language);
		 language = sp_des.getSelectedItem().toString();
		if(!map.containsKey(language)){
			Toast.makeText(this, "不存在该语言", 0).show();
			return;
		}
		final String srcText = et_src.getText().toString().trim();
		final String desLanguage = map.get(language);
		System.out.println("源语言为:" srcLanguage " 目标语言为:" desLanguage  " 翻译的内容为:" srcText);
		//访问网络需要在子线程完成
		new Thread(new Runnable() {
			
			@Override
			public void run() {
//				String desText = TranslateUtils.translateByGet(srcLanguage,desLanguage,srcText);
//				String desText = TranslateUtils.translateByPost(srcLanguage,desLanguage,srcText);
//				String desText = TranslateUtils.translateByHttpGet(srcLanguage,desLanguage,srcText);
				String desText = TranslateUtils.translateByHttpPost(srcLanguage,desLanguage,srcText);
					 
				
				if(desText!=null){
					Message msg = Message.obtain();
					msg.what = SUCCESS;
					msg.obj = desText;
					handler.sendMessage(msg);
				}else{
					Message msg = Message.obtain();
					msg.what = ERROR;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}

	//当使用http-master时,应该使用这个点击事件 
	public void translate1(View v){
		//得到输入的语言,得到输入的文本,得到目标语言
		String language = sp_src.getSelectedItem().toString();
		if(!map.containsKey(language)){
			Toast.makeText(this, "不存在该语言", 0).show();
			return;
		}
		 String srcLanguage =  map.get(language);
		 language = sp_des.getSelectedItem().toString();
		if(!map.containsKey(language)){
			Toast.makeText(this, "不存在该语言", 0).show();
			return;
		}
		 String srcText = et_src.getText().toString().trim();
		 String desLanguage = map.get(language);
		System.out.println("源语言为:" srcLanguage " 目标语言为:" desLanguage  " 翻译的内容为:" srcText);
//		 TranslateUtils.translateByHttpMasterGet(et_des,srcLanguage, desLanguage, srcText);
		 TranslateUtils.translateByHttpMasterPost(et_des,srcLanguage, desLanguage, srcText);
//		System.out.println(desText);
//		et_des.setText(desText);
	}
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
百度翻译/.classpath , 475
百度翻译/.project , 848
百度翻译/.settings/org.eclipse.jdt.core.prefs , 177
百度翻译/AndroidManifest.xml , 946
百度翻译/bin/AndroidManifest.xml , 946
百度翻译/bin/classes/com/baidu/translate/BuildConfig.class , 347
百度翻译/bin/classes/com/baidu/translate/MainActivity$1.class , 1260
百度翻译/bin/classes/com/baidu/translate/MainActivity$2.class , 1508
百度翻译/bin/classes/com/baidu/translate/MainActivity.class , 3949
百度翻译/bin/classes/com/baidu/translate/R$array.class , 398
百度翻译/bin/classes/com/baidu/translate/R$attr.class , 343
百度翻译/bin/classes/com/baidu/translate/R$dimen.class , 464
百度翻译/bin/classes/com/baidu/translate/R$drawable.class , 410
百度翻译/bin/classes/com/baidu/translate/R$id.class , 516
百度翻译/bin/classes/com/baidu/translate/R$layout.class , 406
百度翻译/bin/classes/com/baidu/translate/R$menu.class , 391
百度翻译/bin/classes/com/baidu/translate/R$string.class , 513
百度翻译/bin/classes/com/baidu/translate/R$style.class , 434
百度翻译/bin/classes/com/baidu/translate/R.class , 732
百度翻译/bin/classes/com/baidu/translate/utils/TranslateUtils$1.class , 1969
百度翻译/bin/classes/com/baidu/translate/utils/TranslateUtils$2.class , 1997
百度翻译/bin/classes/com/baidu/translate/utils/TranslateUtils.class , 8745
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpClient$1.class , 1689
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpClient$2.class , 1938
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpClient$3.class , 937
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpClient$InflatingEntity.class , 943
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpClient.class , 21310
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpRequest.class , 4718
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpResponseHandler$ResponderHandler.class , 803
百度翻译/bin/classes/com/loopj/android/http/AsyncHttpResponseHandler.class , 8709
百度翻译/bin/classes/com/loopj/android/http/Base64$Coder.class , 474
百度翻译/bin/classes/com/loopj/android/http/Base64$Decoder.class , 5394
百度翻译/bin/classes/com/loopj/android/http/Base64$Encoder.class , 3952
百度翻译/bin/classes/com/loopj/android/http/Base64.class , 3031
百度翻译/bin/classes/com/loopj/android/http/Base64DataException.class , 401
百度翻译/bin/classes/com/loopj/android/http/Base64OutputStream.class , 2721
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler$1$1.class , 1341
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler$1$2.class , 1358
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler$1.class , 2164
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler$2$1.class , 1451
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler$2$2.class , 1366
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler$2.class , 2274
百度翻译/bin/classes/com/loopj/android/http/BaseJsonHttpResponseHandler.class , 2454
百度翻译/bin/classes/com/loopj/android/http/BinaryHttpResponseHandler.class , 3130
百度翻译/bin/classes/com/loopj/android/http/DataAsyncHttpResponseHandler.class , 3704
百度翻译/bin/classes/com/loopj/android/http/FileAsyncHttpResponseHandler.class , 3565
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler$1$1.class , 1883
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler$1$2.class , 1226
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler$1.class , 1776
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler$2$1.class , 2076
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler$2$2.class , 1226
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler$2.class , 1907
百度翻译/bin/classes/com/loopj/android/http/JsonHttpResponseHandler.class , 3493
百度翻译/bin/classes/com/loopj/android/http/JsonStreamerEntity.class , 8109
百度翻译/bin/classes/com/loopj/android/http/MySSLSocketFactory$1.class , 1175
百度翻译/bin/classes/com/loopj/android/http/MySSLSocketFactory.class , 5979
百度翻译/bin/classes/com/loopj/android/http/PersistentCookieStore.class , 7149
百度翻译/bin/classes/com/loopj/android/http/RequestHandle.class , 1416
百度翻译/bin/classes/com/loopj/android/http/RequestParams$1.class , 633
百度翻译/bin/classes/com/loopj/android/http/RequestParams$FileWrapper.class , 596
百度翻译/bin/classes/com/loopj/android/http/RequestParams$StreamWrapper.class , 685
百度翻译/bin/classes/com/loopj/android/http/RequestParams.class , 12512
百度翻译/bin/classes/com/loopj/android/http/ResponseHandlerInterface.class , 855
百度翻译/bin/classes/com/loopj/android/http/RetryHandler.class , 3037
百度翻译/bin/classes/com/loopj/android/http/SerializableCookie.class , 2370
百度翻译/bin/classes/com/loopj/android/http/SimpleMultipartEntity$FilePart.class , 2879
百度翻译/bin/classes/com/loopj/android/http/SimpleMultipartEntity.class , 7716
百度翻译/bin/classes/com/loopj/android/http/SyncHttpClient.class , 2285
百度翻译/bin/classes/com/loopj/android/http/TextHttpResponseHandler.class , 1820
百度翻译/bin/classes.dex , 800116
百度翻译/bin/dexedLibs/android-support-v4-48d3db98a16058cb74bd413b7a783306.jar , 234431
百度翻译/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
百度翻译/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
百度翻译/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
百度翻译/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
百度翻译/bin/resources.ap_ , 41455
百度翻译/bin/百度翻译.apk , 316978
百度翻译/gen/com/baidu/translate/BuildConfig.java , 161
百度翻译/gen/com/baidu/translate/R.java , 2862
百度翻译/ic_launcher-web.png , 51394
百度翻译/libs/android-support-v4.jar , 627582
百度翻译/proguard-project.txt , 781
百度翻译/project.properties , 563
百度翻译/res/drawable-hdpi/ic_launcher.png , 7658
百度翻译/res/drawable-mdpi/ic_launcher.png , 3777
百度翻译/res/drawable-xhdpi/ic_launcher.png , 12516
百度翻译/res/drawable-xxhdpi/ic_launcher.png , 24777
百度翻译/res/layout/activity_main.xml , 1356
百度翻译/res/menu/main.xml , 263
百度翻译/res/values/dimens.xml , 220
百度翻译/res/values/strings.xml , 464
百度翻译/res/values/styles.xml , 697
百度翻译/res/values-sw600dp/dimens.xml , 203
百度翻译/res/values-sw720dp-land/dimens.xml , 277
百度翻译/res/values-v11/styles.xml , 334
百度翻译/res/values-v14/styles.xml , 391
百度翻译/src/com/baidu/translate/MainActivity.java , 3892
百度翻译/src/com/baidu/translate/utils/TranslateUtils.java , 9505
百度翻译/src/com/loopj/android/http/AsyncHttpClient.java , 44338
百度翻译/src/com/loopj/android/http/AsyncHttpRequest.java , 6245
百度翻译/src/com/loopj/android/http/AsyncHttpResponseHandler.java , 14416
百度翻译/src/com/loopj/android/http/Base64.java , 28608
百度翻译/src/com/loopj/android/http/Base64DataException.java , 209
百度翻译/src/com/loopj/android/http/Base64OutputStream.java , 3918
百度翻译/src/com/loopj/android/http/BaseJsonHttpResponseHandler.java , 6030
百度翻译/src/com/loopj/android/http/BinaryHttpResponseHandler.java , 4727
百度翻译/src/com/loopj/android/http/DataAsyncHttpResponseHandler.java , 5601
百度翻译/src/com/loopj/android/http/FileAsyncHttpResponseHandler.java , 4342
百度翻译/src/com/loopj/android/http/JsonHttpResponseHandler.java , 8519
百度翻译/src/com/loopj/android/http/JsonStreamerEntity.java , 10905
百度翻译/src/com/loopj/android/http/MySSLSocketFactory.java , 6161
百度翻译/src/com/loopj/android/http/PersistentCookieStore.java , 7883
百度翻译/src/com/loopj/android/http/RequestHandle.java , 2487
百度翻译/src/com/loopj/android/http/RequestParams.java , 18731
百度翻译/src/com/loopj/android/http/ResponseHandlerInterface.java , 3169
百度翻译/src/com/loopj/android/http/RetryHandler.java , 4120
百度翻译/src/com/loopj/android/http/SerializableCookie.java , 2617
百度翻译/src/com/loopj/android/http/SimpleMultipartEntity.java , 9308
百度翻译/src/com/loopj/android/http/SyncHttpClient.java , 2674
百度翻译/src/com/loopj/android/http/TextHttpResponseHandler.java , 3655
没有账号? 忘记密码?

社交账号快速登录