Android EditText控件自动转换大小写,光标位置不跳位

et.addTextChangedListener(new TextWatcher()
            {
                int index = 0;
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count)
                {
                    // TODO Auto-generated method stub
                }
                
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after)
                {
                    // TODO Auto-generated method stub
                }
                
                @Override
                public void afterTextChanged(Editable s)
                {
                    // TODO Auto-generated method stub
                    et.removeTextChangedListener(this);//解除文字改变事件
                    index = et.getSelectionStart();//获取光标位置
                    et.setText(s.toString().toUpperCase());转换
                    et.setSelection(index);//重新设置光标位置
                    et.addTextChangedListener(this);//重新绑定事件
                    Log.i("mylog", s.toString());
                }
            });


你可能感兴趣的:(Android EditText控件自动转换大小写,光标位置不跳位)