Android 实现长按列表item出现菜单-弹窗AlertDialog 上下文菜单ContextMenu的具体使用(附参考代码)

弹窗AlertDialog 上下文菜单ContextMenu这两个结合可以实现长按列表item出现菜单,点击菜单项弹出对话框进行进一步操作。

对话框AlertDialog

AlertDialog是Android中最常用的对话框,可以完成常见的交互操作,如提示、确认、选择等功能。 AlertDialog没有公开的构造函数,必须借助AlertDialog.Builder才能完成参数设置,AlertDialog.Builder的常用方法如下。

  • setIcon:设置标题的图标。 setTitle:设置标题的文本。 setMessage:设置内容的文本。
  • setPositiveButton:设置肯定按钮的信息,包括按钮文本和点击监听器。
  • setNegativeButton:设置否定按钮的信息,包括按钮文本和点击监听器。
  • setNeutralButton:设置中性按钮的信息,包括按钮文本和点击监听器,该方法比较少用。
  • setView:写一个XML文件设置更多样式和控件

通过AlertDialog.Builder设置完参数,还需调用create方法才能生成AlertDialog对象。最后调用
AlertDialog对象的show方法,在页面上弹出提醒对话框

private AlertDialog alertDialog1;


View view1 = View.inflate(root.getContext(),R.layout.activity_alter_dialog_setview, null);
//root.getContext是Fragment中的上下文(View root = inflater.inflate(R.layout.fragment_record, container, false);)


        alertDialog1 = new AlertDialog.Builder(root.getContext())
                .setTitle("修改记录")//标题
                <
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,这里是 Kotlin 代码示例: ```kotlin import androidx.compose.foundation.Text import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.gestures.detectTapOrLongPress import androidx.compose.foundation.gestures.waitForUpOrCancellation import androidx.compose.foundation.layout.Row import androidx.compose.material.AlertDialog import androidx.compose.material.Button import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.getSystemService import android.content.ClipboardManager import android.content.Context import android.content.Intent import android.provider.Settings import android.text.style.ClickableSpan import android.view.ContextMenu import android.view.MenuItem import android.view.View import android.widget.TextView @Composable fun LongPressText( text: String, modifier: Modifier = Modifier ) { val context = LocalContext.current val showDialog = remember { mutableStateOf(false) } val dialogText = remember { mutableStateOf("") } fun onContextMenuClick(item: MenuItem): Boolean { when (item.itemId) { 1 -> { val clipboard = getSystemService(context, ClipboardManager::class.java) val clip = android.content.ClipData.newPlainText("text", dialogText.value) clipboard?.setPrimaryClip(clip) } 2 -> { val sendIntent: Intent = Intent().apply { action = Intent.ACTION_SEND putExtra(Intent.EXTRA_TEXT, dialogText.value) type = "text/plain" } val shareIntent = Intent.createChooser(sendIntent, null) context.startActivity(shareIntent) } } return true } fun showContextMenu(view: View, text: String) { val menu = ContextMenu(context) menu.apply { add(0, 1, 0, "复制") add(0, 2, 0, "分享") setOnMenuItemClickListener { item -> onContextMenuClick(item) } } dialogText.value = text menu.show(view, view.x.toInt(), view.y.toInt()) } val annotatedString = buildAnnotatedString { append(text) addStyle(SpanStyle(color = Color.Blue), 0, text.length) addStringAnnotations(text = text, onClick = { showDialog.value = true dialogText.value = it }, onLongClick = { val textView = TextView(context) textView.text = it textView.setTextColor(ContextCompat.getColor(context, android.R.color.black)) textView.textSize = 18f showContextMenu(textView, it) }) } Text(text = annotatedString, modifier = modifier) if (showDialog.value) { AlertDialog( onDismissRequest = { showDialog.value = false }, title = { Text(text = "闲聊") }, text = { Text(text = dialogText.value) }, confirmButton = { Button(onClick = { showDialog.value = false }) { Text("关闭") } } ) } } fun AnnotatedString.Builder.addStringAnnotations(text: String, onClick: (String) -> Unit, onLongClick: (String) -> Unit) { var startIndex = 0 var spaceIndex = text.indexOf(' ', startIndex) var newestSpace = text.length while (startIndex < text.length) { if (spaceIndex == -1) spaceIndex = text.length val word = text.substring(startIndex, spaceIndex) val clickableSpan = object : ClickableSpan() { override fun onClick(widget: View) { onClick(word) } } val range = startIndex until spaceIndex val color = Color.Blue val style = SpanStyle(color = color, textDecoration = null, background = null, fontWeight = null) addStyle(style, range.start, range.end) addStringAnnotation("Clickable", word, clickableSpan) if (word != text.substring(range.last)) { addStringAnnotation("LongClickable", word, LongClickableSpan(onLongClick)) } newestSpace = spaceIndex startIndex = spaceIndex + 1 spaceIndex = text.indexOf(' ', startIndex) } } class LongClickableSpan(private val onClick: (String) -> Unit) : ClickableSpan() { override fun updateDrawState(ds: TextPaint) { ds.color = ds.linkColor // changes the actual color of the clickable text, default is link blue ds.isUnderlineText = false // removes the underline from the clickable text, default is true } override fun onClick(view: View) { val textView = TextView(view.context) textView.text = (view as TextView).text textView.setTextColor(view.textColors) textView.textSize = view.textSize onClick(view.text.toString()) } } ``` 这个示例代码演示了如何使用 Jetpack Compose 开发 Android 应用程序,并在 Text 组件中实现按文本菜单。这里使用了 AnnotatedString 组件来渲染富文本和交互元素。同时,示例代码还包含了一个 AlertDialog 组件,用于显示与用户的交互过程。请注意,示例代码中的一些实现细节可能需要根据您的特定需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值