edittext1.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
selStart = edittext1.getSelectionStart();
selEnd= edittext1.getSelectionEnd();
lengthBefore = edittext1.getText().toString().length();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int lengthAfter = edittext1.getText().toString().length() - lengthBefore;
if(!(lengthAfter < 0))
{
String codedBefore = editTextCoded.getText().toString().substring(0, selStart);
String codedAfter;
if(selEnd>1) {
codedAfter = editTextCoded.getText().toString().substring(selEnd);
}
else
{
codedAfter = "";
}
editTextCoded.setText(codedBefore);
//Zakodowanie nowego
String toBeCoded = edittext1.getText().toString().substring(selStart, selEnd+ lengthAfter);
for(int i=0; i<toBeCoded.length(); i++)
{
newChar = toBeCoded.charAt(i);
editTextCoded.setText(editTextCoded.getText().toString() + Character.toString(newChar));
}
//Dopisanie końcówki tłumaczenia
editTextCoded.setText(editTextCoded.getText().toString() + codedAfter);
}
else
{
String codedBefore = editTextCoded.getText().toString().substring(0, selStart + lengthAfter);
String codedAfter = editTextCoded.getText().toString().substring(selStart, editTextCoded.length());
editTextCoded.setText(codedBefore + codedAfter);
}
}
});
在此操作之后,editTextCoded和edittext1应该显示相同的文本,但是当我复制部分文本,然后选择另一个(较小的)部分并粘贴它替换所选部分时,editTextCoded和edittext1中的文本会有所不同。
任何人都可以帮助我吗?我不知道如何处理它。