kotlin示例

以下代码是我写的练习程序,更好的代码可以从这里查看:代码

生日卡片

在这里插入图片描述

package com.example.happybirthdayimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.happybirthday.ui.theme.HappyBirthdayThemeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {HappyBirthdayTheme {// A surface container using the 'background' color from the themeSurface(modifier = Modifier.fillMaxSize(),color = MaterialTheme.colorScheme.background) {GreetingImage(message = stringResource(R.string.happy_birthday_sam),from = stringResource(R.string.signature_text))}}}}
}@Composable
fun GreetingText(message: String,from: String,modifier: Modifier = Modifier) {Column (verticalArrangement = Arrangement.Center, modifier = modifier){Text(text = message,fontSize = 100.sp,lineHeight = 116.sp,textAlign = TextAlign.Center)Text(text = from,fontSize = 36.sp,modifier = Modifier.padding(16.dp).align(alignment = Alignment.CenterHorizontally))}
}@Composable
fun GreetingImage(message: String, from: String, modifier: Modifier = Modifier) {val image = painterResource(R.drawable.androidparty)Box(modifier) {Image(painter = image,contentDescription = null,contentScale = ContentScale.Crop,alpha = 0.5F)GreetingText(message = message,from = from,modifier = Modifier.fillMaxSize().padding(8.dp))}
}@Preview(showBackground = true)
@Composable
fun BirthdayCardPreview() {HappyBirthdayTheme {GreetingImage(message = stringResource(R.string.happy_birthday_sam),from = stringResource(R.string.signature_text))}
}

strings.xml

<resources><string name="app_name">Happy Birthday</string><string name="happy_birthday_sam">Happy Birthday Sam!</string><string name="signature_text">From Emma</string>
</resources>

Compose 文章

在这里插入图片描述

package com.example.composearticleimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composearticle.ui.theme.ComposeArticleThemeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {ComposeArticleTheme {Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->Greeting(name = "Android",modifier = Modifier.padding(innerPadding))}}}}
}@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {val image = painterResource(R.drawable.bg_compose_background)Column {Image(painter = image,contentDescription = null,contentScale = ContentScale.FillWidth)Text(text = "Jetpack Compose tutorial",fontSize = 24.sp,modifier = Modifier.padding(16.dp))Text(text = "Jetpack Compose is a modern toolkit for building native Android UI. Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.",modifier = Modifier.padding(start = 16.dp,end=16.dp))Text(text = "In this tutorial, you build a simple UI component with declarative functions. You call Compose functions to say what elements you want and the Compose compiler does the rest. Compose is built around Composable functions. These functions let you define your app\\'s UI programmatically because they let you describe how it should look and provide data dependencies, rather than focus on the process of the UI\\'s construction, such as initializing an element and then attaching it to a parent. To create a Composable function, you add the @Composable annotation to the function name.",modifier = Modifier.padding(16.dp))}
}@Preview(showBackground = true)
@Composable
fun GreetingPreview() {ComposeArticleTheme {Greeting("Android")}
}

TaskCompleted

在这里插入图片描述

package com.example.taskcompletedimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.taskcompleted.ui.theme.TaskCompletedThemeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {TaskCompletedTheme {Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->Greeting(name = "Android",modifier = Modifier.padding(innerPadding))}}}}
}@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {val image = painterResource(R.drawable.ic_task_completed)Column(verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally,modifier = Modifier.fillMaxWidth().fillMaxHeight(),) {Image(painter = image,contentDescription = null)Text(text = "All tasks completed",modifier = Modifier.padding(top = 24.dp, bottom = 8.dp),fontWeight = FontWeight.Bold)Text(text = "Nice work!",fontSize = 16.sp)}
}@Preview(showBackground = true)
@Composable
fun GreetingPreview() {TaskCompletedTheme {Greeting("Android")}
}

ComposeQuadrant

在这里插入图片描述

