android '设置有选项菜单(布尔值):单位“已弃用,在Java中已弃用

qxgroojn  于 2023-02-06  发布在  Android
关注(0)|答案(7)|浏览(163)

如何在Android fragment中声明菜单?我之前使用的方法现在已弃用。

    • 原文:**
override fun onCreateView(...): View {
        setHasOptionsMenu(true)
    }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        this.menu = menu
        inflater.inflate(R.menu.menu, this.menu)
    }
gywdnpxw

gywdnpxw1#

根据开发人员文档,这可以通过以下方式实现:

/**
  * Using the addMenuProvider() API directly in your Activity
  **/
class ExampleActivity : ComponentActivity(R.layout.activity_example) {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Add menu items without overriding methods in the Activity
   addMenuProvider(object : MenuProvider {
      override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        // Add menu items here
        menuInflater.inflate(R.menu.example_menu, menu)
      }

      override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        // Handle the menu selection
        return true
      }
    })
  }
}

/**
  * Using the addMenuProvider() API in a Fragment
  **/
class ExampleFragment : Fragment(R.layout.fragment_example) {

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    // The usage of an interface lets you inject your own implementation
    val menuHost: MenuHost = requireActivity()
  
    // Add menu items without using the Fragment Menu APIs
    // Note how we can tie the MenuProvider to the viewLifecycleOwner
    // and an optional Lifecycle.State (here, RESUMED) to indicate when
    // the menu should be visible
    menuHost.addMenuProvider(object : MenuProvider {
      override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        // Add menu items here
        menuInflater.inflate(R.menu.example_menu, menu)
      }

      override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        // Handle the menu selection
            return when (menuItem.itemId) {
                R.id.menu_clear -> {
                    // clearCompletedTasks()
                    true
                }
                R.id.menu_refresh -> {
                    // loadTasks(true)
                    true
                }
                else -> false
            }
      }
    }, viewLifecycleOwner, Lifecycle.State.RESUMED)
  }

片段集有选项菜单已弃用,请使用集有选项菜单

kx5bkwkv

kx5bkwkv2#

扩展@joseph-wambura和@hammad-zafar-bawara所说的内容,您还可以实现片段中的接口...

class MyFragment : Fragment(), MenuProvider {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Do stuff...
        val menuHost: MenuHost = requireActivity()
        menuHost.addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)
    }

    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        menuInflater.inflate(R.menu.options, menu)
        // Do stuff...
    }

    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        // Do stuff...
        return false
    }
}
zmeyuzjn

zmeyuzjn3#

在Kotlin中,ActivityFragmentPreferenceFragmentCompat的声明

活动

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

   addMenuProvider(object : MenuProvider {
      override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        menuInflater.inflate(R.menu.main_menu, menu)
      }

      override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        // Handle the menu selection
        return true
      }
    })
  }
}

碎片

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    // The usage of an interface lets you inject your own implementation
    val menuHost: MenuHost = requireActivity()

    menuHost.addMenuProvider(object : MenuProvider {
      override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        // Add menu items here
        menuInflater.inflate(R.menu.main_menu, menu)
      }

      override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        // Handle the menu selection
            return when (menuItem.itemId) {
                R.id.action_menu1 -> {
                    // todo menu1
                    true
                }
                R.id.action_menu2 -> {
                    // todo menu2
                    true
                }
                else -> false
            }
      }
    }, viewLifecycleOwner, Lifecycle.State.RESUMED)
 }

首选项片段兼容

val menuHost: MenuHost = requireHost() as MenuHost
//Same declaration with Fragment

使用菜单提供程序接口

class FirstFragment : Fragment(), MenuProvider {
 
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
              val menuHost: MenuHost = requireActivity()
              menuHost.addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)
      }
 
      override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        // Add menu items here
        menuInflater.inflate(R.menu.second_menu, menu)
      }
 
      override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        // Handle the menu selection
            return when (menuItem.itemId) {
                R.id.menu_clear -> {
                    // Do stuff...
                    true
                }
                R.id.menu_refresh -> {
                    // Do stuff...
                    true
                }
                else -> false
            }
      }, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
lf3rwulv

lf3rwulv4#

JAVA代码
活动中的选项菜单

addMenuProvider(new MenuProvider() {
            @Override
            public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
                menuInflater.inflate(R.menu.bottom_nav_menu, menu);

                // Add menu options here

            }

            @Override
            public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {

                // Handle Menu Options Selection Here

                return false;
            }
        });

片段中的选项菜单

requireActivity().addMenuProvider(new MenuProvider() {
            @Override
            public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
                menuInflater.inflate(R.menu.bottom_nav_menu, menu);
                
                // Add option Menu Here
                
            }

            @Override
            public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
                return false;
                
                // Handle option Menu Here
                
            }
        }, viewLifecycleOwner, Lifecycle.State.RESUMED);
vybvopom

vybvopom5#

如果您使用的是Jetpack NavigationUI,则需要setSupportActionBar(toolbar),否则菜单将不显示。

m0rkklqb

m0rkklqb6#

这对onCreateView方法有帮助:

requireActivity().addMenuProvider(new MenuProvider() {
        @Override
        public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
            menuInflater.inflate(R.menu.bottom_nav_menu, menu);
            
            // Add option Menu Here
            
        }

        @Override
        public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
                           
            // Handle option Menu Here
             return false;
        }
    }, getViewLifecycleOwner, Lifecycle.State.RESUMED);
dtcbnfnu

dtcbnfnu7#

我不知道为什么所有的答案都建议将Lifecycle.State.RESUME作为生命周期状态传递给addMenuProvider调用,这会使菜单在片段暂停时消失,这在片段暂停但仍然可见时看起来不太好。
例如,点击菜单项后显示对话框将使菜单消失。当对话框关闭时,菜单将重新显示。
在大多数情况下,传入的更好的生命周期状态是Lifecycle.State.CREATE,它只会在视图被销毁时删除菜单,这也是默认行为,所以您可以简单地忽略生命周期状态。

相关问题