Kotlin - 单击按钮时如何迭代范围?

问题描述 投票:0回答:1

我正在 Android Studio 中开发一个小应用程序,我需要的是“下一步”按钮在四个图像之间一张一张地切换。我记得我在 Dice Roller 应用程序中使用的逻辑,它使用范围和 .random 函数在 1..6 之间进行选择。然后,您将图像分配给这些数字。

这就是我所做的,我用 .random 函数运行它,工作完美,现在我只需要弄清楚如何在单击按钮时在范围内一一递增。进行:1、2、3、4,然后在达到 4 时再次进行。

这是代码:

package com.example.artspace

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
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.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.ContentScale
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.artspace.ui.theme.ArtSpaceTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ArtSpaceTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                ) {
                    ArtSpace()
                }
            }
        }
    }



    }


@Composable
fun ArtSpace(modifier: Modifier = Modifier) {
    var range by remember { mutableStateOf(1) }
    val imageResource = when(range) {
        1 -> R.drawable.bairen
        2 -> R.drawable.scenic_cliff
        3 -> R.drawable.the_stair_tree
        else -> R.drawable.abandoned




    }

    Column( modifier = modifier
        .fillMaxSize()
        .padding(10.dp),
        horizontalAlignment = Alignment.Start)
    {
        Row(modifier.padding(10.dp),
            verticalAlignment = Alignment.Top,
            horizontalArrangement = Arrangement.spacedBy(150.dp)
          ) {
            Button(
                onClick = { /*TODO*/ },
                modifier
                    .align(Alignment.Top)
                    .size(width = 110.dp, height = 50.dp)
            )
            {
                Text(stringResource(R.string.previous))
            }

            Button(
                onClick = { range = (1..4).random() },
                modifier
                    .align(Alignment.Bottom)
                    .size(width = 110.dp, height = 50.dp),
            )
            {
                Text(stringResource(R.string.next))

            }






        }

        Image(
            painter = painterResource(imageResource),
            contentDescription = "cliff",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(600.dp)
                .clip(RoundedCornerShape(16.dp))
                .border(3.dp, Color.Black, RoundedCornerShape(16.dp))
        )
        Spacer(modifier = Modifier.size(50.dp))
        Row(modifier = Modifier.align(Alignment.CenterHorizontally)) {

            Text(
                stringResource(R.string.image_name1),
                modifier = Modifier
                    .padding(16.dp)
                    .border(3.dp, Color.Black, RectangleShape)
                    .padding(16.dp)
                    .padding(horizontal = 100.dp)

                )
        }
         }

    }









@Preview(showBackground = true)
@Composable
fun ArtSpacePreview() {
    ArtSpaceTheme {
        ArtSpace()
    }
}

android kotlin android-jetpack-compose
1个回答
0
投票

我们到了!我用过

Button(
                onClick = { range--.until(0)},
                modifier
                    .align(Alignment.Top)
                    .size(width = 110.dp, height = 50.dp)
            )
            {
                Text(stringResource(R.string.previous))
            }

            Button(
                onClick = { range++.until(5) },
                modifier
                    .align(Alignment.Bottom)
                    .size(width = 110.dp, height = 50.dp),
            )
            {
                Text(stringResource(R.string.next))

            }

© www.soinside.com 2019 - 2024. All rights reserved.