我用2种语言制作了动物清单以及如何发音。 但是,当我点击语音按钮时,它只会说出数组列表中的第一个单词(即“天鹅”),如何让它说出下一个单词?我迷失了,也不熟悉Android Studio :(
我应该如何完成“位置”方法?
public class ListHewan extends AppCompatActivity {
TextToSpeech textToSpeech;
ImageButton speakButton;
TextView speakText;
int[] images = {R.drawable.angsa,
R.drawable.anjing};
String[] indo = {"Indonesia: Angsa",
"Indonesia: Anjing"};
String[] english = {"Swan",
"Dog"
};
ListView mListView;
public int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_hewan);
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
textToSpeech.setLanguage(Locale.ENGLISH);
} else {
speakButton.setEnabled(true);
speak();
}
}
});
mListView = (ListView) findViewById(R.id.List_Hewan);
speakText = (TextView) findViewById(R.id.englishView);
CustomAdaptor customAdaptor = new CustomAdaptor();
mListView.setAdapter(customAdaptor);
}
private void speak() {
String text = english[position].toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
else
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
class CustomAdaptor extends BaseAdapter {
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.customlayout, null);
ImageView mImageView = (ImageView) view.findViewById(R.id.hewanView);
TextView mTextView = (TextView) view.findViewById(R.id.indoView);
TextView nTextView = (TextView) view.findViewById(R.id.englishView);
ImageButton mButton = (ImageButton) view.findViewById(R.id.Spkbutton);
mImageView.setImageResource(images[position]);
mTextView.setText(indo[position]);
nTextView.setText(english[position]);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
});
return view;
}
}
@Override
protected void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
答案 0 :(得分:0)
你可以这样做 -
ArrayList<String> english = new ArrayList<String>();
english.add("Swan");
english.add("Dog");
tts.speak(english, TextToSpeech.QUEUE_FLUSH, null);
它会说出ArrayList english中的所有单词。
答案 1 :(得分:0)
显然我只需要添加这个
private void speak(int pos) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
textToSpeech.speak(english[pos],TextToSpeech.QUEUE_FLUSH, null,null);
else
textToSpeech.speak(english[pos],TextToSpeech.QUEUE_FLUSH, null);
}
感谢那些试图提供帮助的人!