【问题标题】:Android EditText Gmail like to fieldAndroid EditText Gmail 喜欢字段
【发布时间】:2012-11-24 17:30:34
【问题描述】:

我正在使用一个应用程序,我正在尝试制作一个类似“收件人”字段的 gmail,其中包含一旦添加就无法编辑的块,而只是完全删除(如附图中所示)。如果它也可以有图像,那将是完美的。

【问题讨论】:

  • 您好,您做到了吗?请告诉我。我也在尝试这样实现,但是遇到了很多问题@Ciprian
  • 最终成功了,但当时让我很头疼:)
  • @Ciprian 嘿,如果你有,请发布示例代码.. 否则请回答这个问题。如果你知道.. stackoverflow.com/questions/18780756/…
  • 只要按照 CommonsWare 的指示,我就会找到解决方案

标签: android gmail android-edittext


【解决方案1】:

我开源了我们的解决方案TokenAutoComplete on github。我的已经测试回 2.2。我设计了我的代码以允许非常简单的实现和自定义。

这是使用我的库的示例实现:

子类 TokenCompleteTextView

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 的布局代码(您可以在此处使用任何类型的布局,或者如果您想要令牌中的图像,可以将 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);
        completionView.setPrefix("To: ");
    }
}

布局代码

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

【讨论】:

【解决方案2】:

Roman Nurik 在a Google+ post 中讨论了这种称为“芯片”的技术。他反过来指向Macarse's answer here on StackOverflow。反过来,它们指向您在股票消息客户端中看到的the implementation of this UI

【讨论】:

  • @Gopinath:AFAIK,是的。如果您在使用它时遇到问题,请打开一个新的 StackOverflow 问题并提供详细信息。
  • @CommonsWare ,我尝试在 Google+ 帖子之后使用芯片 UI。它有很多对Android4.0框架类的引用,我放弃了。从那时起,社区中似乎提供了更好的样本。我的首选答案之一是stackoverflow.com/questions/10812316/contact-bubble-edittext/…
  • @CommonsWare 你是对的,但是很难理解和实现股票 mms 应用程序中的代码。我做不到。我希望它可以轻松重复使用
  • @CommonsWare 我知道,但自定义它并使用在线数据填充它对我来说似乎很难。
  • @CommonsWare 我无法导入 github.com/Derelicte/android_frameworks/tree/master/ex/chips 。我认为这些是在 ndk 环境中开发的。我无法测试或使用该代码。在多自动完成文本视图中实现芯片的任何直接或好的教程
猜你喜欢
  • 2016-11-05
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多