我在Activity中有一个EditText小部件,当用户在键入时按空格时会创建文本芯片。我将结果保存为逗号分隔值并将其存储在数据库中。
现在我正在尝试创建另一个Activity,它从数据库中获取数据并进行编辑。我能做的最大限度是在EditText中设置文本。
我被困在这里。我可以在用户输入时创建芯片,但不能通过代码创建芯片。我该怎么做才能获得理想的行为?
我的代码如下:
edt.setText("Europe USA London Spain");
edt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence s, int arg1, int arg2, int arg3) {
String addedString = s.toString();
if (addedString.length() > 0 && addedString.endsWith(" ")) {
if (!isTextAdditionInProgress) {
// something is added
edt.setCursorVisible(true);
int startIndex = isContactAddedFromDb ? addedString.length() : edt.getSelectionEnd();
startIndex = startIndex < 1 ? 1 : startIndex;
isTextAdditionInProgress = true;
addOrCheckSpannable(s, startIndex);
}
stringLength = addedString.length();
} else {
Log.v("", "Doesn't contain space, good to go!");
}
}
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
beforeChangeIndex = edt.getSelectionStart();
changeString = s.toString();
}
@Override
public void afterTextChanged(Editable s) {
if (!isTextDeletedFromTouch && s.toString().length() < changeString.length() && !isTextAdditionInProgress) {
String deletedString = "";
try {
deletedString = changeString.substring(afterChangeIndex, beforeChangeIndex);
} catch (Exception e) {
}
if (deletedString.length() > 0 && deletedString.contains(","))
deletedString = deletedString.replace(",", "");
}
}
});
public void addOrCheckSpannable(CharSequence s, int startIndex) {
boolean checkSpannable = false;
String overallString;
if (s == null) {
checkSpannable = true;
s = edt.getText();
startIndex = edt.getSelectionEnd();
startIndex = startIndex < 1 ? 1 : startIndex;
// startIndex =-1;
overallString = edt.toString();
if (TextUtils.isEmpty(overallString.trim()))
return;
} else {
overallString = s.toString();
startIndex = startIndex - 1;
}
int spanEnd = 0;
for (int i = startIndex - 1; i >= 0; i--) {
Character c = overallString.charAt(i);
if (c == ' ') {
spanEnd = i;
break;
}
}
SpannableStringBuilder ssb = new SpannableStringBuilder(s);
int cursorCurrentPoint = edt.getSelectionEnd();
boolean addedFromFirst = cursorCurrentPoint < overallString.length();
ClickableSpan[] spans = ssb.getSpans(0, addedFromFirst ? spanEnd : ssb.length(), ClickableSpan.class);
boolean someUnknownChange = false;
if (spans.length > 0) {
ClickableSpan underlineSpan = spans[spans.length - 1];
spanEnd = ssb.getSpanEnd(underlineSpan);
// int spanCheck = spanEnd+1;
int k = 0;
for (int m = 0; m < spans.length; m++) {
ClickableSpan someSpan = spans[m];
int end = ssb.getSpanEnd(someSpan);
;
if (k < end)
k = end;
}
spanEnd = k;
if (spanEnd < overallString.length()) {
Character c = overallString.charAt(spanEnd);
if (c == ' ') {
spanEnd += 1;
} else {
ssb.insert(spanEnd, " ");
spanEnd += 1;
startIndex += 1;
someUnknownChange = true;
}
}
}
if (startIndex > -1 && spanEnd > -1) {
if (checkSpannable) {
ClickableSpan[] span = someUnknownChange ? ssb.getSpans(spanEnd - 1, startIndex - 1, ClickableSpan.class) : ssb.getSpans(spanEnd, startIndex, ClickableSpan.class);
if (span.length > 0) {
// has span
} else if (startIndex > spanEnd) {
// ssb.replace(someUnknownChange?spanEnd-1:spanEnd,
// someUnknownChange?startIndex-1:startIndex , "");
ssb.replace(spanEnd, startIndex, "");
}
return;
} else {
// this is to checked whether the user deletes comma and adds
// again
if ((Math.abs(spanEnd - 1 - startIndex) > 1)) {
String userInputString = someUnknownChange ? overallString.substring(spanEnd - 1, startIndex - 1) : overallString.substring(spanEnd, startIndex);
String trimString = userInputString.trim();
if (trimString.length() == 0) {
ssb.replace(spanEnd, startIndex + 1, "");
} else {
if (userInputString.charAt(userInputString.length() - 1) == ' ' && spanEnd - 1 >= 0 && startIndex - 1 >= 0)
userInputString = overallString.substring(spanEnd - 1, startIndex - 1);
if (checkValidation) {
BitmapDrawable bmpDrawable = getBitmapFromText(userInputString);
bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight());
ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// mm=phoneNum.getMovementMethod();
edt.setMovementMethod(LinkMovementMethod.getInstance());
} else {
BitmapDrawable bmpDrawable = getBitmapFromText(userInputString);
bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight());
ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// mm=phoneNum.getMovementMethod();
edt.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
}
setSpannableText(ssb);
}
}
public void setSpannableText(final Spannable ssb) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
edt.setText(ssb);
edt.setSelection(edt.getText().toString().length());
resetFlags();
if (!ssb.toString().equalsIgnoreCase(" ")) {
str = ssb.toString();
}
}
}, 20);
}
public void resetFlags() {
isContactAddedFromDb = false;
isTextAdditionInProgress = false;
checkValidation = true;
}
private BitmapDrawable getBitmapFromText(String message) {
TextView textView = (TextView) layoutInflater.inflate(R.layout.textview, null);
textView.setText(message);
BitmapDrawable bitmapDrawable = (BitmapDrawable) extractBitmapFromTextView(textView);
return bitmapDrawable;
}
@SuppressWarnings("deprecation")
public static Object extractBitmapFromTextView(View view) {
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
view.setDrawingCacheEnabled(true);
Bitmap cacheBmp = view.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
view.destroyDrawingCache();
return new BitmapDrawable(viewBmp);
}
答案 0 :(得分:2)
试试这个会起作用, 添加此方法和变量
int count = 0;
private void setText(String str) {
final String[] names = str.split(" ");
count = names.length;
isContactAddedFromDb = true;
final Handler h = new Handler();
Runnable r = new Runnable() {
@Override
public void run() {
isContactAddedFromDb = true;
String s = names[names.length - count];
count--;
edt.append(s + " ");
if (count > 0)
h.postDelayed(this, 50);
}
};
h.post(r);
}
并像这样打电话
setText("Europe USA London Spain");
而不是
edt.setText("Europe USA London Spain");
答案 1 :(得分:0)
您可以使用spannable类来设置样式,调整大小并为文本添加颜色。以下是您可以尝试的一些链接:
android-spannable-text-view-to-change-color-size-style-and-adding-click-event-for-particular-word