package com.example.composequadrantimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composequadrant.ui.theme.ComposeQuadrantThemeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {ComposeQuadrantTheme {Surface(modifier = Modifier.fillMaxSize(),color = MaterialTheme.colorScheme.background){ComposeQuadrantApp();}}}}
}@Composable
fun ComposeQuadrantApp() {Column(Modifier.fillMaxWidth()) {Row (Modifier.weight(1f)){ComposableInfoCard(title = stringResource(R.string.first_title),description = stringResource(R.string.first_description),backgroundColor = Color(0xFFEADDFF),modifier = Modifier.weight(1f))ComposableInfoCard(title = stringResource(R.string.second_title),description = stringResource(R.string.second_description),backgroundColor = Color(0xFFD0BCFF),modifier = Modifier.weight(1f))}Row(Modifier.weight(1f)) {ComposableInfoCard(title = stringResource(R.string.third_title),description = stringResource(R.string.third_description),backgroundColor = Color(0xFFB69DF8),modifier = Modifier.weight(1f))ComposableInfoCard(title = stringResource(R.string.fourth_title),description = stringResource(R.string.fourth_description),backgroundColor = Color(0xFFF6EDFF),modifier = Modifier.weight(1f))}}
}@Composable
private fun ComposableInfoCard(title: String,description:String,backgroundColor: Color,modifier: Modifier= Modifier){Column(modifier = modifier.fillMaxSize().background(backgroundColor).padding(16.dp),verticalArrangement = Arrangement.Center,horizontalAlignment = Alignment.CenterHorizontally) {Text(text = title,modifier = Modifier.padding(bottom = 16.dp),fontWeight = FontWeight.Bold)Text(text = description,textAlign = TextAlign.Justify)}
}@Preview(showBackground = true)
@Composable
fun ComposeQuadrantAppPreview() {ComposeQuadrantTheme {ComposeQuadrantApp()}
}

名片应用

在这里插入图片描述

package com.example.businesscardimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.businesscard.ui.theme.BusinessCardTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Call
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.Iconclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {BusinessCardTheme {Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->Greeting(name = "Android",modifier = Modifier.padding(innerPadding))}}}}
}@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {Column(Modifier.fillMaxWidth().background(color =  Color(0xFFCBE4CF))) {Column(modifier = Modifier.weight(5f).fillMaxWidth(),horizontalAlignment = Alignment.CenterHorizontally,verticalArrangement = Arrangement.Center,) {val image = painterResource(R.drawable.android_logo)Box(modifier = Modifier.background(Color.Black).width(width = 100.dp).height(height = 100.dp)){Image(painter = image,contentDescription = null,modifier = Modifier.width(width = 100.dp))}Text(text = "Jennifer Doe",modifier = Modifier.padding(top = 24.dp, bottom = 8.dp),fontSize = 48.sp)Text(text = "Android Developer Extraordinaire",fontSize = 16.sp,color = Color(0xFF036339),fontWeight = FontWeight.Bold)}Column(modifier = Modifier.weight(2f).fillMaxSize() // 填充父容器大小.wrapContentSize(Alignment.Center) // 在父容器中居中) {Row {Icon(imageVector = Icons.Filled.Call, contentDescription = "Call", tint = Color(0xFF006633), modifier = Modifier.padding(end = 10.dp))Text(text = "+11(123)444 5555 666",textAlign = TextAlign.Justify)}Row {Icon(imageVector = Icons.Filled.Share, contentDescription = "Share", tint = Color(0xFF006633),modifier = Modifier.padding(end = 10.dp))Text(text = "@AndroidDev")}Row {Icon(imageVector = Icons.Filled.Email, contentDescription = "Email", tint = Color(0xFF006633),modifier = Modifier.padding(end = 10.dp))Text(text = "jen.joe@android.com")}}}}@Preview(showBackground = true)
@Composable
fun GreetingPreview() {BusinessCardTheme {Greeting("Android")}
}

创建交互式 Dice Roller 应用

在这里插入图片描述

package com.example.dicerollerimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.diceroller.ui.theme.DiceRollerTheme
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValueclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)enableEdgeToEdge()setContent {DiceRollerTheme {DiceRollerApp()}}}
}@Preview
@Composable
fun DiceRollerApp(){DiceWithButtonAndImage(modifier = Modifier.fillMaxSize().wrapContentSize(Alignment.Center))
}@Composable
fun DiceWithButtonAndImage(modifier: Modifier = Modifier){var result by remember { mutableStateOf(1) }val imageResource = when (result) {1 -> R.drawable.dice_12 -> R.drawable.dice_23 -> R.drawable.dice_34 -> R.drawable.dice_45 -> R.drawable.dice_5else -> R.drawable.dice_6}Column (modifier = modifier,horizontalAlignment = Alignment.CenterHorizontally) {Image(painter = painterResource(imageResource), contentDescription = result.toString())Spacer(modifier = Modifier.height(16.dp))Button(onClick = { result = (1..6).random() }) {Text(stringResource(R.string.roll))}}
}

