我在写“ #data”时在EditText中写一些文本,其颜色应更改,但不更改我该怎么办.请检查以下我使用过的EditText
<EditText androID:ID="@+ID/et_simple" androID:layout_height="wrap_content" androID:layout_wIDth="match_parent"></EditText>
解决方法:
希望此解决方案将对您有所帮助.
我用这个解决方案非常有用!例如在您的editText上添加textWatcher界面,然后侦听textChange并找出单词是否以hashTag开头,然后对该单词调用Change The color方法!它有一些缺陷,但那些是可忽略的,请在此处查看此简单的缺陷.
Spannable mspanable;int hashTagIsComing = 0;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); final EditText edtTxtmine = (EditText) findVIEwByID(R.ID.editText1); mspanable = edtTxtmine.getText(); edtTxtmine.addTextChangedListener(new TextWatcher() { @OverrIDe public voID onTextChanged(CharSequence s, int start, int before, int count) { String startChar = null; try{ startChar = Character.toString(s.charat(start)); Log.i(getClass().getSimplename(), "CHaraCTER OF NEW WORD: " + startChar); } catch(Exception ex){ startChar = ""; } if (startChar.equals("#")) { changeThecolor(s.toString().substring(start), start, start + count); hashTagIsComing++; } if(startChar.equals(" ")){ hashTagIsComing = 0; } if(hashTagIsComing != 0) { changeThecolor(s.toString().substring(start), start, start + count); hashTagIsComing++; } } @OverrIDe public voID beforeTextChanged(CharSequence s, int start, int count, int after) { // Todo auto-generated method stub } @OverrIDe public voID afterTextChanged(Editable s) { // Todo auto-generated method stub } });}private voID changeThecolor(String s, int start, int end) { mspanable.setSpan(new ForegroundcolorSpan(getResources().getcolor(R.color.color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}
总结 以上是内存溢出为你收集整理的android-如何在EditText中以不同颜色标记值?全部内容,希望文章能够帮你解决android-如何在EditText中以不同颜色标记值?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)