2012-07-01 297 views
4

我仍然是Android的新手,我试图绕着自动完成文本框工作。我正在使用MultiAutoCompleteTextView来填充文本框,并从字符串数组中提供提示。数组中的每个字符串都是具有一个id的对象的名称。所以,我的问题是双重的:Android自动完成

  • 上自动完成定条目的用户点击后,我如何才能找到对应于由用户选择的串号?

  • 是否可以在从自动完成列表中选择的项目周围创建一个“类似Facebook的框”,该列表与用户可以通过按下X来删除的原子单元一样工作? (类似于在每一个标签在这里发生的事情在标签框计算器)

在此先感谢

回答

4

Android有在邮件应用程序中使用的“芯片”小部件的源代码。它们代表了接收消息的用户的芯片。它们看起来就像您所指的Facebook小部件:带有“X”的名称可以取消。我能够调整代码并使其适用于我自己的需求,但说实话,它确实非常复杂,我花了很长时间来围绕它。

核心原理是您使用Spannable字符串并手动为背景和“X”绘制位图。

这里是Android源代码:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/com/android/ex/chips/RecipientEditTextView.java

核心方法createSelectedChipconstructChipSpan

+0

嗯,不错,这将做到这一点,谢谢!我认为在Android中有一种“预先定义”的方式,但这给了它更大的灵活性。 :) – Sagito

+0

我们可以在我们的项目中使用这个芯片库吗?或者是否有任何授权问题? –

+0

自从我发布了我的答案后,我已经发现了这个包:https://github.com/splitwise/TokenAutoComplete –

4

我开源了一个TokenAutoComplete on github,解决了这个很好。那里似乎没有更简单的答案。这是一个基本实现你的描述:用于contact_token

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.getName()); 

     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); 
     } 
    } 
} 

布局代码(你需要找到自己的X绘制)

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

    <ImageView 
     android:layout_height="10dp" 
     android:layout_width="10dp" 
     android:src="@drawable/x" 
     android:layout_gravity="center_vertical" 
     android:layout_marginLeft="3dp" 
     android:layout_marginRight="5dp" /> 
</LinearLayout> 

令牌backgound绘制

<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", "[email protected]"), 
       new Person("Margaret Smith", "[email protected]"), 
       new Person("Max Jordan", "[email protected]"), 
       new Person("Meg Peterson", "[email protected]"), 
       new Person("Amanda Johnson", "[email protected]"), 
       new Person("Terry Anderson", "[email protected]") 
     }; 

     adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people); 

     completionView = (ContactsCompletionView)findViewById(R.id.searchView); 
     completionView.setAdapter(adapter); 
     completionView.setTokenClickStyle(TokenCompleteTextView.TokenClickStyle.Delete); 
    } 
} 

布局代码

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

这里就是你得到的接口:

Image of working TokenAutoComplete activity

+0

我怎样才能得到所有选定的值?我的意思是,如果用户输入的电子邮件地址不在定义数组列表中,那么我们怎样才能得到它? – Scorpion

+0

getObjects将返回字段中令牌的所有对象。如果它是一个新对象,它将是你从defaultObject返回的值 –

+0

您好先生,我有一个问题。我想删除交叉标记图像的onclick。我可以知道如何做这种令牌风格。 – Rajasekhar