我怎样才能逐字下划线?

how can i underline a text word by word?

如何在 android studio 中逐字下划线?所以我的程序将是这样的,如果我单击一个按钮,下划线将首先开始,当我再次单击该按钮时,它将再次移动到下一个单词。

例如:

_The_ *** *** ***** **** *** **** ***.

所以如果我想单击按钮,下划线将移动到它旁边的单词或 3 个星号。

The _***_ *** ***** **** *** **** ***.

然后再次单击下一个星号。

这是我为此使用的代码:

public class MainActivity extends AppCompatActivity {

Button btn;
TextView txt;
int counter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = findViewById(R.id.button);
    txt = findViewById(R.id.display);

    txt.setText("the red fox jumps over the lazy dog.");
    final String [] compare1 = txt.getText().toString().split("\s");
    final String arr[] = txt.getText().toString().split(" ",2);
    final String fword = arr[0];
    String rword = arr[1];
    final String reprword = rword.replaceAll("[a-z]", "*");
    txt.setText(fword + " " + reprword);
    final String [] display = txt.getText().toString().split("\s");
    /*final ArrayList<String> getters = new ArrayList<String>(Arrays.asList(display));*/

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(counter <= display.length) {
                if(compare1[counter].length() == display[counter].length())
                {
                    txt.setText(formatText(fword + " " + reprword,display[counter]));
                }

                counter++;
            }
            else
            {


            }
        }
    });
}
public CharSequence formatText(String base, String highlight) {
    int start = base.indexOf(highlight);
    if (start >= 0) {
        SpannableString span = new SpannableString(base);
        span.setSpan(new UnderlineSpan(), start, start + highlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    }
    return base;
}
}

我改下半句来测试一下。

base.indexOf(highlight) ==> indexOf() returns base 中第一次出现的高亮,这就是为什么下划线跨度跳到第一个 "the" 而不是下一次出现的原因。您可以使用 indexOf(String str, int fromIndex)。 以下代码跟踪此变量 "nextStartIndex" 中的 "fromIndex",并在完成句子后将下划线重置为第一个单词。

public class UnderlineWordsActivity extends AppCompatActivity {

    private Button btn;
    private TextView txt;
    private int counter=0;
    private int nextStartIndex=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = findViewById(R.id.button);
        txt = findViewById(R.id.display);
        txt.setText("the red fox jumps over the lazy dog.");

        final String[] arr = txt.getText().toString().split(" ", 2);
        final String firstWord = arr[0];
        String remainingWords = arr[1];
        final String reprword = remainingWords.replaceAll("[a-z]", "*");
        String text = firstWord+" "+reprword;
        txt.setText(text);

        final String[] display = txt.getText().toString().split("\s");


       btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (counter >= display.length) {
                    counter = 0;
                    nextStartIndex = 0;
                }
                txt.setText(formatText(text, display[counter]));
                counter++;
            }
        });
    }

    public CharSequence formatText(String base, String highlight) {
        int start = base.indexOf(highlight,nextStartIndex);
        nextStartIndex = start+highlight.length();
        if (start >= 0) {
            SpannableString span = new SpannableString(base);
            span.setSpan(new UnderlineSpan(), start, nextStartIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return span;
        }
        return base;
    }
}