How display formatted text in EditText?

拟墨画扇 提交于 2019-12-11 00:50:22

问题


Now I am writing simple note app. And i need to display formatted separate selected text in EditText.

I tried,

EditText et = (EditText)findViewById(R.id.edittext);
String string;
int startSelection = et.getSelectionStart();
int endSelection = et.getSelectionEnd();

string = et.getText().toString();
string.substring(startSelection, endSelection);



Spanned s = Html.fromHtml("<font color=\"red\">" + string + "</font>");

et.setText(s);

This solution displays only separate selected text. But i need to display formatted text among other text. I have no idea.

UPD: Formatting happening, when user clicks on the button.


回答1:


Try this

EditText text = new EditText(this); 
text.setText(Html.fromHtml(html));
text.setMovementMethod(LinkMovementMethod.getInstance());



回答2:


try using this

String string = et.getText().toString();
    String selectedString = string.substring(startSelection, endSelection);

    Spanned s = Html.fromHtml(string.replace(selectedString, "<font color=\"red\">" + selectedString + "</font>"));



回答3:


If you talking about formatting the Text, You can always do like this

EditText et = new EditText(this);
et.setText(Html.fromHtml("Simple Text <B>Formatted Text</B> Simple Text Again"));

And If you want to linkify a specific part of the Text then do it like this using this Blog

    EditText mTextSample = new EditText(this);
    String text = "click to see stackoverflow.com here";   
    mTextSample.setText(text);        
    Pattern pattern = Pattern.compile("stackoverflow.com");   
    Linkify.addLinks(mTextSample, pattern, "http://"); 


来源:https://stackoverflow.com/questions/11363042/how-display-formatted-text-in-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!