向Spanned String添加其他文本可以删除我的Bold文本。

时间:2014-07-20 20:55:36

标签: java android html android-edittext spannablestring

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

这是我的代码:

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();


        }
    });

2 个答案:

答案 0 :(得分:2)

您从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);

答案 1 :(得分:0)

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

这些代码行使您在TextView普通文字和新文字上的文字变为粗体,因为您只是在<b>之后使用</b>storyText标记。这就是为什么只有新文本是粗体而旧文本保持规则。

将您的代码更改为:

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