How to create custom Popup Menu using Kotlin in AndroidStudio?

折月煮酒 提交于 2020-05-12 12:01:08

问题


I am trying to create popup menu similarly like this on click on a button view in Android using Koltin. I searched for SOF and Google didn't find any suggestions. Can anyone provide a sample code to achieve it using kotlin.


回答1:


Finally, I did it, It may help someone

Step 1. First, create an activity_main.xml contains a button named my_button

Step 2. Then take header_menu.xml under res/menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/header1"
        android:title="H1" />
    <item
        android:id="@+id/header2"
        android:title="H2" />
    <item
        android:id="@+id/header3"
        android:title="H3" />

</menu>

Step 3. Finally, in MainActivity.kt use it like:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val clickListener = View.OnClickListener { view ->
            when (view.id) {
                R.id.my_button -> {
                    showPopup(view)
                }
            }
        }

        my_button.setOnClickListener(clickListener)
    }

    private fun showPopup(view: View) {
        var popup: PopupMenu? = null;
        popup = PopupMenu(this, view)
        popup.inflate(R.menu.header_menu)

        popup.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item: MenuItem? ->

            when (item!!.itemId) {
                R.id.header1 -> {
                    Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
                }
                R.id.header2 -> {
                    Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
                }
                R.id.header3 -> {
                    Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
                }
            }

            true
        })

        popup.show()
    }



回答2:


Following Kotlin's nice and clean principle: You can do this as well:

1)in your .xml file: (but "onClick" in xml can be used while doing java as well)

<Button 
  ........
  android:onClick="showPopUp"
  ....../>

2)in your .kt file: (using kotlin's lambda expression)

fun showPopUp(view: View) {
    val popupMenu = PopupMenu(this, view)
    val inflater = popupMenu.menuInflater
    inflater.inflate(R.menu.header_menu, popupMenu.menu)
    popupMenu.show()

    popupMenu.setOnMenuItemClickListener {
        when(it.itemId) {
            R.id.header1 -> {
                Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
            }
            R.id.header2 -> {
                Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
            }
            R.id.header3 -> {
                Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
            }
            ...........
        }
        true
    }
}


来源:https://stackoverflow.com/questions/48626995/how-to-create-custom-popup-menu-using-kotlin-in-androidstudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!