Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I figure out a simple solution by using a tag like HTML</p> <pre><code>ImageBtn.setOnClickListener(new OnClickListener() { @override public void onClick(View view) { // TODO Auto-generated method stub mContentEdit.append("[1;34m [m"); //this is the tag I use //here I simplely append new tag to the end ,we can also add this tag where cursor is } }); </code></pre> <p>add a TextWatcher for my EditText;</p> <pre><code> mContentEdit.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub switch(mInsertIndex){ case 1: mSelection = mContentEdit.getSelectionStart(); break; case 2: mContentEdit.setSelection(mSelection); break; //recover the cursor's position } } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub switch (mInsertIndex) { case 1: //this is p:Pattern p = Pattern.compile("\\[[\\d{1};]*(3\\d{1})m(.*?)\\[[\\d;]*m"); Matcher m = p.matcher(s.toString()); StringBuffer sb = new StringBuffer(); while(m.find()){ m.appendReplacement(sb, "&lt;font color=#"+CssUtil.getColor(m.group(1))+"&gt;"+"[1;"+m.group(1)+"m"+m.group(2)+"[m"+"&lt;/font&gt;"); } //add a tag otherwise the next time we input,color will be gone; m.appendTail(sb); mInsertIndex = 2; mContentEdit.setText(Html.fromHtml(sb.toString())); break; //replace tag by html color case 2: mInsertIndex = 1; break; //avoid recursive message loop } } </code></pre> <p>this code does good job for my application. I can post the raw text in edittext directly to website, and website will parse the tags for css color;</p> <p>But there is still defect:</p> <p>1: What if I don't want see the tags? In my question , what I need is actually a color state machine,once btn pressed ,where ever u input ,the color will make effect. But in my solution ,I have to input in the tags.</p> <p>2:With the growth of the input length, efficiency might be a problem; As u see, a regex is employed;</p> <p>So, still looking forward to a hero to solve my problem!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload