我希望有一个类似于它提供的字段(http://aehlke.github.io/tag-it/)但是在Android应用中。有没有人知道一个实现或我应该怎么做才能在Android中使用它?我浏览过,一无所获。我是Android新手
答案 0 :(得分:1)
好的,我找到了一个很好的解决方案,我调整了一下,简化了一下,就在这里:http://www.kpbird.com/2013/02/android-chips-edittext-token-edittext.html
基本上我扩展了MultiAutoCompleteTextView
。我还创建了一个自定义分隔符来使用空间,但这并不重要,可以在其他地方找到。我在代码中注释了这应该改为逗号。
我添加了一个自定义TextWatcher
实现,我在其中实现了onTextChange
方法来运行我称为bubbleWord()的方法(主要来自上面提到的来源)
private void bubbleWord() {
int numberOfBubbles = 0;
String triggersString = getText().toString();
//note that I use space as a separator
if (triggersString.contains(" ")) {
SpannableStringBuilder ssb = new SpannableStringBuilder(
getText());
BubbleMultiAutoCompleteTextView.this
.setTriggersArray(triggersString.trim().split(" "));
String[] triggers = BubbleMultiAutoCompleteTextView.this
.getTriggers();
for (String trigger : triggers) {
LayoutInflater lf = (LayoutInflater) getContext()
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) lf.inflate(
R.layout.bubble_edit, null);
textView.setText(trigger); // set text
int spec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(),
textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(),
textView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(),
-textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888,
true);
textView.destroyDrawingCache(); // destory drawable
// create bitmap drawable for imagespan
@SuppressWarnings("deprecation")
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
bmpDrawable.setBounds(0, 0,
bmpDrawable.getIntrinsicWidth(),
bmpDrawable.getIntrinsicHeight());
// create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable), numberOfBubbles,
numberOfBubbles + trigger.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
numberOfBubbles = numberOfBubbles + trigger.length() + 1;
}
// set chips span
setText(ssb);
// move cursor to last
setSelection(getText().length());
}
}
在布局文件夹中包含了这个文件(与之前相同,几乎与源文件相同但有点不同):
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edtTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9191B5"
android:drawablePadding="2dp"
android:drawableRight="@drawable/exclamation_octagon_fram"
android:padding="8dp"
android:shadowColor="#FFFFFF"
android:shadowDy="1"
android:shadowRadius="0.01"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
如果有人需要,请告诉我,我忘记了什么。