Kotlin——jetpackCompose按钮弹出dialog

Kotlin——jetpackCompose按钮弹出dialog

 

package com.example.jetpackcompose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.example.jetpackcompose.ui.theme.JetpackComposeTheme

class MainActivity : ComponentActivity() {
    private lateinit var openDialog: MutableState<Boolean>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JetpackComposeTheme {
                Surface(color = MaterialTheme.colors.background) {
                    openDialog = remember {
                        mutableStateOf(false)
                    }
                    addAlterDialog(openDialog = openDialog)
                    addButton(openDialog = openDialog)

                }
            }
        }
    }
}

@Composable
fun addButton(openDialog: MutableState<Boolean>) {
    Button(onClick = { openDialog.value = true }) {
        Text(text = "点击弹窗")
    }
}

@Composable
fun addAlterDialog(openDialog: MutableState<Boolean>) {
    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = { openDialog.value = false },
            title = { Text(text = "标题") },
            text = {
                Text(
                    text = "文本内容"
                )
            }, confirmButton = {
                TextButton(onClick = { openDialog.value = false }) {
                    Text(text = "确认")
                }
            }, dismissButton = {
                TextButton(onClick = { openDialog.value = false }) {
                    Text(text = "取消")
                }
            })
    }
}

上一篇:yum_1.Nginx


下一篇:使用 555 定时器实现 DC 倍压