gpt4 book ai didi

java - 向 Spanned String 添加额外的文本会去掉我的粗体文本。

转载 作者:行者123 更新时间:2023-11-27 23:54:30 25 4
gpt4 key购买 nike

我接受用户输入并将该文本作为粗体文本添加到警告对话框的编辑文本框中。问题是,当用户第二次、第三次或第四次(等等)添加更多文本时,之前的粗体文本消失,只显示最新的粗体文本。感谢您的帮助,谢谢。

这是我的代码:

newField.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
storyText = story.getText().toString();


AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Title");

// Set up the input
final EditText input = new EditText(getActivity());
input.setHint("Ex: Noun, Verb, Color, etc");
// Specify the type of input expected; this, for example, sets
// the input as a password, and will mask the text
builder.setView(input);

// Set up the buttons
builder.setPositiveButton("Add Field",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
int y;
for(int i = 0; i < 200; i++)
{
field[i] = "";
}
for(y = 0; y < 200; y++)
{
if(field[y].equals(""))
{
field[y] = input.getText().toString();
break;
}
}


storyText = storyText + " " + "<b>" + field[y] + "</b>" + " ";
storyText.replaceAll("\n", "<br />");
spanned = Html.fromHtml(storyText);
story.setText(spanned);

Toast.makeText(
getActivity().getApplicationContext(),
"Field Added", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});

builder.show();


}
});

最佳答案

您从 TextView 获取的是字符串而不是 SpannableString,因此您丢失了格式设置。

目前你有类似的东西:

String storyText = story.getText().toString();
// ...
storyText = storyText + " " + "<b>" + field[y] + "</b>" + " ";
storyText.replaceAll("\n", "<br />");
spanned = Html.fromHtml(storyText);
story.setText(spanned);

但是你可能想尝试这样的事情:

SpannableString spannedStoryText = new SpannableString(story.getText());
// ...
String additionalStoryText = " " + "<b>" + field[y] + "</b>" + " ";
additionalStoryText.replaceAll("\n", "<br />"); // assuming you only want this for the appended text.
spannedStoryText = new SpannableString(TextUtils.concat(spannedStoryText, Html.fromHtml(additonalStoryText)));
story.setText(spannedStoryText);

关于java - 向 Spanned String 添加额外的文本会去掉我的粗体文本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24854837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com