Android中的标签或泡泡在EditText中

在这里输入图像说明 有人可以请我指出正确的方向如何在EditText做这些泡沫或标签,就像当你想添加一个圈子或联系人,当你想在stream的Google+中编写的东西时看到的那些东西? 矩形是一个自动完成的edittext。

您所显示的是与SMS股票应用程序相同的行为。 在这里search代码,看看它是如何完成的。

编辑:

代码应该在platform_packages_apps_mms中 。 看看RecipientsEditor类。

我在github上构build了TokenAutoComplete来解决类似的问题,它也应该为你工作。 以下是一个演示应用程序的基本实现:

public class ContactsCompletionView extends TokenCompleteTextView { public ContactsCompletionView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected View getViewForObject(Object object) { Person p = (Person)object; LayoutInflater l = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); LinearLayout view = (LinearLayout)l.inflate(R.layout.contact_token, (ViewGroup)ContactsCompletionView.this.getParent(), false); ((TextView)view.findViewById(R.id.name)).setText(p.getEmail()); return view; } @Override protected Object defaultObject(String completionText) { //Stupid simple example of guessing if we have an email or not int index = completionText.indexOf('@'); if (index == -1) { return new Person(completionText, completionText.replace(" ", "") + "@example.com"); } else { return new Person(completionText.substring(0, index), completionText); } } } 

contact_token的布局代码(您可以在这里使用任何types的布局,或者如果您需要令牌中的图像,则可以投入ImageView)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/token_background" android:padding="5dp" android:textColor="@android:color/white" android:textSize="18sp" /> </LinearLayout> 

令牌背景可绘制

 <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffafafaf" /> <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp" android:topRightRadius="5dp" android:bottomRightRadius="5dp" /> </shape> 

人员对象代码

 public class Person implements Serializable { private String name; private String email; public Person(String n, String e) { name = n; email = e; } public String getName() { return name; } public String getEmail() { return email; } @Override public String toString() { return name; } } 

示例活动

 public class TokenActivity extends Activity { ContactsCompletionView completionView; Person[] people; ArrayAdapter<Person> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); people = new Person[]{ new Person("Marshall Weir", "marshall@example.com"), new Person("Margaret Smith", "margaret@example.com"), new Person("Max Jordan", "max@example.com"), new Person("Meg Peterson", "meg@example.com"), new Person("Amanda Johnson", "amanda@example.com"), new Person("Terry Anderson", "terry@example.com") }; adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people); completionView = (ContactsCompletionView)findViewById(R.id.searchView); completionView.setAdapter(adapter); } } 

布局代码

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.tokenautocomplete.ContactsCompletionView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> 

你可以通过创buildandroid.text.style.DynamicDrawableSpan的子类来实现。 ImageSpan就是一个例子:它用图像replace文本的跨度(范围)。

这个例子会在编辑字段中放置一个星号,代替文本“test”。 在布局中创build一个EditText,其ID为“text”,并放在onCreate() (或任何地方):

  EditText mText = (EditText) findViewById(R.id.text); final Editable e = mText.getEditableText(); final SpannableStringBuilder sb = new SpannableStringBuilder(); sb.append("test"); sb.setSpan(new ImageSpan(this, android.R.drawable.btn_star), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); e.append(sb); 

我没有看到任何看起来像可以用普通文本打包的类,但是通过重写getDrawable()方法并自己渲染文本,可以很容易地解决这个问题。

我解决了这里这里联系气泡EditText

 final SpannableStringBuilder sb = new SpannableStringBuilder(); TextView tv = createContactTextView(contactName); BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv); bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight()); sb.append(contactName + ","); sb.setSpan(new ImageSpan(bd), sb.length()-(contactName.length()+1),sb.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); to_input.setText(sb); public static Object convertViewToDrawable(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.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(viewBmp); } public TextView createContactTextView(String text){ //creating textview dynamically TextView tv = new TextView(this); tv.setText(text); tv.setTextSize(20); tv.setBackgroundResource(R.drawable.oval); tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_search_api_holo_light, 0); return tv; } 

如果你的意思是提示,你可以简单地添加:

 android:hint="@string/myHint" 

这将在灰色的标签在EditText中,当它是空的。

要设置EditText左侧的圆形图标,您可以更改左侧的leftDrawable

你可以在layout xml文件android:drawableRight="@drawable/search_icon"或编程方式下使用setCompoundDrawablesWithIntrinsicBounds函数来完成。

如果你也想给气泡风格,你必须改变具有风格的9补丁可绘制的背景。 在这里你有一个谷歌地图9补丁泡沫教程。

希望它有帮助! 🙂

我认为它使用setCompoundDrawables()方法插入编辑文本中的图片

我决定给社区一些东西,并创build图书馆,目的是解决这个确切的问题,你有。 图书馆和示例项目一起在GitHub上可用: https : //github.com/RafalManka/BubbleEditText