0

如何在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;
}
}

我改变了句子的另一半来测试它。

4

1 回答 1

1

base.indexOf(highlight)==>indexOf()返回 base 中第一次出现的 highlight ,这就是为什么下划线跨度跳转到第一个“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;
    }
}
于 2020-05-24T16:32:12.353 回答