主要思路就是在edittext重绘的时候加上分隔符,下面的代码只是一种示例,在每个字符后加了一个逗号。
[code="java"]public class CustomEdit extends EditText {
public CustomEdit(Context context, AttributeSet attrs) {
super(context, attrs);
}
private String oldText = "";
@Override
protected void onDraw(Canvas canvas) {
String text = getText().toString();
text = text.replace(",", "");
if (!oldText.equals(text)) {
oldText = text;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
builder.append(text.charAt(i)).append(",");
}
setText(builder);
setSelection(builder.length());
}
super.onDraw(canvas);
}
}
[/code]
获取editext中的文本:
String text = getText().toString();
text = text.replace(",","");
[code="java"]public class CustomEdit extends EditText {
public CustomEdit(Context context, AttributeSet attrs) {
super(context, attrs);
}
private String oldText = "";
@Override
protected void onDraw(Canvas canvas) {
String text = getText().toString();
text = text.replace(",", "");
if (!oldText.equals(text)) {
oldText = text;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
builder.append(text.charAt(i)).append(",");
}
setText(builder);
setSelection(builder.length());
}
super.onDraw(canvas);
}
}
[/code]
获取editext中的文本:
String text = getText().toString();
text = text.replace(",","");