首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >升级到beta-01后用Jetpack编写的键盘上的动画和效果

升级到beta-01后用Jetpack编写的键盘上的动画和效果
EN

Stack Overflow用户
提问于 2021-03-02 22:29:29
回答 1查看 430关注 0票数 1

在将我的键盘完全用Jetpack写成的键盘更新后,我遇到了一个问题,从撰写版本alpha-11 beta-01**.**

在升级之前,UI运行得很好,就像您预期的那样。涟漪显示得很好。升级后,动画和效果(如按下按钮)不能正确播放(Ripple效果似乎被卡住了)。看一看:

这是想要的行为,以及版本升级之前的样子:

注意:ComposeKeyboardView之外的相同代码运行得非常好。而且,在Jetpack编写alpha-11beta-01之前,相同的代码运行得非常好。我不确定这是个错误,还是我自己能解决这个问题。我很感激任何帮助或提示来恢复期望的行为。

您可以使用以下代码再现问题:

Keyboard.kt

代码语言:javascript
复制
@Composable
fun Keyboard() {
    Column(
        Modifier
            .fillMaxWidth()
            .height(200.dp)
            .background(Color.Gray),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(color = Color.Black, text = "This should resemble a keyboard")
        Button(modifier = Modifier.width(250.dp),onClick = {  }) {
            Text(text = "A Button")
        }
    }
}

ComposeKeyboardView.kt

代码语言:javascript
复制
class ComposeKeyboardView constructor(
    context: Context,

    ) : AbstractComposeView(context) {

    @Composable
    override fun Content() {
        Keyboard()
       
    }
}

IMEService.kt

代码语言:javascript
复制
class IMEService : InputMethodService(), LifecycleOwner, ViewModelStoreOwner,
    SavedStateRegistryOwner {

    override fun onCreateInputView(): View {
        val view = ComposeKeyboardView(this)
        window!!.window!!.decorView.let { decorView ->
            ViewTreeLifecycleOwner.set(decorView, this)
            ViewTreeViewModelStoreOwner.set(decorView, this)
            ViewTreeSavedStateRegistryOwner.set(decorView, this)
        }
        return view
    }


    //Lifecycle Methods

    private var lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)

    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry
    }


    private fun handleLifecycleEvent(event: Lifecycle.Event) =
        lifecycleRegistry.handleLifecycleEvent(event)

    override fun onCreate() {
        super.onCreate()
        savedStateRegistry.performRestore(null)
        handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
    }



    override fun onDestroy() {
        super.onDestroy()
        handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    }


    //ViewModelStore Methods
    private val store = ViewModelStore()

    override fun getViewModelStore(): ViewModelStore = store

    //SaveStateRegestry Methods

    private val savedStateRegistry = SavedStateRegistryController.create(this)

    override fun getSavedStateRegistry(): SavedStateRegistry = savedStateRegistry.savedStateRegistry

不要忘记将IMEService添加到您的AndroidManifest.xml

代码语言:javascript
复制
<application>
[...]
<service
            android:name=".IMEService"
            android:label="Example Comopose IME"

            android:permission="android.permission.BIND_INPUT_METHOD">
            <intent-filter>
                <action android:name="android.view.InputMethod" />
            </intent-filter>

            <meta-data
                android:name="android.view.im"
                android:resource="@xml/method" />
        </service>
</application>

app\build.gradle

代码语言:javascript
复制
[...]
dependencies {
    def compose_version = "1.0.0-beta01"  
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.activity:activity-compose:1.3.0-alpha03"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
}

编辑:撰写-beta02 02中的相同问题

或者您可以在这里找到所有代码并直接复制它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-19 17:37:14

我下载了您的代码,并在撰写程序的bug跟踪器中看到了你的评论。看起来,如果在IMEService类中执行以下操作,动画就会工作。

代码语言:javascript
复制
override fun onCreate() {
    super.onCreate()
    savedStateRegistry.performRestore(null)
    handleLifecycleEvent(Lifecycle.Event.ON_RESUME) // << here
}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66448080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档