求Compose中支持图片键盘、适配主题的EditText-backed TextField方案
问题:Compose中适配主题且支持图片键盘的文本控件选择
我想在Compose应用中实现图片键盘支持功能,但目前官方暂无公开API可用(相关问题已存在两年)。虽然TextInputServiceAndroid类有fun createInputConnection(outAttrs: EditorInfo): InputConnection方法,但它被标记为internal,无法通过重写实现功能,因此只能采用View-Compose互操作方案。
由于长期没接触Android View的高级定制,我对EditText的样式调整(比如颜色、形状)感到棘手。我曾尝试用EditText实现核心功能,代码如下:
val textColor = LocalContentColor.current.toArgb() AndroidView( modifier = Modifier.fillMaxWidth(), factory = { object : EditText(it) { override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection { val ic: InputConnection = super.onCreateInputConnection(editorInfo) EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/*")) val callback = InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, opts -> val lacksPermission = (flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && lacksPermission) { try { inputContentInfo.requestPermission() } catch (e: Exception) { return@OnCommitContentListener false } } uris.add(inputContentInfo.contentUri) true } return InputConnectionCompat.createWrapper(ic, editorInfo, callback) } }.apply { setBackgroundColor(Color.TRANSPARENT) setTextColor(textColor) } } )
但进一步定制EditText的成本很高,而且我不想同时维护两套主题系统,希望直接从MaterialTheme中获取配置。想请教社区:哪种Android View文本编辑控件能支持重写onCreateInputConnection,且易于适配Compose主题,可以作为TextField的底层实现?
内容的提问来源于stack exchange,提问作者Calamity
相关产品推荐
相关产品推荐

