【发布时间】:2021-03-02 22:29:29
【问题描述】:
在将完全用 Jetpack Compose 编写的键盘从 Compose 版本 alpha-11 更新到 beta-01 后,我遇到了问题。
在升级之前,用户界面可以正常工作,正如您所期望的那样。波纹显示得很好。 升级后动画和效果(如按下按钮)无法正确播放(波纹效果似乎卡住)。 看看:
这是期望的行为以及版本升级前的样子:
注意:ComposeKeyboardView 之外的相同代码运行良好。此外,在 Jetpack Compose alpha-11 和 beta-01 之前,相同的代码运行良好。
我不确定这是否是一个错误,或者我是否可以自己解决问题。
对于恢复所需行为的任何帮助或提示,我表示感谢。
您可以使用以下代码重现该问题:
Keyboard.kt
@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
class ComposeKeyboardView constructor(
context: Context,
) : AbstractComposeView(context) {
@Composable
override fun Content() {
Keyboard()
}
}
IMEService.kt
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:
<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
[...]
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'
}
编辑: compose-beta02 中的同样问题
【问题讨论】:
-
抱歉,但我们需要帖子以独立,并附上您的minimal reproducible example此处,问题内。只要 Stack Overflow 上的问题持续存在,就不能保证外部网站存在。
标签: android android-jetpack-compose