Код: Выделить всё
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Test1(
Test1Properties(
textProperties = TextProperties(),
buttonProperties = ButtonProperties(),
),
Test1Functions(),
)
}
}
}
@Composable
fun Test1(
test1Properties: Test1Properties,
test1Functions: Test1Functions,
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxSize(),
) {
Text(
text = test1Properties.textProperties.text,
color = test1Properties.textProperties.color,
fontSize = test1Properties.textProperties.fontSize,
fontWeight = test1Properties.textProperties.fontWeight,
)
Button(
onClick = { test1Functions.changeTextPropertiesText(test1Properties) },
colors = ButtonDefaults.buttonColors(
containerColor = test1Properties.buttonProperties.containerColor
),
) {
Text(text = test1Properties.buttonProperties.text)
}
}
}
< /code>
Вот файл, содержащий все классы данных, которые содержат все значения для композиции.
Существует три класса данных. Один для композиционного текста, один для кнопки и один, чтобы удерживать их все вместе. Существует один класс для функционального составления композиции COMPOSABLE COMPANTSFUNCTIONS import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
data class Test1Properties(
val textProperties: TextProperties,
val buttonProperties: ButtonProperties,
)
data class TextProperties(
var text: String = "Hello World",
var color: Color = Color.Red,
val fontSize: TextUnit = 30.sp,
val fontWeight: FontWeight = FontWeight.Bold,
)
data class ButtonProperties(
val onClick: () -> Unit = {},
val text: String = "Button",
val containerColor: Color = Color.Blue,
)
class Test1Functions {
fun changeTextPropertiesText(test1Properties: Test1Properties): Color {
test1Properties.textProperties.color = Color.Blue
return test1Properties.textProperties.color
}
}
< /code>
Все кажется хорошим, ошибки не возникают, но при нажатии кнопки предполагаемая функциональность не происходит. Предполагаемое функционально включает текст в текстовом композиционном синем.>
Подробнее здесь: https://stackoverflow.com/questions/784 ... -jetpack-c
Мобильная версия