片段中的Spannable字符串

时间:2016-01-21 18:03:11

标签: java android regex android-fragments spannablestring

我使用SpannableString为与现有字符串匹配的单词着色。

第一次调用片段时,这种方法非常有效,可以找到,匹配和着色的单词。但第二次调用片段时,检查匹配单词的匹配和颜色的方法会触发,从日志中我可以看到单词被找到并匹配但没有着色。

我猜测它与片段的生命周期有关,而SpannableString正在初始化。

我尝试初始化SpannableStringonResume();

中的setUserVisibleHint(boolean isVisibleToUser);

这是我的方法

public class LessonOne extends ActualFragmetHolder  {

@Override
public void init(Bundle savedInstanceState) {
    addSlide(FragmentPronounce.newInstance("The cat is jumping over the     mouse"));

    addSlide(FragmentPronounce.newInstance("This house is really big"));
.....

FragmentPronounce.java

public class FragmentPronounce extends Fragment implements View.OnClickListener,RecognitionListener,TextToSpeech.OnInitListener {
  private static final String ARG_OPTION_ONE = "opt1";
  private SpannableString hashText;
  ....


public static FragmentPronounce newInstance(String option1) {

  FragmentPronounce sampleSlide = new FragmentPronounce();

  Bundle args = new Bundle();
  args.putString(ARG_OPTION_ONE, option1);
  sampleSlide.setArguments(args);

  return sampleSlide;
 }


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ....

 if (getArguments() != null && getArguments().size() != 0) {
  option1 = getArguments().getString(ARG_OPCTION_ONE);

 }
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pronounce_material, container, false);

final TextView the_question = (TextView) v.findViewById(R.id.text_user_must_say);
if (theQuestion != 0) {
  the_question.setText(theQuestion);
}
 //mic button
 fabMic.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    ///starts listening for speech and does its thing
    ///Im using my own custom speech recognition which works fine
    speech.startListening(recognizerIntent);
    ////check for OnResults()
 }
}

@Override
public void onResults(Bundle results) {
//if its a OnResults is fine then
 methodToCheckAnswer(results);
 ....
}

private void methodToCheckAnswer(Bundle results) {
 //checks answer and calls method which counts and colors words in string
 countAndColorWords();
}

//This is the method which check and colors, it triggers fine every time  fragment is called, but it only colors the word the first time is called in the fragment's life cycle
private void countAndColorWords(){
 String strRight = rightanswer;
    .....
    the_question = (TextView) getActivity().findViewById(R.id.text_user_says);
    the_question.setText(theQuestion);
    System.out.println("The question's TEXT" + String.valueOf(the_question.getText().toString()) + "Tokens counted" + tokensCounted + "Option1" + option1 + "What was spoken " + userAnswered);
    hashText = SpannableString.valueOf(the_question.getText().toString());
    System.out.println("hashText Value "+hashText+  "Right Answer "+ rightanswer);
    Matcher matcher = Pattern.compile("\\S+").matcher(strRight);
    Matcher usermatcher = Pattern.compile("\\S+").matcher(userAnswered);
    int groupCounted = 0;
    groupCounted += matcher.groupCount()+1;
    Log.d(TAG, "The position number of groups:"+groupCounted +" "+userAnswered);
    int rightAnswerCount = matcher.groupCount();
    int userAnswerCount = usermatcher.groupCount();
    String rightAnswer;
    String userAnswer;
    int contadorGroup = 0;;
    int matchCounter =0;
    contadorGroup += matcher.groupCount() + 1;

    while (matcher.find() && usermatcher.find()) {
      System.out.println(matcher.start() + ":" + matcher.group());

      rightAnswer = matcher.group();
      userAnswer = usermatcher.group();
      rightAnswerCount += matcher.groupCount();
      userAnswerCount += usermatcher.groupCount();
      String check = "";
      for (int i = 0; i <= rightAnswerCount && i <= userAnswerCount; i++) {

        if (matcher.group(i).equalsIgnoreCase(usermatcher.group(i))) {//matcher.group(i) == usermatcher.group(i)
          matchCounter++;
          check = matcher.group(i);

          hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#64DD17")), matcher.start(), matcher.end(), i);
        }

      }

    }
    //hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#64DD17")), matcher.start(), matcher.end(), palabraColor);
    the_question.setText(hashText);
}

对不起意大利面条代码,尝试在尝试100种不同的东西后尽可能地清理它。

0 个答案:

没有答案