我正在使用片段(因为我有标签)为 android 创建一个翻译器应用程序。
我找到了一些很好的使用微软 API 翻译的教程,但没有一个有效,直到我找到了这个.
作为一项活动,当我将其更改为片段时,它不会。
这是一个小改动后的代码:
public class HomeFragment extends Fragment
{
TextView text;
String translatedText;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
text=(TextView)rootView.findViewById(R.id.translatedtext);
text.setText("<This text should change after translation has occurred in AsyncTask>");
new MyAsyncTask() {
protected void onPostExecute(Boolean result) {
text.setText(translatedText);
}
}.execute();
return rootView;
}
class MyAsyncTask extends AsyncTask<Void, Integer, Boolean>
{
@Override
protected Boolean doInBackground(Void... arg0) {
Translate.setClientId("MicrosoftTranslatorJavaAPI");
Translate.setClientSecret("0VHbhXQnJrZ7OwVqcoX/PDZlyLJS9co3cVev1TPr8iM=");
try {
translatedText = Translate.execute("I should probably set this to something a little less profane", Language.ENGLISH, Language.FRENCH);
} catch(Exception e) {
translatedText = e.toString();
}
return true;
}
}
}
XML 当前只是一个简单的 TextView(如 Github 示例)。
我必须改变什么才能使 asynctask 工作?