gpt4 book ai didi

android - 创建具有类似标签功能的自定义 editText

转载 作者:IT老高 更新时间:2023-10-28 21:59:41 29 4
gpt4 key购买 nike

我一直在四处寻找,但找不到任何答案。我要实现的是一个类似于 ICS gmail 应用程序的撰写屏幕中的“收件人”字段的 EditText。

这是一张描述我想要的图片: enter image description here

我正在考虑扩展 EditText 并实现我自己的自定义 EditText 类,但我不确定如何做到这一点,或者即使这是最好的解决方案。有什么想法吗?

最佳答案

改编自 this answer 的解决方案。插入逗号时自动分隔输入(分隔符可以调整)。创建一个 ImageSpan 和一个 ClickableSpan(可以通过单击右侧部分来删除条目)。

public class TagEditText extends EditText {

TextWatcher textWatcher;

String lastString;

String separator = ",";

public TagEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}


private void init() {
setMovementMethod(LinkMovementMethod.getInstance());

textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
String thisString = s.toString();
if (thisString.length() > 0 && !thisString.equals(lastString)) {
format();

}
}
};

addTextChangedListener(textWatcher);
}


private void format() {

SpannableStringBuilder sb = new SpannableStringBuilder();
String fullString = getText().toString();

String[] strings = fullString.split(separator);


for (int i = 0; i < strings.length; i++) {

String string = strings[i];
sb.append(string);

if (fullString.charAt(fullString.length() - 1) != separator.charAt(0) && i == strings.length - 1) {
break;
}

BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(createTokenView(string));
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());

int startIdx = sb.length() - (string.length());
int endIdx = sb.length();

sb.setSpan(new ImageSpan(bd), startIdx, endIdx, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

MyClickableSpan myClickableSpan = new MyClickableSpan(startIdx, endIdx);
sb.setSpan(myClickableSpan, Math.max(endIdx-2, startIdx), endIdx, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

if (i < strings.length - 1) {
sb.append(separator);
} else if (fullString.charAt(fullString.length() - 1) == separator.charAt(0)) {
sb.append(separator);
}
}


lastString = sb.toString();

setText(sb);
setSelection(sb.length());

}

public View createTokenView(String text) {


LinearLayout l = new LinearLayout(getContext());
l.setOrientation(LinearLayout.HORIZONTAL);
l.setBackgroundResource(R.drawable.bordered_rectangle_rounded_corners);

TextView tv = new TextView(getContext());
l.addView(tv);
tv.setText(text);
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);

ImageView im = new ImageView(getContext());
l.addView(im);
im.setImageResource(R.drawable.ic_cross_15dp);
im.setScaleType(ImageView.ScaleType.FIT_CENTER);

return l;
}

public Object convertViewToDrawable(View view) {
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), 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(getContext().getResources(), viewBmp);
}

private class MyClickableSpan extends ClickableSpan{

int startIdx;
int endIdx;

public MyClickableSpan(int startIdx, int endIdx) {
super();
this.startIdx = startIdx;
this.endIdx = endIdx;
}

@Override
public void onClick(View widget) {



String s = getText().toString();

String s1 = s.substring(0, startIdx);
String s2 = s.substring(Math.min(endIdx+1, s.length()-1), s.length() );

TagEditText.this.setText(s1 + s2);
}

}
}

R.drawable.bordered_rectangle_rounded_corners:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/transparent"/>
<stroke android:width="1dp" android:color="#AAAAAA" />
<corners
android:radius="100dp" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
</shape>

最后要添加的是“x 按钮”的 png。到目前为止效果很好,唯一的问题是长按删除键不起作用(如果有人知道如何使它起作用,请随时发表评论)

关于android - 创建具有类似标签功能的自定义 editText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11318551/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com