如何逐字为文本添加下划线?



如何在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()返回 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;
}
}

相关内容

  • 没有找到相关文章

最新更新