|
|
|
|
@ -169,19 +169,16 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
this.setContentView(R.layout.note_edit);
|
|
|
|
|
|
|
|
|
|
mContentEditText = findViewById(R.id.note_edit_view);
|
|
|
|
|
Button translateButton = findViewById(R.id.btn_translate);
|
|
|
|
|
|
|
|
|
|
mTranslationService = new TranslationService(this);
|
|
|
|
|
|
|
|
|
|
if (savedInstanceState == null && !initActivityState(getIntent())) {
|
|
|
|
|
finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
initResources();
|
|
|
|
|
// 初始化编辑文本框
|
|
|
|
|
mContentEditText = findViewById(R.id.note_edit_view);
|
|
|
|
|
// 初始化翻译按钮
|
|
|
|
|
Button translateButton = findViewById(R.id.btn_translate);
|
|
|
|
|
// 创建翻译服务实例,这个服务是与翻译 API(TranslationService类) 进行交互。
|
|
|
|
|
mTranslationService = new TranslationService(this);
|
|
|
|
|
// 设置翻译按钮点击事件
|
|
|
|
|
translateButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
@ -192,9 +189,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
}
|
|
|
|
|
// 显示翻译对话框
|
|
|
|
|
private void showTranslationDialog() {
|
|
|
|
|
//获取编辑框中的文本并去除首尾空格
|
|
|
|
|
String text = mContentEditText.getText().toString().trim();
|
|
|
|
|
if (text.isEmpty()) {// 如果文本为空,则提示用户并返回
|
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
|
Toast.makeText(this, "请输入需要翻译的内容", Toast.LENGTH_SHORT).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@ -202,16 +198,14 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
// 创建语言选择对话框
|
|
|
|
|
final String[] languages = {"中文", "英语", "日语", "韩语", "法语", "西班牙语"};
|
|
|
|
|
final String[] languageCodes = {"zh", "en", "jp", "kor", "fra", "spa"};
|
|
|
|
|
final int[] selectedFromIndex = {0}; // 默认中文
|
|
|
|
|
final int[] selectedToIndex = {1}; // 默认英语
|
|
|
|
|
final int[] selectedFromIndex = {1}; // 默认中文
|
|
|
|
|
final int[] selectedToIndex = {0}; // 默认英语
|
|
|
|
|
|
|
|
|
|
// 创建一个AlertDiaLog构建器
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
|
builder.setTitle("选择翻译语言");
|
|
|
|
|
|
|
|
|
|
// 创建语言选择视图
|
|
|
|
|
View dialogView = getLayoutInflater().inflate(R.layout.dialog_translate, null);
|
|
|
|
|
// 从布局中获得原语言和翻译后的目标语言的下拉选择框
|
|
|
|
|
final androidx.appcompat.widget.AppCompatSpinner fromSpinner = dialogView.findViewById(R.id.spinner_from);
|
|
|
|
|
final androidx.appcompat.widget.AppCompatSpinner toSpinner = dialogView.findViewById(R.id.spinner_to);
|
|
|
|
|
|
|
|
|
|
@ -219,31 +213,25 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
|
|
|
|
|
android.R.layout.simple_spinner_item, languages);
|
|
|
|
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
|
// 给两个下拉框用上适配器
|
|
|
|
|
fromSpinner.setAdapter(adapter);
|
|
|
|
|
toSpinner.setAdapter(adapter);
|
|
|
|
|
|
|
|
|
|
// 设置默认选择
|
|
|
|
|
// 原语言选择
|
|
|
|
|
fromSpinner.setSelection(selectedFromIndex[0]);
|
|
|
|
|
// 目标语言选择
|
|
|
|
|
toSpinner.setSelection(selectedToIndex[0]);
|
|
|
|
|
|
|
|
|
|
// 把自定义的试图设置到对话框构建器中
|
|
|
|
|
builder.setView(dialogView);
|
|
|
|
|
|
|
|
|
|
// 设置确定按钮
|
|
|
|
|
builder.setPositiveButton("翻译", (dialog, which) -> {
|
|
|
|
|
// 获取选择的原语言和目标语言的索引
|
|
|
|
|
selectedFromIndex[0] = fromSpinner.getSelectedItemPosition();
|
|
|
|
|
selectedToIndex[0] = toSpinner.getSelectedItemPosition();
|
|
|
|
|
// 如果相同,提示用户并返回
|
|
|
|
|
|
|
|
|
|
if (selectedFromIndex[0] == selectedToIndex[0]) {
|
|
|
|
|
Toast.makeText(NoteEditActivity.this, "源语言和目标语言不能相同", Toast.LENGTH_SHORT).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据索引获取对应的语种
|
|
|
|
|
String from = languageCodes[selectedFromIndex[0]];
|
|
|
|
|
String to = languageCodes[selectedToIndex[0]];
|
|
|
|
|
|
|
|
|
|
@ -253,22 +241,18 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
.setCancelable(false)
|
|
|
|
|
.show();
|
|
|
|
|
|
|
|
|
|
// 执行翻译, 调用翻译服务API
|
|
|
|
|
// 执行翻译
|
|
|
|
|
mTranslationService.translate(text, from, to, new TranslationService.TranslationCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
// 回调成功的结果
|
|
|
|
|
public void onSuccess(String translatedText) {
|
|
|
|
|
loadingDialog.dismiss();
|
|
|
|
|
// 显示翻译结果
|
|
|
|
|
showTranslationResult(text, translatedText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
// 回调时被的结果
|
|
|
|
|
public void onFailure(String errorMessage) {
|
|
|
|
|
loadingDialog.dismiss();
|
|
|
|
|
Log.e(TAG, "翻译失败: " + errorMessage); // 加日志
|
|
|
|
|
// 显示错误的信息
|
|
|
|
|
Toast.makeText(NoteEditActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|