MultiAutoCompleteTextView android限制用户多次选择相同的值

问题描述 投票:0回答:1

MultiAutoCompleteTextView android如何限制用户多次选择相同的值?

android xamarin.android multiautocompletetextview
1个回答
1
投票

您可以检查在ItemClick事件中选择的值。

我为您制作代码示例。

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

MainActivity.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;
        };

在示例代码中,当选择相同的值时,我会重置MultiAutoCompleteTextView的值。enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.