如何在Android应用中使用AutoCompleteTextView传递用户的选择?

时间:2016-05-16 19:59:26

标签: android xamarin xamarin.android

我正在Xamarin中构建一个应用程序,我已经有了一个AutoCompleteTextView,可以在用户开始输入文本后为其提供多个选择。它可以工作,但由于某种原因,我无法将选择保存在字符串中并传递给下一个函数。

    string choise;

    void InitializeAutocomplete() {
        var autoCompleteOptions = new String[] { "hello", "see you" };
        ArrayAdapter autoCompleteAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);

        var autoCompleteTextView = FindViewById<AutoCompleteTextView>(Resource.Id.autoComplete);
        autoCompleteTextView.Adapter = autoCompleteAdapter;
        choise = ?!?!
    }

如何实现它?

1 个答案:

答案 0 :(得分:0)

通过Xamarin论坛上的this帖子管理解决它:

添加了:

void autoTextView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
    AutoCompleteTextView autoText = (AutoCompleteTextView)sender;
    choise = autoText.Text;
}

void InitializeAutocomplete() {
        var autoCompleteOptions = new String[] { "hello", "see you" };
        ArrayAdapter autoCompleteAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);

        var autoCompleteTextView = FindViewById<AutoCompleteTextView>(Resource.Id.autoComplete);
        autoCompleteTextView.Adapter = autoCompleteAdapter;
        autoCompleteTextView.ItemClick += autoTextView_ItemClick;
    }

现在变量选择返回用户选择。