Jetpack Compose学习 -------- Checkbox 复选框

Checkbox 复选框 基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14

@Composable
fun CheckBoxDemo() {
val checkedState = remember {
mutableStateOf(true)
}
Checkbox(
checked = checkedState.value,
onCheckedChange = {
checkedState.value = it
}
)
}

Jetpack Compose学习 -------- Card

Card 卡片 简单使用

1
2
3
4
5
6
7
8
9
10
@Composable
fun CardDemo() {
Card(
modifier = Modifier.fillMaxWidth() ,
backgroundColor = Color.White,
elevation = 10.dp,
) {
Text(text = "This is a Card Demo",Modifier.padding(10.dp))
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

@Composable
fun ComposableSample() {
Card(
modifier = Modifier
.size(200.dp)
.padding(10.dp),
//形状 圆角矩形
shape = RoundedCornerShape(8.dp),
//背景颜色
backgroundColor = Color.White,
//内容的颜色
contentColor = Color.Black,
//边框
border = BorderStroke(0.5.dp, Color.Gray),
//阴影
elevation = 10.dp
) {
Column() {
Image(
painter = painterResource(id = R.mipmap.icon_header),
contentDescription = "图片描述",
contentScale = ContentScale.FillBounds
)
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@Composable
fun CardDemo() {
Card(
modifier = Modifier
.fillMaxWidth()
// 外边距
.padding(15.dp)
.clickable(
// 设置点击波纹效果,注意如果 CardDemo() 函数不在 ExamplesTheme() {} 下调用
// 将无法显示波纹效果
onClick = {
Log.d("Clickable", " clicked.")
}),
backgroundColor = Color.White,
// 设置阴影
elevation = 10.dp,
)
{
Column(
// 内边距
Modifier.padding(15.dp)
) {
Text(buildAnnotatedString {
append("welcome to ")
withStyle(
style = SpanStyle(
color = Color(0xFF4552B8),
fontWeight = FontWeight.W900,
)
) {
append("Jetpack Compose Playground")
}
})
Text(buildAnnotatedString {
append("Now you are in the ")
withStyle(
style = SpanStyle(
fontWeight = FontWeight.W900,
)
) {
append("Card")
}
append(" section")
})
}
}
}

Jetpack Compose学习 -------- Canvas 图形

Canvas 图形 基本属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

//绘制 线
@Composable
fun CanvasDrawLineDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
val canvasWidth = size.width
val canvasHeight = size.height

//绘制 线
drawLine(
start = Offset(x = canvasWidth, y = 0f),
end = Offset(x = 0f, y = canvasHeight),
color = Color.Red
)
}
}

//绘制 圆
@Composable
fun CanvasDrawCircleDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
val canvasWidth = size.width
val canvasHeight = size.height

//绘制 圆
drawCircle(
color = Color.Red,
//圆心 画布中心点
center = Offset(
x = canvasWidth / 2,
y = canvasHeight / 2
),
//半径 : 画布最小边 的 1/4
radius = size.minDimension / 4
)
}
}

//绘制 弧
@Composable
fun CanvasDrawArcDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
//绘制 弧
drawArc(
color = Color.Red,
startAngle = 0f,
sweepAngle = 60f,
useCenter = true,
size = Size(300f, 300f),
topLeft = Offset(60f, 60f)
)
}
}

//绘制 矩形
@Composable
fun CanvasDrawRectDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
val canvasQuadrantSize = size / 2f

//绘制 矩形
drawRect(
color = Color.Red,
size = canvasQuadrantSize
)
}
}


//绘制 矩形
@Composable
fun CanvasInsetDrawRectDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
val canvasQuadrantSize = size / 2f
//更改绘图边界
inset(150f, 30f) {
//绘制 矩形
drawRect(
color = Color.Red,
size = canvasQuadrantSize
)
}
}
}

//绘制 矩形
@Composable
fun CanvasDrawRectTopLeftDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
val canvasWidth = size.width
val canvasHeight = size.height
val canvasSize = size
//绘制 矩形
drawRect(
color = Color.Red,
topLeft = Offset(x = canvasWidth / 3f, y = canvasHeight / 3f),
size = canvasSize / 3f
)
}
}

//绘制 矩形
@Composable
fun CanvasRotateDrawRectDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
//旋转 45 度
rotate(degrees = 45f) {
val canvasWidth = size.width
val canvasHeight = size.height
val canvasSize = size
//绘制 矩形
drawRect(
color = Color.Red,
topLeft = Offset(x = canvasWidth / 3f, y = canvasHeight / 3f),
size = canvasSize / 3f
)
}
}
}

