TextField(value = "", onValueChange = {},
leadingIcon = {
Icon( Icons.Default.Search, contentDescription = "")
},
placeholder = {
Text(text = stringResource(id = R.string.placeholder_search))
},
colors = TextFieldDefaults.textFieldColors(), // 这个API已废弃
modifier = modifier
.heightIn(min = 56.dp)
.fillMaxWidth()
)
这段代码中,TextFieldDefaults.textFieldColors()
被标记为过时,并提示您迁移到 Material3 库。在您添加了 Material3 依赖后,依然显示 textFieldColors()
已废弃。
对于 Material3 中的 TextField 颜色配置,您可以使用 MaterialTheme3.TextFieldDefaults.textFieldColors()
替换原有的 TextFieldDefaults.textFieldColors()
。请尝试以下更新后的代码:
import androidx.compose.material3.MaterialTheme3
// ...
TextField(value = "",
onValueChange = {},
leadingIcon = {
Icon(Icons.Default.Search, contentDescription = "")
},
placeholder = {
Text(text = stringResource(id = R.string.placeholder_search))
},
colors = MaterialTheme3.TextFieldDefaults.textFieldColors(),
modifier = modifier
.heightIn(min = 56.dp)
.fillMaxWidth()
)
同时,请确认您的 build.gradle 文件中的依赖关系如下(根据您的描述已经包含 Material3,这里再次列出作为参考):
dependencies {
def composeBom = platform('androidx.compose:compose-bom:2023.10.01')
implementation(composeBom)
androidTestImplementation(composeBom)
implementation 'androidx.core:core-ktx:1.12.0'
implementation "androidx.compose.ui:ui"
implementation 'androidx.compose.material3:material3'
implementation 'androidx.compose.material3:material3-window-size-class:1.1.2'
implementation "androidx.compose.material3:material3-icons-extended" // 更改为 material3 的图标库
implementation "androidx.compose.ui:ui-tooling-preview"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4"
debugImplementation "androidx.compose.ui:ui-tooling"
}