`

EditText设定文本背景实用需求

 
阅读更多
之前想做这样一个效果,
如图:


之前的code:
et = (EditText) findViewById(R.id.et);
         String text = "<font style='background-color:#FFCCFF'>"+"123456789"+"</font>";
         et.setText(Html.fromHtml(text));
 

这个是实现不了的.

后来发现EditView的selectAll()属性.
该属性意味选中当前所有文本。
修改之后:
 et = (EditText) findViewById(R.id.et);
        String text = "123456456";
        et.setText(text);
        et.selectAll();//设定全部选中
        et.setHighlightColor(Color.BLUE);//设定选中背景色


这样,文本背景色设定OK.
效果:


如果还要设定文本字体颜色,如上如一样。
可以这样去做:
 et = (EditText) findViewById(R.id.et);
        String text = "123456456";
        et.setText(Html.fromHtml("<font color=red>"+text+"</font>"));
        et.selectAll();//设定全部选中
        et.setHighlightColor(Color.BLUE);//设定选中背景色

效果:
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics