android Jetpack撰写onClick {...}吐司通知未显示

s3fp2yjn  于 2022-12-02  发布在  Android
关注(0)|答案(1)|浏览(96)

我试图学习喷气背包组成,这是我正在尝试的代码
我的Activity

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            UserList()
           // MessageCard()

        }
    }
}

我的Data class

data class User(val id: Int)

val users = listOf(User(1), User(2), User(3), User(4), User(5), User(5))

@Composable
fun UserList( ) {
    LazyColumn {
        items(users){
            user ->MessageCard()
        }
    }
}

@Composable
fun MessageCard() {
    val context = LocalContext.current
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
            .clickable {
               Toast.makeText(context, R.string.app_name,Toast.LENGTH_SHORT)
           },
        backgroundColor = MaterialTheme.colors.surface.copy(alpha = .5f)
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {

            Column(horizontalAlignment = Alignment.CenterHorizontally) {
                Text(
                    text = "Title",
                    modifier = Modifier
                        .padding(horizontal = 15.dp)

                        .fillMaxWidth(),
                    style = MaterialTheme.typography.h6,

                    )
                Text(
                    text = "Sub title", modifier = Modifier
                        .padding(horizontal = 15.dp)
                        .fillMaxWidth(),
                    style = MaterialTheme.typography.subtitle1
                )
            }
        }
    }
}

单击MessageCard时未显示吐司消息
任何帮助都是非常感谢的。

atmip9wb

atmip9wb1#

你忘了打电话

Toast.makeText(context, R.string.app_name,Toast.LENGTH_SHORT).show()

相关问题