Android compose入门学习

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
                ) {
//                    Greeting("Android")
                    var mumber by remember {
                        mutableStateOf(0)
                    }
                    count2(mumber, OnClickValue = { mumber += 1 })
                }
            }
        }
    }
}

@Preview
@Composable
fun Hello() {
    Row() {
        Text(text = "hello gu")
        Text(text = "click me", modifier = Modifier
            .background(Color.Blue)
            .clickable {
                println("--data-- 已经被点击")
            }
            .padding(10.dp))
    }

}

@Preview
@Composable
fun more() {
//    Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
//        for (i in 1..10){
//            item("Compose课程第${i}课,快来学习吧~")
//        }
//    }

    LazyRow(content = {
        item(getData()) {
            repeat(getData().size) {
//              Text(text = "item:${it}")
                item("Compose课程第${it}课,快来学习吧~")
            }
        }
    })
}

@Preview
@Composable
fun more2() {
    LazyColumn(content = {
        item {
            for (i in 1..10) {
                item("Compose课程第${i}课,快来学习吧~")
            }
        }
    })
}

@Preview
@Composable
fun more3() {
    LazyColumn(content = {
        items(items = getData()) { data ->
            item(name = data)
        }
    })
}
@Preview
@Composable
fun more4() {
    LazyColumn(content = {
        items(items = getData()) { data ->
            item2(name = data)
        }
    })
}

fun getData(): List {
    return List(10) { "第${it}课,Compose课程,快来学习吧~" }
}

@Composable
fun item(name: String) {
    Row(
        modifier = Modifier
            .padding(10.dp)
            .background(Color.Blue)
    ) {
        Text(
            text = name, fontSize = 15.sp, color = Color.Black, modifier = Modifier
        )
        Button(onClick = {
            println("查看详情")
        }) {
            Text(text = "查看详情!", color = Color.Gray)
        }
    }
}

@Preview
@Composable
fun item() {
    Row(
        modifier = Modifier
            .padding(10.dp)
            .background(Color.Blue)
    ) {
        Text(
            text = "Compose课程已完善", fontSize = 15.sp, color = Color.Black, modifier = Modifier
                .weight(1f)
                .padding(0.dp, 10.dp, 0.dp, 0.dp)
        )
        Button(onClick = {
            println("查看详情")
        }, modifier = Modifier.weight(1f)) {
            Text(text = "查看详情!", color = Color.Gray)
        }
    }
}

@Preview
@Composable
fun item2() {
    var isvisit by remember {
        mutableStateOf(false)
    }
    Column() {
        Row(
            modifier = Modifier
                .background(Color.Blue)
                .fillMaxWidth()
            , horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(
                text = "Compose课程已完善", fontSize = 15.sp, color = Color.Black
            )
            Button(onClick = {
                isvisit = !isvisit
                println("点击")
            }) {
                Text(text = if (!isvisit) "查看详情!" else "收起", color = Color.Gray)
            }
        }
        if (isvisit) {
            Text(text = "我是详情哈哈哈哈哈", modifier = Modifier.height(100.dp))
        }
    }

}
@Composable
fun item2(name: String) {
    var isvisit by remember {
        mutableStateOf(false)
    }
    Column() {
        Row(
            modifier = Modifier.padding(10.dp)
                .fillMaxWidth().clip(RoundedCornerShape(5.dp)).background(Color.Blue)
                , horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = name, fontSize = 15.sp, color = Color.Black, modifier = Modifier
            )
            Button(onClick = {
                isvisit=!isvisit
                println("查看详情")
            }) {
                Text(text = if (!isvisit) "详情" else "收起", color = Color.Gray)
            }
        }
        if (isvisit){
            Text(text = "我是详情哈哈哈", modifier = Modifier.height(100.dp).padding(10.dp))
        }
    }

}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!", modifier = modifier
    )
}

@Preview
@Composable
fun count() {
    var num by remember {
        mutableStateOf(0)
    }
    Column() {
        Text(text = "item=${num}")
        Button(onClick = { num++ }) {
            Text("add+1")
        }
    }
}

@Composable
fun count2(mumber: Int, OnClickValue: () -> Unit) {
    Column() {
        Text(text = "item=${mumber}")
        Button(onClick = OnClickValue) {
            Text("add+1")
        }
    }
}

你可能感兴趣的:(android基础,android,学习)