简体   繁体   English

我们如何在 android jetpack compose 中使用行项目的意图?

[英]How we can use intent for row items in android jetpack compose?

I start to learning jetpack compose in Android jetpack compose, I try to use intent for text menu items, I was saw many example for button on internet and in some books, but I want to use it for text row, I try to use below example, but app crashed, I do not know what I missed?我开始在 Android jetpack compose 中学习 jetpack compose,我尝试将意图用于文本菜单项,我在互联网和一些书籍中看到了很多按钮示例,但我想将它用于文本行,我尝试在下面使用例如,但应用程序崩溃了,我不知道我错过了什么?

class MyActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyScreen()
        }
    }

}
@Composable
fun MyScreen(

) {

    val context = LocalContext.current


            Column(
                modifier = Modifier.fillMaxSize()
            ) {

               MainRow(

                   name = "Menu1",
                
                   context.startActivity(Intent(context, Menu1Activity::class.java))

               )

                MainRow(
                    name = "Menu2",
            
                    context.startActivity(Intent(context, Menu2Activity::class.java))
                )
          }
        }
@Composable
fun MainRow(
    name: String,
  
    startActivity: Unit

) {

        Row(
            modifier = Modifier
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {

           

            Text(
                text = name,
                style = TextStyle(
                    fontSize = 16.sp,
                    color = Color.Gray
                ),
            )

        }
    }
@Composable
fun MainRow(
    name: String,  
    startActivity: **() -> Unit**
) {
        Row(
            modifier = Modifier**.clickable { startActivity() }**
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = name,
                style = TextStyle(
                    fontSize = 16.sp,
                    color = Color.Gray
                ),
            )
        }
    }

And call it like this:并这样称呼它:

MainRow(name = "Menu2", 
startActivity = **{**context.startActivity(Intent(context, Menu2Activity::class.java))**}**)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在jetpack compose中为每行使用切换按钮? - How we can use toggle button for per row in jetpack compose? 我们如何在jetpack compose中为不同的图标使用可点击属性? - How we can use clickable property for different icons with row in jetpack compose? 如何使用 Android Jetpack Compose 在 Row 上布局所有项目 - How to layout all items on Row using Android Jetpack Compose 如何在 android jetpack compose Row 中自定义左/右对齐项目 - How to custom left/right align items in android jetpack compose Row 我们如何在 jetpack compose 中使用颜色网格视图? - How we can use color grid view in jetpack compose? 我们如何在 jetpack compose 中使用底板? - How we can use bottom sheet in jetpack compose? 我们如何在 Jetpack Compose 的底部表单中使用 onLongClick 侦听器? - How we can use onLongClick listener for bottom sheet in jetpack compose? 如何使用 trailingIcon 将焦点同步到 OutlinedTextField 上? - Android Jetpack Compose - How can sync focuse on OutlinedTextField with use trailingIcon ? - Android Jetpack Compose 我们如何在 Android Jetpack Compose UI 中将文本格式化为上标或下标? - How we can we format Text as superscript or subscript in Android Jetpack Compose UI? Android Jetpack Compose:所有行项目未在较小的屏幕上呈现 - Android Jetpack Compose: All row items not rendering on smaller screens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM