如何在显示之前更改TextView的输入文本

时间:2014-08-30 00:30:30

标签: android android-activity android-edittext

我需要在显示原始文本之前更改editView的输入文本 我试图制作onKeyListener,但它没有使用虚拟键盘 这是我的尝试

 OnKeyListener okl = new EditText.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {
            EditText et = (EditText) v;
            if(secFlag.equals(false))
            {
                if(petitionText.getText().length()==19)
                 {
                     if(keyCode==KeyEvent.KEYCODE_COMMA)
                     {
                         secFlag=true;
                     }
                 }
            }
            if(secFlag)
            {
                if(pos<=c.length-1);
                shownText.append(c[pos]);
                if(keyCode==KeyEvent.KEYCODE_SPACE)
                {
                    sBuilder.append(' ');
                }else if (keyCode==KeyEvent.KEYCODE_COMMA) {
                    sBuilder.append(',');
                }else
                {

                    sBuilder.append((char)event.getUnicodeChar());
                }
                petitionText.setText(shownText.toString());
                pos=pos+1;
                petitionText.setSelection(petitionText.getText().length()-1);
            }
            return false;
        }
    };

1 个答案:

答案 0 :(得分:0)

您可以使用TextWatcher类。

    final EditText myedit = (EditText) findViewById(R.id.myedit);
    myedit.setOnKeyListener(this);
    TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i("my", "onTextChanged=" + s);
        }
    };
    myedit.addTextChangedListener(fieldValidatorTextWatcher);