总结

  • 定义可组合函数。
  • 使用组合创建布局。
  • 使用 Button 可组合函数创建按钮。
  • 导入 drawable 资源。
  • 使用 Image 可组合函数显示图片。
  • 使用可组合函数构建交互式界面。
  • 使用 remember 可组合函数将组合中的对象存储到内存中。
  • 使用 mutableStateOf() 函数刷新界面以创建可观察对象。

小费计算器

在这里插入图片描述

package com.example.calculatorimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.calculator.ui.theme.CalculatorTheme
import java.text.NumberFormat
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.text.input.KeyboardTypeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {enableEdgeToEdge()super.onCreate(savedInstanceState)setContent {CalculatorTheme {Surface(modifier = Modifier.fillMaxSize(),) {TipTimeLayout()}}}}
}@Composable
fun TipTimeLayout() {var amountInput by remember { mutableStateOf("") }val amount = amountInput.toDoubleOrNull() ?: 0.0val tip = calculateTip(amount)Column(modifier = Modifier.statusBarsPadding().padding(horizontal = 40.dp).safeDrawingPadding(),horizontalAlignment = Alignment.CenterHorizontally,verticalArrangement = Arrangement.Center) {Text(text = stringResource(R.string.calculate_tip),modifier = Modifier.padding(bottom = 16.dp, top = 40.dp).align(alignment = Alignment.Start))EditNumberField(value = amountInput,onValueChange = { amountInput = it },modifier = Modifier.padding(bottom = 32.dp).fillMaxWidth())Text(text = stringResource(R.string.tip_amount, tip),style = MaterialTheme.typography.displaySmall)Spacer(modifier = Modifier.height(150.dp))}
}/*** Calculates the tip based on the user input and format the tip amount* according to the local currency.* Example would be "$10.00".*/
private fun calculateTip(amount: Double, tipPercent: Double = 15.0): String {val tip = tipPercent / 100 * amountreturn NumberFormat.getCurrencyInstance().format(tip)
}@Composable
fun EditNumberField(value: String,onValueChange: (String) -> Unit,modifier: Modifier = Modifier
) {TextField(value = value,onValueChange = onValueChange,modifier = modifier,label = { Text(stringResource(R.string.bill_amount)) },singleLine = true,keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}@Preview(showBackground = true)
@Composable
fun TipTimeLayoutPreview() {CalculatorTheme {TipTimeLayout()}
}

小费计算器2

在这里插入图片描述

package com.example.calculatorimport android.os.Bundle
import androidx.compose.material3.Switch
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.calculator.ui.theme.CalculatorTheme
import java.text.NumberFormat
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardTypeclass MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {enableEdgeToEdge()super.onCreate(savedInstanceState)setContent {CalculatorTheme {Surface(modifier = Modifier.fillMaxSize(),) {TipTimeLayout()}}}}
}@Composable
fun TipTimeLayout() {var roundUp by remember { mutableStateOf(false) }var amountInput by remember { mutableStateOf("") }val amount = amountInput.toDoubleOrNull() ?: 0.0var tipInput by remember { mutableStateOf("") }val tipPercent = tipInput.toDoubleOrNull() ?: 0.0val tip = calculateTip(amount,tipPercent,roundUp)Column(modifier = Modifier.statusBarsPadding().padding(horizontal = 40.dp).verticalScroll(rememberScrollState()).safeDrawingPadding(),horizontalAlignment = Alignment.CenterHorizontally,verticalArrangement = Arrangement.Center) {Text(text = stringResource(R.string.calculate_tip),modifier = Modifier.padding(bottom = 16.dp, top = 40.dp).align(alignment = Alignment.Start))EditNumberField(label = R.string.how_was_the_service,leadingIcon = R.drawable.money,keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number,imeAction = ImeAction.Next),value = amountInput,onValueChange = { amountInput = it },modifier = Modifier.padding(bottom = 32.dp).fillMaxWidth())EditNumberField(label = R.string.how_was_the_service,leadingIcon = R.drawable.percent,keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number,imeAction = ImeAction.Done),value = tipInput,onValueChange = { tipInput = it },modifier = Modifier.padding(bottom = 32.dp).fillMaxWidth())RoundTheTipRow(roundUp = roundUp,onRoundUpChanged = { roundUp = it },modifier = Modifier.padding(bottom = 32.dp))Text(text = stringResource(R.string.tip_amount, tip),style = MaterialTheme.typography.displaySmall)Spacer(modifier = Modifier.height(150.dp))}
}/*** Calculates the tip based on the user input and format the tip amount* according to the local currency.* Example would be "$10.00".*/
private fun calculateTip(amount: Double,tipPercent: Double = 15.0,roundUp: Boolean): String {var tip = tipPercent / 100 * amountif (roundUp) {tip = kotlin.math.ceil(tip)}return NumberFormat.getCurrencyInstance().format(tip)
}@Composable
fun EditNumberField(@StringRes label: Int,@DrawableRes leadingIcon: Int,keyboardOptions: KeyboardOptions,value: String,onValueChange: (String) -> Unit,modifier: Modifier = Modifier
) {TextField(value = value,leadingIcon = { Icon(painter = painterResource(id = leadingIcon), null) },onValueChange = onValueChange,modifier = modifier,label = { Text(stringResource(label)) },singleLine = true,keyboardOptions = keyboardOptions)
}@Composable
fun RoundTheTipRow(roundUp: Boolean,onRoundUpChanged: (Boolean) -> Unit,modifier: Modifier = Modifier
) {Row(modifier = modifier.fillMaxWidth().size(48.dp),verticalAlignment = Alignment.CenterVertically) {Text(text = stringResource(R.string.round_up_tip))Switch(modifier = modifier.fillMaxWidth().wrapContentWidth(Alignment.End),checked = roundUp,onCheckedChange = onRoundUpChanged,)}
}@Preview(showBackground = true)
@Composable
fun TipTimeLayoutPreview() {CalculatorTheme {TipTimeLayout()}
}

