You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android自定义搜索框触发requestCursorUpdates警告且搜索失效

问题解决方案

一、实现实时输入过滤列表(核心功能问题)

当前代码仅在触发输入法Next动作时才执行搜索,无法满足输入时实时过滤的需求。需要修改SearchBar,监听输入内容变化并实时触发搜索:

修改SearchBar代码

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun SearchBar(
    modifier: Modifier = Modifier,
    onSearch: (String) -> Unit = {}
) {
    val searchQueryState = rememberSaveable { mutableStateOf("") }
    val keyboardController = LocalSoftwareKeyboardController.current
    
    // 监听输入变化,实时触发搜索
    LaunchedEffect(searchQueryState.value) {
        onSearch(searchQueryState.value.trim())
    }

    Column {
        Surface(modifier.padding(bottom = 10.dp),
            color = MaterialTheme.colors.primaryVariant) {
            CommonTextField(
                valueState = searchQueryState,
                placeholder = "Search",
                // 改为Done动作,适配回车清空逻辑
                imeAction = ImeAction.Done,
                onAction = KeyboardActions {
                    searchQueryState.value = ""
                    keyboardController?.hide()
                }
            )
        }
    }
}

优化ViewModel的搜索逻辑(添加防抖避免频繁过滤)

class YourViewModel : ViewModel() {
    private var _anyInfoResults = MutableStateFlow<List<AnywhereListEntity>>(emptyList())
    val anyInfoResults = _anyInfoResults.asStateFlow()

    private var cachedSearch = listOf<AnywhereListEntity>()
    private var isSearchStarting = true
    private val _isSearching = MutableStateFlow(false)
    val isSearching = _isSearching.asStateFlow()

    // 用Flow实现输入防抖
    private val searchQuery = MutableStateFlow("")

    init {
        searchQuery
            .debounce(300) // 300ms防抖,避免输入时频繁触发过滤
            .onEach { query ->
                performSearch(query)
            }
            .launchIn(viewModelScope)
    }

    // 对外暴露的搜索方法
    fun onSearchQueryChanged(query: String) {
        searchQuery.value = query
    }

    // 内部搜索逻辑
    private fun performSearch(query: String) {
        val listToSearch = if (isSearchStarting) _anyInfoResults.value else cachedSearch
        if (query.isEmpty()) {
            _anyInfoResults.value = cachedSearch
            _isSearching.value = false
            isSearchStarting = true
            return
        }
        val results = listToSearch.filter {
            it.name.contains(query, ignoreCase = true)
        }
        if (isSearchStarting) {
            cachedSearch = _anyInfoResults.value
            isSearchStarting = false
        }
        _anyInfoResults.value = results
        _isSearching.value = true
    }
}

最后在调用SearchBar的地方,传入onSearch = viewModel::onSearchQueryChanged即可实现实时过滤。

二、消除W/RecordingIC: requestCursorUpdates is not supported警告

该警告属于系统输入法框架的兼容性日志,和代码逻辑无关,是当前使用的输入法不支持requestCursorUpdatesAPI导致的,不会影响功能。消除方式:

  • 更换系统默认输入法(如Google输入法)测试
  • 在Logcat中通过过滤规则-tag:RecordingIC隐藏该类日志

额外优化建议

ViewModel中isSearching建议统一使用StateFlow而非mutableStateOf,保持数据流一致性,如上述代码所示。

内容的提问来源于stack exchange,提问作者OEThe11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 10:01:57