//绘制 矩形
@Composable
fun CanvasTransformDrawRectDemo() {
//画布
Canvas(
//充满整个屏幕
modifier = Modifier.fillMaxSize()
) {
val canvasWidth = size.width
val canvasHeight = size.height
val canvasSize = size
//多种转换
withTransform({
//平移
translate(left = canvasWidth / 5f)
//旋转 45 度
rotate(degrees = 45f)
}) {
//绘制 矩形
drawRect(
color = Color.Red,
topLeft = Offset(x = canvasWidth / 3f, y = canvasHeight / 3f),
size = canvasSize / 3f
)

}
}
}

Jetpack Compose学习 -------- 按钮 Button , IconButton

Button 按钮 基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14

@Composable
fun ButtonSample() {
Button(
onClick = {
Log.d("ButtonSample", "按钮被点击")
},
modifier = Modifier.padding(10.dp),
colors = ButtonDefaults.textButtonColors(backgroundColor = Color.Blue)
) {
Text(text = "按钮", color = Color.White)
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

@Composable
fun ComposableSample() {
Button(onClick = { }) {
Icon(
imageVector = Icons.Filled.Favorite,
contentDescription = "喜爱",
modifier = Modifier.size(ButtonDefaults.IconSize)
)
//间隔
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
Text(text = "喜爱")
}
}

IconButton 图标按钮 基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

@Composable
fun ComposableSample() {
Row {
IconButton(onClick = { }) {
Icon(Icons.Filled.Search, null)
}

IconButton(onClick = { }) {
Icon(Icons.Filled.ArrowBack, null)
}

IconButton(onClick = { }) {
Icon(Icons.Filled.Done, null)
}
val isSelected = remember {
mutableStateOf(false)
}
//取消水波纹
IconButtonNoIndication(onClick = {
isSelected.value = !isSelected.value
}) {
Icon(
Icons.Filled.Home,
null,
tint = if (isSelected.value) {
Color.Red
} else {
Color.Gray
}
)
}
}
}


/**
* 去掉水波纹
*/
@Composable
fun IconButtonNoIndication(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
) {
Box(
modifier = modifier
.clickable(
onClick = onClick,
enabled = enabled,
role = Role.Button,
interactionSource = interactionSource,
//取消水波纹
indication = null
)
.then(Modifier.size(48.dp)),
contentAlignment = Alignment.Center
) { content() }
}

Jetpack Compose学习 -------- AlertDialog

AlertDialog 提示框 简单使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

@Composable
fun AlertDialogSample() {
MaterialTheme {
Column(
Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
) {
val openDialog = remember {
mutableStateOf(false)
}
Button(onClick = {
openDialog.value = true
}) {
Text(text = "弹出提示框")
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// 当用户点击对话框以外的地方或者按下系统返回键将会执行的代码
openDialog.value = false
},
title = {
Text(text = "提示框标题")
},
text = {
Text(text = "提示框内容")
},
confirmButton = {
Button(onClick = { openDialog.value = false }) {
Text(text = "确认按钮")
}
},
dismissButton = {
Button(onClick = { openDialog.value = false }) {
Text(text = "取消按钮")
}
})
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

@Composable
fun AlertDialogSample2() {
MaterialTheme {
Column(
Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
) {
val openDialog = remember {
mutableStateOf(false)
}
Button(onClick = {
openDialog.value = true
}) {
Text(text = "弹出提示框")
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// 当用户点击对话框以外的地方或者按下系统返回键将会执行的代码
openDialog.value = false
},
title = {
Text(
text = "开启位置服务",
fontWeight = FontWeight.W700,
style = MaterialTheme.typography.h6
)
},
text = {
Text(
text = "这将意味着,我们会给您提供精准的位置服务,并且您将接受关于您订阅的位置信息",
fontSize = 16.sp
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
},
) {
Text(
"确认",
fontWeight = FontWeight.W700,
style = MaterialTheme.typography.button
)
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text(
"取消",
fontWeight = FontWeight.W700,
style = MaterialTheme.typography.button
)
}
})
}
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

@Composable
fun AlertDialogSample3() {
MaterialTheme {
Column(
Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
) {
val openDialog = remember {
mutableStateOf(false)
}
Button(onClick = {
openDialog.value = true
}) {
Text(text = "弹出提示框")
}
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
openDialog.value = false
},
title = {
Text(
text = "开启位置服务",
fontWeight = FontWeight.W700,
style = MaterialTheme.typography.h6
)
},
text = {
Text(
text = "这将意味着,我们会给您提供精准的位置服务,并且您将接受关于您订阅的位置信息",
fontSize = 16.sp
)
},
buttons = {
Row(
modifier = Modifier.padding(all = 8.dp),
horizontalArrangement = Arrangement.Center
) {
Button(
modifier = Modifier.fillMaxWidth(),
onClick = { openDialog.value = false }
) {
Text("必须接受!")
}
}
}
)
}
}
}
}