0

我要做的是实现一个能够将字母着色为绿色或红色的文本输入。我的一段代码可以做到这一点,但有一个问题。我不能写国家信函,因为弹出窗口没有出现。


edit_text.addTextChangedListener(new TextWatcher() {
    boolean input_changed = false;        

    private boolean isInputBlocked()
    {
        this.input_changed = !this.input_changed;                
        return !this.input_changed;                
    }

    @Override
    public void afterTextChanged(Editable s) 
    {
        // Prevent recursive 
        if (isInputBlocked()) return;                                

        // Some staff                                                

        Outer.this.edit_text.setText(Html.fromHtml(html_input.toString()));
    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) 
    {}        
});

当我评论Outer.this.edit_text.setText(Html.fromHtml(html_input.toString())); ,出现弹出窗口。

4

1 回答 1

1

InputFilter 帮我解决了这个问题:

InputFilter filter = new InputFilter() {
        final String good_letter = "<font color='#2FEE0D'>$</font>";
        final String bad_letter = "<font color='#FF0000'>$</font>";

        public CharSequence filter(CharSequence source, int start, int end, 
                Spanned dest, int dstart, int dend) 
        {
            String input = dest.toString().substring(0, dstart) + source.
                    subSequence(start, end) + dest.toString().substring(dend);
            StringBuffer output = new StringBuffer();
            List<Entry<Character, Boolean>> correction = Learn.this.
                learn_manager.getLetters(input);

            Log.d(TAG, "afterTextChanged: input size (" + input.length() + 
                    ")");

            System.out.println(input);

            for (int i = dstart; i < dstart + end; i++)
            {                    
                if (correction.get(i).getValue())
                {
                    output.append(this.good_letter.replace('$', correction.
                            get(i).getKey()));
                } else {

                    output.append(this.bad_letter.replace('$', correction.
                            get(i).getKey()));
                }
            }

            return Html.fromHtml(output.toString());
        } 
};
于 2012-09-24T20:47:00.713 回答