android

时间:2016-02-10 10:36:33

标签: android

我在编辑文本时面临一些问题,实际上我需要的是输入的前10个字母应为红色,此后输入的字母应为灰色。我做了换色,问题是当我输入第11个字母时,所有以前输入的10个字母的颜色也在改变。我想保持第1个10个字母的颜色不变,颜色应该只在第10个字母后改变。请帮帮我。

this is my code, i want user to enter the text in live, after he enter a message of 10 letter,the 11th letter colour should be changed

3 个答案:

答案 0 :(得分:0)

尝试使用Spannable

Spannable spannable = myEditText.getText();
spannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

答案 1 :(得分:0)

使用 try: dataFile = open('data.txt', 'w') try: dataFile.write(data) except (Exception) as inst: print(type(inst)) print("error at writing to file") except (Exception) as inst: print(type(inst)) print("error opening file") finally: dataFile.close() 字符串作为

Spannable

答案 2 :(得分:0)

试试这段代码(我测试过它并按预期工作):

final EditText input = (EditText) findViewById(R.id.my_edittext);
input.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

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

            Spannable spannable = input.getText();
            if (!TextUtils.isEmpty(s) && s.length() > 9){
                spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this, R.color.my_color_red)),
                        0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {}
    });