在android文本输入中标记关键字



我想有一个类似于这提供了一个字段(http://aehlke.github.io/tag-it/),但在Android应用程序。有人知道的实现或我应该做什么,使这项工作在Android?我浏览了一下,什么也没找到。I am new to Android

好的,我找到了一个很好的解决方案,我调整和简化了一点,它在这里: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" />

如果有人需要这个,请告诉我,我忘了什么。

相关内容

  • 没有找到相关文章

最新更新