代码下载

$ git clone https://github.com/google-developer-training/basic-android-kotlin-compose-training-tip-calculator.git

参考文档

https://developer.android.com/reference

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3267613.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

使用echo写入多行文字到文件时换行的处理

目标 想使用echo写入如下内容到文件program.c里 #include<stdio.h> int main(){printf("hello!\n"); } 需要处理 1、如何处理行换 2、代码中的换行如何处理 实际例子 创建文件夹 mkdir test cd test chmod 777 . 创建文件写入内容 查看 cat -n program.c…

Flink入门(更新中)

目录 一、Flink 1.1 基本概念 1.1.1 flink简介 1.2 flink编程模版 1.3 常用概念 1.2.1 datastream 1.2.2 算子、Task 1.2.3 多流操作 1.2.6 时间语义 二、Flink编程实战(Java) 2.1 wordcount 一、Flink 1.1 基本概念 1.1.1 flink简介 1.图片介绍 性能&#xff1a…

[练习]如何使用递归算法?

&#x1f3a5; 个人主页&#xff1a;Dikz12&#x1f525;个人专栏&#xff1a;算法(Java)&#x1f4d5;格言&#xff1a;吾愚多不敏&#xff0c;而愿加学欢迎大家&#x1f44d;点赞✍评论⭐收藏 目录 1. 递归概述 2.汉诺塔问题 题目描述​编辑 题解 代码实现 3…

package.json中对peerDependencies的理解

peerDependencies只要是用来限制依赖的&#xff0c;最近在开发的时候有遇到这样的问题&#xff0c;所以研究了一下 "peerDependencies": {"vue/composition-api": "^1.0.5","vue/runtime-core": "^3.0.0","echarts&q…

【Android】Activity生命周期与五种启动模式

文章目录 生命周期返回栈Activity状态生命周期方法 启动模式standard模式singleTask模式singleTop模式singleInstance模式singleInstancePerTask模式配置方式 生命周期 返回栈 每个Activity的状态由它在Activity栈&#xff08;又叫“回退栈back stack”&#xff09;中的位置决…

【ACM出版】2024年教育人工智能国际学术会议(ISAIE 2024,9月6-8)

2024年教育人工智能国际学术会议&#xff08;ISAIE 2024&#xff09;将于2024年9月6-8日在中国西安举行。本届会议由西京学院主办。 会议主要围绕人工智能在教育领域的最新研究成果展开&#xff0c;为来自国内外高等院校、科学研究所、企事业单位的专家、教授、学者、工程师等提…

windows安装redis设置密码、修改端口、提供外部访问

windows安装redis设置密码、修改端口、提供外部访问 一、前言1. 设置密码2. 修改端口3. 允许外部访问4. 注意事项 一、前言 设置Redis在Windows上设置密码、修改端口以及允许外部访问&#xff0c;需要进行以下步骤&#xff1a; 下载地址 https://github.com/tporadowski/redi…

快速入门Jupyter notebook

快速入门 Jupyter notebook 一、前言&#xff08;一&#xff09;优点&#xff08;二&#xff09;特点&#xff08;三&#xff09;调用运行&#xff08;四&#xff09;新建 二、认识界面快捷键&#xff08;一&#xff09;三种模式&#xff08;1&#xff09;蓝色模式&#xff1a;…

