如何在单击按钮时获取 autocompleteTextView 的值?

How can I get the value of the autocompleteTextView on button click?

我需要一些帮助。我有这个代码:

string hozzaadnivalo;

public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {

            View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false);
            container.AddView(view);

            Button hozzaadas = view.FindViewById<Button>(Resource.Id.hozzaad);

            var autoCompleteOptions = new string[] { "Sajt", "Tej", "Kecske", "Barátnő", "piros", "alma" };
            ArrayAdapter autoCompleteAdapter = new ArrayAdapter(container.Context, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);
            AutoCompleteTextView mautoCompleteTextView = view.FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);
            mautoCompleteTextView.Adapter = autoCompleteAdapter;

            hozzaadas.Click += hozaadasListViewhez;
            hozzaadnivalo = mautoCompleteTextView.Text;
        }
private void hozaadasListViewhez(object sender, EventArgs e)
        {
            adapter.Add(mautoCompleteTextView.Text);
            adapter.NotifyDataSetChanged();
        }

所以我想将 autocompleteTextView 的文本添加到我的列表视图适配器,但我不能在那里做,因为那里不存在它。所以我创建了一个等于 mautoCompleteTextView.Text 的字符串,但它将是空的,因为它会在程序启动时 运行,此时用户还没有做任何事情。所以我的问题是当用户按下按钮时我无法获得 mautoCompleteTextView.Text 。如果有人能阻止那将是非常感谢。

您是在您的方法中声明 mautoCompleteTextView,因此它只能在该方法中本地使用。

如果将声明移到方法之外,则 mautoCompleteTextView 将在整个 class 中可用。

// declare it here
AutoCompleteTextView mautoCompleteTextView;

// then instantiate it within the method
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
    {
       ... // code omitted

       mautoCompleteTextView = view.FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);

       ... // code omitted
    }