Android + Kotlin: отменяет ли объявление «Кнопка» объявление «@Composable»?Android

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Android + Kotlin: отменяет ли объявление «Кнопка» объявление «@Composable»?

Сообщение Anonymous »

Ниже приведен пример проекта из раздела Jetpack на сайте Developer.android.com. Я расширил его функцией (MyColumnPainter), позволяющей рисовать «катящийся кубик». Первые два вызова и пятый компилируются нормально. Вторые два вызова дают мне ошибку «Функция @Composable должна быть вызвана из функции @Composable». Что происходит и как это «исправить»?
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.diceroller

import android.R.drawable
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
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.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.example.diceroller.ui.theme.DiceRollerTheme
import android.util.Log
import androidx.compose.ui.graphics.painter.Painter

val myImageList = intArrayOf(
R.drawable.dice_1,
R.drawable.dice_2,
R.drawable.dice_3,
R.drawable.dice_4,
R.drawable.dice_5,
R.drawable.dice_6
)

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DiceRollerTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
DiceRollerApp()
}
}
}
}
}

@Preview
@Composable
fun DiceRollerApp() {
DiceWithButtonAndImage(modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
)
}

@Composable
fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
MyColumnPainter(result = 1, modifier = Modifier, imageResource = 11)
var x: Integer // No imagination.
var xString1: String
var xString2: String // Also no imigination.
var xString3: String

var result by remember { mutableStateOf(1) }
val imageResource: Int = myImageList[result - 1]

Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
Image(painter = painterResource(imageResource), contentDescription = result.toString())
MyColumnPainter(result = 1, modifier = Modifier, imageResource = 11)

Button(
onClick = {
MyColumnPainter(result = 1, modifier = Modifier, imageResource = 11)
for (x in 1..6) {
result = (1..6).random()
MyColumnPainter(result, modifier, MyImageList[])
xString1 = "x = " + x.toString() + " result = " + result.toString()
Log.i("FYI", xString1)
}
},
) {
Text(text = stringResource(R.string.roll), fontSize = 24.sp)
MyColumnPainter(result = 1, modifier = Modifier, imageResource = 11)

}

}

}

@Composable
fun MyColumnPainter(result: Int, modifier: Modifier, imageResource: Int) {
Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
Image(
painter = painterResource(imageResource),
contentDescription = result.toString()
)
}

}


Подробнее здесь: https://stackoverflow.com/questions/787 ... -declarati
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Android»