0

我希望有一个类似于它提供的字段(http://aehlke.github.io/tag-it/),但在 Android 应用程序中。有谁知道一个实现或我应该怎么做才能在 Android 中完成这项工作?我浏览过,一无所获。我是安卓新手

4

1 回答 1

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" />

如果有人需要这个,请告诉我,我忘记了一些东西。

于 2013-05-05T16:52:04.267 回答