中国森林地上和地下生物量碳变化数据集(2002-2021年)

中国森林地上和地下生物量碳变化数据集&#xff08;2002-2021年&#xff09; 数据介绍 为了量化中国近期全国性恢复工作的生态后果&#xff0c;过去20年森林生物量碳储量变化的空间显性信息至关重要。然而&#xff0c;在全国范围内进行长期生物量追踪仍然具有挑战性&#xff0c…

203、移除链表元素

1、题目描述 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xff1a;[1,2,3,4,5]示例 2&#xff1a; 输…

基于迁移学习的手势分类模型训练

1、基本原理介绍 这里介绍的单指模型迁移。一般我们训练模型时&#xff0c;往往会自定义一个模型类&#xff0c;这个类中定义了神经网络的结构&#xff0c;训练时将数据集输入&#xff0c;从0开始训练&#xff1b;而迁移学习中&#xff08;单指模型迁移策略&#xff09;&#x…

Layui修改表格分页为英文

Layui修改表格分页为英文 1.前言2.Laypage属性 1.前言 主要记录初次使用Layui没有好好看官方文档踩坑&#xff0c;修改了源码才发现可以自定义 使用的Layui版本2.9.14 2.Laypage属性 Laypage属性中带的有自定义文本的属性 示例代码 table.render({.......page: {skipText: …

状态机 XState 使用

状态机 一般指的是有限状态机&#xff08;Finite State Machine&#xff0c;FSM&#xff09;&#xff0c;又可以称为有限状态自动机&#xff08;Finite State Automation&#xff0c;FSA&#xff09;&#xff0c;简称状态机&#xff0c;它是一个数学模型&#xff0c;表示有限个…

硬核科普:什么是网络准入控制系统|网络准入控制系统四大品牌介绍

网络准入控制系统&#xff08;Network Access Control, NAC&#xff09;是一种用于确保只有授权设备和用户才能接入网络的安全技术。 本文将介绍几种常用的网络准入控制系统&#xff0c;帮助您更好地了解如何选择适合您企业的NAC系统。 网络准入控制的重要性和作用 网络准入控…

java学习--练习题

在类中this.属赋值&#xff0c;则外部创建对象调用其值也会随之一样 package com.test01;/* author:我与java相爱相杀---c语言梦开始的地方 今天又是努力学习的一天&#xff01;&#xff01;&#xff01;&#xff01; */ /*1. 在Frock类中声明私有的静态属性currentNum[int类型…

idm软件最新破解版下载 idm永久激活码 IDM中文绿色特别版 idm下载器汉化版

在互联网时代&#xff0c;下载管理软件成为了我们日常使用电脑不可或缺的工具之一。说起下载工具&#xff0c;大家的第一反应可能是网盘、迅雷。但在PC端其实还有一个可以对标他们的软件——IDM&#xff0c;这是一个口碑炸裂的多线程下载工具。 Internet Download Manager&…

让你的设计更出色:10个最受欢迎的3D画图工具盘点

随着渲染工具的发生和客户对立体效果的要求越来越高&#xff0c;设计师应该能够及时用设计风格解释空间界面&#xff0c;全面使用3D画图工具进行展览设计。3D画图工具在建筑、工程、产品设计等行业使用不同的算法&#xff0c;为图像添加色调、质感等细节。不同类型的3D画图工具…

鸿蒙HarmonyOS【应用开发五、组件介绍】

✍️作者简介&#xff1a;小北编程&#xff08;专注于HarmonyOS、Android、Java、Web、TCP/IP等技术方向&#xff09; &#x1f433;博客主页&#xff1a; 开源中国、稀土掘金、51cto博客、博客园、知乎、简书、慕课网、CSDN &#x1f514;如果文章对您有一定的帮助请&#x1f…

Java之 jvm

jvm之管理内存 程序计数器&#xff1a;当前线程所执行的字节码的行号指示器。程序计数器是唯一一个不会出现 OutOfMemoryError 的内存区域&#xff0c;它的生命周期随着线程的创建而创建&#xff0c;随着线程的结束而死亡。Java虚拟机栈 方法调用 一个方法调用都会有对应的栈帧…

Redis - SpringDataRedis - RedisTemplate

目录 概述 创建项目 引入依赖 配置文件 测试代码 测试结果 数据序列化器 自定义RedisTemplate的序列化方式 测试报错 添加依赖后测试 存入一个 String 类型的数据 测试存入一个对象 优化 -- 手动序列化 测试存入一个Hash 总结&#xff1a; 概述 SpringData 是 S…