[英]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.