【发布时间】:2015-11-29 16:29:55
【问题描述】:
有一个类似的答案here,但它没有说明我要问什么。
我的 Android 视图中有一个“粗体文本”选项,如果用户选择该选项,则在后面的代码中将布尔值设置为 true。
使用 TextWatcher,如何在 EditText 中的特定点之后将用户键入的文本更改为 bold。如果用户将其关闭,则键入的文本应采用正常样式。一切都取决于布尔值。
这是我目前所拥有的:
Boolean isBolded = false;
// Code that turns the bold option true and false...
contentBox = (EditText) findViewById(R.id.contentBox);
contentBox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(isBolded)
{
//Start bolding the text typed after that point
}
else
{
//Stop styling the text typed after that point
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
困难在于确定用户在键入时决定关闭和打开粗体的 EditText 中的点。你们有什么想法?
【问题讨论】:
-
这是一个文本观察器的基本实现,希望对您有所帮助stackoverflow.com/questions/33645769/… 您将文本加粗的逻辑在 afterTextChanged() 中
标签: java android android-layout android-intent mobile