
[英]Android: Restrict user from selecting other than autocompletion suggestions?
[英]MultiAutoCompleteTextView android restrict user from selecting same value multiple times
MultiAutoCompleteTextView android 如何限制用户多次选择相同的值?
您可以检查在 ItemClick 事件中选择的值。
我为您制作了一个代码示例。
布局2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
主Activity.cs
var multiAutoCompleteTextView1 = FindViewById<MultiAutoCompleteTextView>(Resource.Id.multiAutoCompleteTextView1);
string[] arraydata = { "apple1", "apple2", "shanghai1", "shanghai2" };
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, arraydata);
multiAutoCompleteTextView1.Adapter = adapter;
multiAutoCompleteTextView1.SetTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
string PreviousText = string.Empty;
multiAutoCompleteTextView1.ItemClick += (s, e) =>
{
var currentText = multiAutoCompleteTextView1.Text;
var currentList = currentText.Replace(" ","").Split(',');
var length = currentList.Length;
if (length > 2)
{
for (int i = 0; i < length - 1; i++)
{
if (i < length - 2)
{
if (currentList[i] == currentList[length - 2])
{
multiAutoCompleteTextView1.Text = PreviousText;
//do the something when you select the same value
}
}
}
}
PreviousText = multiAutoCompleteTextView1.Text;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.