从返回值设置 textview

Set textview from returned value

我正在创建一个水平字符串,每个文本之间都有彩色的项目符号点。我使用 Spanned StringBuilder 和 return 值作为 Html.fromHtml(builder.toString()) 的方法。问题是如何使用 return 值在 TextView 中设置文本。

public class MainActivity extends AppCompatActivity {

    TextView text;
    StringBuilder builder;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);
        text.setText(Html.fromHtml(builder.toString()));

    }

    Spanned createSpannable(List<String> interests) {
        builder = new StringBuilder();
        int size = interests.size();
        for (int i = 0; i < size; i++) {
            builder.append(interests.get(i));
            if (i < size - 1) {
                builder.append(" <font color=\"#c52d08\">&#8226</font> ");
            }
        }

        return Html.fromHtml(builder.toString());
    }

}

试试这个:

public class MainActivity extends AppCompatActivity {

    TextView text;
    StringBuilder builder;
    Spanned mSpanned;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);
        mSpanned = createSpannable(YourList); //Pass your list
        text.setText(mSpanned);

    }

    Spanned createSpannable(List<String> interests) {
        builder = new StringBuilder();
        int size = interests.size();
        for (int i = 0; i < size; i++) {
            builder.append(interests.get(i));
            if (i < size - 1) {
                builder.append(" <font color=\"#c52d08\">&#8226</font> ");
            }
        }
        return Html.fromHtml(builder.toString());
    }

}