长按溢出菜单的子菜单项
LongClick on subMenu item of overflow menu
我正在处理 android 应用程序中具有溢出菜单的项目。在 onCreateOptionsMenu() 中,我根据用户详细信息动态更新菜单和子菜单,但任务要求我对该菜单按钮进行 2 次操作,我已经通过 [=22 进行了一次单击=]onOptionsItemSelected()
是否可以将类似 OnLongClickListener 的内容添加到溢出菜单的子菜单项中?
在我的菜单中XML我这样设置subItem
<item
android:id="@+id/help"
android:orderInCategory="101"
android:title="Help"
app:actionViewClass="android.widget.ImageButton"
app:showAsAction="never" >
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/event1"
android:title="Event 1"
app:actionViewClass="android.widget.TextView"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/event2"
android:title="Event 2"
app:actionViewClass="android.widget.TextView"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/event3"
android:title="Event 3"
app:actionViewClass="android.widget.TextView"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
然后在 onCreateOptionsMenu 中我像这样附加 longClick
var helpMenuItem = menu.findItem(R.id.help)
helpMenuItem.subMenu?.let { subMenu ->
val ct = subMenu.size()
for (i in 0 until ct) {
val subItem = subMenu.getItem(i)
subItem.actionView?.let { view ->
view.setOnLongClickListener {
Log.d("Long Click Event", "I'm here!!")
true
}
}
}
}
代码可以编译,但我始终无法获得 onLongClick。求助这几天一直在找解决方案wo luck
所以没有人回答,但我需要完成它,所以我继续挖掘。这是我想出的解决方法。我没有保留子菜单的工具栏菜单,而是创建了一个新对话框,在那里我覆盖并添加了我的 onClick 和 LongClick。这对我有用,没有别的。
private fun displayPopupDialog() {
val context = applicationContext
val buds = myArray.toTypedArray<CharSequence>()
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle("Select a Buddy")
builder.setItems(buds, object : DialogInterface.OnClickListener {
override fun onClick(dialogInterface: DialogInterface?, item: Int) {
// showShortToast("Clicked on:"+buddy[item]);
val ptcode = buds[item].toString()
clickFromDialog("code: $ptcode")
return
}
})
val ad = builder.create() //don't show dialog yet
ad.setOnShowListener(object : DialogInterface.OnShowListener {
override fun onShow(dialog: DialogInterface?) {
val lv = ad.listView //this is a ListView with your "buds" in it
lv.setOnItemLongClickListener(object : AdapterView.OnItemLongClickListener {
override fun onItemLongClick(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
): Boolean {
var textOnView = (view as MaterialTextView).text
Log.d("Long Click!", "List Item #" + position + " $textOnView was long clicked")
return true
}
})
}
})
val window: Window? = ad.getWindow()
val wlp: LayoutParams? = window?.getAttributes()
wlp?.gravity = Gravity.RIGHT or Gravity.TOP
window?.setAttributes(wlp)
ad.show()
}
private fun clickFromDialog(ptcode: String) {
Log.d("Click!", "List Item $ptcode was clicked")
}
我正在处理 android 应用程序中具有溢出菜单的项目。在 onCreateOptionsMenu() 中,我根据用户详细信息动态更新菜单和子菜单,但任务要求我对该菜单按钮进行 2 次操作,我已经通过 [=22 进行了一次单击=]onOptionsItemSelected()
是否可以将类似 OnLongClickListener 的内容添加到溢出菜单的子菜单项中?
在我的菜单中XML我这样设置subItem
<item
android:id="@+id/help"
android:orderInCategory="101"
android:title="Help"
app:actionViewClass="android.widget.ImageButton"
app:showAsAction="never" >
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/event1"
android:title="Event 1"
app:actionViewClass="android.widget.TextView"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/event2"
android:title="Event 2"
app:actionViewClass="android.widget.TextView"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="@+id/event3"
android:title="Event 3"
app:actionViewClass="android.widget.TextView"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
然后在 onCreateOptionsMenu 中我像这样附加 longClick
var helpMenuItem = menu.findItem(R.id.help)
helpMenuItem.subMenu?.let { subMenu ->
val ct = subMenu.size()
for (i in 0 until ct) {
val subItem = subMenu.getItem(i)
subItem.actionView?.let { view ->
view.setOnLongClickListener {
Log.d("Long Click Event", "I'm here!!")
true
}
}
}
}
代码可以编译,但我始终无法获得 onLongClick。求助这几天一直在找解决方案wo luck
所以没有人回答,但我需要完成它,所以我继续挖掘。这是我想出的解决方法。我没有保留子菜单的工具栏菜单,而是创建了一个新对话框,在那里我覆盖并添加了我的 onClick 和 LongClick。这对我有用,没有别的。
private fun displayPopupDialog() {
val context = applicationContext
val buds = myArray.toTypedArray<CharSequence>()
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle("Select a Buddy")
builder.setItems(buds, object : DialogInterface.OnClickListener {
override fun onClick(dialogInterface: DialogInterface?, item: Int) {
// showShortToast("Clicked on:"+buddy[item]);
val ptcode = buds[item].toString()
clickFromDialog("code: $ptcode")
return
}
})
val ad = builder.create() //don't show dialog yet
ad.setOnShowListener(object : DialogInterface.OnShowListener {
override fun onShow(dialog: DialogInterface?) {
val lv = ad.listView //this is a ListView with your "buds" in it
lv.setOnItemLongClickListener(object : AdapterView.OnItemLongClickListener {
override fun onItemLongClick(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
): Boolean {
var textOnView = (view as MaterialTextView).text
Log.d("Long Click!", "List Item #" + position + " $textOnView was long clicked")
return true
}
})
}
})
val window: Window? = ad.getWindow()
val wlp: LayoutParams? = window?.getAttributes()
wlp?.gravity = Gravity.RIGHT or Gravity.TOP
window?.setAttributes(wlp)
ad.show()
}
private fun clickFromDialog(ptcode: String) {
Log.d("Click!", "List Item $ptcode was clicked")
}