简体   繁体   English

如何逐字在文本下划线?

[英]how can i underline a text word by word?

how can i underline word by word in a sentence in android studio?如何在 android studio 中逐字下划线? so my program would be like this if i click a button the underline will start at first and when i click again the button it will move again to the next word.因此,如果我单击一个按钮,我的程序将是这样的,下划线将首先开始,当我再次单击该按钮时,它将再次移动到下一个单词。

for Example:例如:

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

so if i want to click the button the underline will move next to it word or 3 asterisk.因此,如果我想单击按钮,下划线将移动到它旁边的单词或 3 个星号。

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

and click again to the next asterisk.并再次单击下一个星号。

this is my code using for this:这是我用于此的代码:

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;
}
}

I change the other half the sentence to test it for this.我改变了句子的另一半来测试它。

base.indexOf(highlight) ==> indexOf() returns the first occurrence of highlight in base, thats why the underline span jumps to first "the" instead of the next occurrence. base.indexOf(highlight) ==> indexOf()返回 base 中第一次出现的高亮,这就是为什么下划线跨度跳转到第一个“the”而不是下一个出现的原因。 You can use indexOf(String str, int fromIndex) .您可以使用indexOf(String str, int fromIndex) The following code tracks the "fromIndex" in this variable "nextStartIndex", and also underlining resets to first word after completing the sentence.以下代码跟踪此变量“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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM