无法与应用程序预览进行交互

时间:2020-10-28 15:38:09

标签: android-studio android-jetpack-compose

我正在关注适用于Android Jetpack Compose的官方Google Code Labs,我们在其中构建了一个带有基于文本的计数器的交互式按钮。 Tutorial section 5

但是似乎我无法与预览互动,但是在我的设备上可以正常运行。我的意思是,点击似乎有效,但该值从未更新。

我有Fedora 32 64位

这是我的Android Studio:

Android Studio 4.2 Canary 15 
Build #AI-202.7660.26.42.6922807, built on October 22, 2020
Runtime version: 11.0.8+0-b944-P17168821 amd64
VM: OpenJDK 64-Bit Server VM by N/A
Linux 5.8.15-201.fc32.x86_64
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 4
Non-Bundled Plugins: org.jetbrains.kotlin
Current Desktop: GNOME 

这是MainActivity.kt

package com.loloof64.firstcomposecodelab

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Divider
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
import com.loloof64.firstcomposecodelab.ui.FirstComposeCodeLabTheme

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
            setContent {
                MyApp {
                    MyScreenContent()
                }
            }
        }
    }

    @Composable
    fun MyApp(content: @Composable () -> Unit) {
        FirstComposeCodeLabTheme {
            Surface(color = Color.Yellow) {
                content()
            }
        }
    }

    @Composable
    fun Greeting(name: String) {
        Text(text = "Hello $name !", modifier = Modifier.padding(8.dp))
    }

    @Composable
    fun MyScreenContent(names: List<String> = listOf("Android", "there")) {
        Column {
            for (name in names) {
                Greeting(name = name)
                Divider(color = Color.Black)
            }
        Divider(color = Color.Transparent, thickness = 32.dp)
        Counter()
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApp {
        MyScreenContent(listOf("Laurent", "Loloof64", "AChessGeek"))
    }
}

@Composable
fun Counter() {
    val count = remember{mutableStateOf(0)}

    Button(onClick =  {count.value++}) {
        Text("I've been clicked ${count.value} times !")
    }
}

我还尝试了连续单击几次,以确保这不是计算机上的滞后问题。

0 个答案:

没有答案