Добавить пункт меню программно - PullRequest
0 голосов
/ 21 марта 2020

Привет, я хотел бы добавить пункт меню программно. Я знаю, как добавить пункт меню в XML, но как мне добиться этого динамически? Я ничего не нашел в inte rnet ... Возможно ли это вообще? Я хотел бы добиться чего-то вроде этого: https://youtu.be/Es5UFII4oak?t=832 (но в JAVA)

1 Ответ

0 голосов
/ 23 марта 2020

Вы должны переопределить OnPrepareOptionsMenu, когда хотите добавить элемент.

После того, как вы нажмете кнопку добавления, вы должны вызвать InvalidateOptionsMenu(); Вот код.

   [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        EditText menu_item;
        EditText menu_name;
        List<string> list;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
             menu_item = FindViewById<EditText>(Resource.Id.menu_item);
             menu_name = FindViewById<EditText>(Resource.Id.menu_name);
            Button add = FindViewById<Button>(Resource.Id.button1);

            list= new List<string>();
            add.Click += Add_Click;
        }

        private void Add_Click(object sender, System.EventArgs e)
        {
       //  int item_id=   Integer.ParseInt(menu_item.Text.ToString());
            string menu_name_text = menu_name.Text.ToString();
            list.Add(menu_name_text);

            ApplyChanged();
        }

        public void ApplyChanged()
        {
            InvalidateOptionsMenu();
        }

        public override bool OnPrepareOptionsMenu(IMenu menu)
        {
            menu.Clear();

            foreach (var item in list)
            {
                menu.Add(item);
            }

            return base.OnPrepareOptionsMenu(menu);
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

Вот макет.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <EditText
    android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="id"
    android:id="@+id/menu_item"/>

<EditText
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="name"
    android:id="@+id/menu_name"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="add"
        android:id="@+id/button1"
    />

</LinearLayout>

Здесь работает Gif.

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...