Android 如何将短语拆分为单词,并在每个单词中显示相应的图像?
我已经在下面尝试了这些代码,但它只读取单词而不是短语,这些代码有效,不符合我的要求。用户输入的每个单词只会添加并添加到空数组列表中。我想要实现的是,当用户输入一个短语时,它将按空格分割并存储在一个空数组列表中。当条件为真时,该数组列表将与另一个包含单词的数组列表进行比较,它将显示相应的图像,现在将读取短语中的下一个单词Android 如何将短语拆分为单词,并在每个单词中显示相应的图像?,android,android-studio,Android,Android Studio,我已经在下面尝试了这些代码,但它只读取单词而不是短语,这些代码有效,不符合我的要求。用户输入的每个单词只会添加并添加到空数组列表中。我想要实现的是,当用户输入一个短语时,它将按空格分割并存储在一个空数组列表中。当条件为真时,该数组列表将与另一个包含单词的数组列表进行比较,它将显示相应的图像,现在将读取短语中的下一个单词 private List<String> wordsList; private ArrayList<String>wordsToDisplay=new
private List<String> wordsList;
private ArrayList<String>wordsToDisplay=new ArrayList<>();
boolean missingGIF;
int counter;
}
这是我的动画监听器,它用用户输入的所有单词显示相应的图像
@Override
public void onAnimationCompleted(int loopNumber) {
if(wordsToDisplay.size()>1){
if(counter<wordsToDisplay.size()){
try{
counter++;
gifImageView.setImageResource(getResources().getIdentifier(wordsToDisplay.get(counter),"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
catch(IndexOutOfBoundsException e){
Log.d("Expected",e.toString());
}
}
}
}
移除计数器并使用下面的逻辑
@Override
public void onAnimationCompleted(int loopNumber) {
if(!wordsToDisplay.isEmpty()){
String nextWord = wordsToDisplay.remove(0);
gifImageView.setImageResource(getResources().getIdentifier(nextWord,"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
}
@Override
public void onAnimationCompleted(int loopNumber) {
if(wordsToDisplay.size()>1){
if(counter<wordsToDisplay.size()){
try{
counter++;
gifImageView.setImageResource(getResources().getIdentifier(wordsToDisplay.get(counter),"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
catch(IndexOutOfBoundsException e){
Log.d("Expected",e.toString());
}
}
}
}
@Override
public void onAnimationCompleted(int loopNumber) {
if(!wordsToDisplay.isEmpty()){
String nextWord = wordsToDisplay.remove(0);
gifImageView.setImageResource(getResources().getIdentifier(nextWord,"drawable",getPackageName()));
animation = (GifDrawable)gifImageView.getDrawable();
resetAnimation();
startAnimation();
animation.addAnimationListener(this);
}
}