开发者

Android EditText Dynamicaly changing text to Emoticons

how can i change Dynamically the text written in the edit to an Emoticon... Ex: "nice to meet you :) " = Dynamic Result "nice to meet you [pic]smiley[pic]":

    Editest.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        public void afterTextChanged(Editable s) {开发者_StackOverflow中文版


        }
    });


Hint to do so:

What you might be able to do is create a custom Manager that is a container for EditFields and BitmapFields. When the user clicks in the initial EditField that is inside of your Manager and begins typing, you let them type until they want an emoticon. When they select it you create a BitmapField right after the EditField and then place a new EditField to the right of the BitmapField. You'll have to keep track of things like when the user hits backspace and empties an EditField, it should be removed, and then they are selecting the BitmapField, and a subsequent backspace would delete the BitmapField and put focus on the previous EditField.

You will have to also create your own EditField that you can control the size of, and BitmapField that allows for backspace to delete it.

As far as I can think ahead, you shouldn't have any problem using this for 5.0

Edit for comments:

You'll have to be implementing your own sublayout() of your Manager in any approach you take, so you know positions because you're the one who put them there. If you want to want to do multi-line (my approach was for single-line), you can do one of three things:

  1. Just have new EditFields for each line and do the linking the same way I talked about where backspace drops you to the previous Field, in this case the last line. You have to keep measurements of how big the field is based off of the text, and see if that will move it to the next line. If it does just adjust where the EditFields are positioned.

  2. You can do your image placement in the multi-line field by implementing your own EditField that expands height, in which case you'll know where the text is and what lines you are on. Drawing the Bitmap would be a matter of calculating (what line number you are) * (font height) and getAdvance(text_up_to_bitmap).

  3. Implement your own EditField where you have a focusable Manager that you manually do all of the drawing. You can parse a String that you save from capturing key strokes and maybe have a flag where an emoticon should be. Draw the text before it, draw the emoticon, then continue drawing the text. The difficulty with this is you will have to do things like determining where the cursor should be, bringing up the virtual keyboard, and handling some of the other tasks that the EditField does for you.


You can use regular expression to scan the text for the string ":)" and then replace it with smiley.

As I don't know what your app is exactly doing I cannot say if it is a good idea to use a TextWatcher as it will fire each time a character is/was entered. Perhaps it might be better to do the replacement after a certain event like pressing an OK button or when the TextView loses focus.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