类似微信聊天中的@好友功能,封装到一个EditText中,gist打不开了,直接贴代码到这里吧:
/**
* @好友的输入组件
*/
public class AtEditText extends EditText {
/**
* @的text的最长长度,根据addAt方法来更新
*/
private int mMaxAtTextLength = 4;
private OnAtChangedListener mOnAtInputedListener = null;
public AtEditText(Context context) {
this(context, null);
}
public AtEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public AtEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public void setOnAtInputedListener(OnAtChangedListener l) {
mOnAtInputedListener = l;
}
/**
* 以@方式添加一个text
*
* @param atText
*/
public void addAt(String atText) {
if (TextUtils.isEmpty(atText)) {
return;
}
// 前面插入@,后面插入空格
String str = "@" + atText + " ";
Editable edit = getEditableText();
edit.insert(getSelectionEnd(), str);
// 更新最长的@ length
int len = atText.length();
if (len + 1 > mMaxAtTextLength) {
mMaxAtTextLength = len + 1;
}
}
/**
* @param atText
* @return 返回将会插入到edit的带@的text,不会插入到EditText中
*/
public String sudoAddAt(String atText) {
if (TextUtils.isEmpty(atText)) {
return null;
}
// 前面插入@,sudo后面不插入空格
String str = "@" + atText;
return str;
}
private void init(Context context, AttributeSet attrs) {
this.addTextChangedListener(new AtTextWatcher());
}
private int[] findAt(Editable edit, int position) {
if (edit == null) {
return null;
}
if (edit.length() < 2) {
return null;
}
if (position < 2) {
return null;
}
if (position > edit.length()) {
return null;
}
String str = edit.toString();
for (int i = position - 1, j = 0; i >= 0 && j < mMaxAtTextLength; i--, j++) {
if (‘ ‘ == str.charAt(i)) {
return null;
}
if (‘@‘ == str.charAt(i)) {
int startOfDel = i;// 删除的开始index
int endOfDel = position;
return new int[] { startOfDel, endOfDel };
}
}
return null;
}
private class AtTextWatcher implements TextWatcher {
private int countOfBefore = 0;
private char onC = 1;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
countOfBefore = s.length();
if (getSelectionEnd() - 1 > -1 && getSelectionEnd() - 1 < s.length()) {
onC = s.charAt(getSelectionEnd() - 1);
}
}
@Override
public void afterTextChanged(Editable edit) {
int countOfAfter = edit.length();
int offset = countOfAfter - countOfBefore;
if (offset == 1) {// 在输入
if (getSelectionEnd() - 1 > -1 && getSelectionEnd() - 1 < edit.length()) {
char c = edit.charAt(getSelectionEnd() - 1);
if (c == ‘@‘) {// 在输入@
if (mOnAtInputedListener != null) {
if (mOnAtInputedListener.onAtInputed() == true) {
edit.delete(getSelectionEnd() - 1, getSelectionEnd());
}
}
}
}
} else if (offset == -1) {// 在删除
if (onC == ‘ ‘) {// 在删除空格
int[] delIndexs = findAt(edit, getSelectionEnd());
if (delIndexs != null && delIndexs.length == 2) {
int startOfDel = delIndexs[0];
int endOfDel = delIndexs[1];
CharSequence cs = edit.subSequence(startOfDel + 1, endOfDel);
edit.delete(startOfDel, endOfDel);
if (mOnAtInputedListener != null && cs != null) {
mOnAtInputedListener.onAtDeleted(cs.toString());
}
}
}
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
/**
* 与@相关的输入变化时
*
* @author viyu
*
*/
public interface OnAtChangedListener {
/**
* 当输入了@的时候回调
*
* @return true if你要删除刚刚输入的@,自己接管;false的话就不管
*/
public boolean onAtInputed();
/**
* 当删除了一个 "@abc "的时候的回调
*
* @param textWithoutAt
* "@abc "被删除的话,就是"abc"
*/
public void onAtDeleted(String textWithoutAt);
}
}