android - 安卓。SetText 不会更改 Button 的文本
问题描述
我有自定义视图 - 自定义键盘。每个按钮都是MaterialButton
。我需要实现shift
按钮来更改字母大小写,所以我这样做了
private fun onCaseChanged() {
buttons.forEach { button ->
(button as? TextView)?.let {
val text = it.text.toString()
it.text = if (isUpperCase)
text.toUpperCase(Locale.getDefault())
else
text.toLowerCase(Locale.getDefault())
}
}
}
除了 UI 之外,一切都正常工作 - 按钮上的文本不会改变。当我将按钮更改为TextView
s - 一切正常。但不适用于任何类型的Button
. 所以现在这对我来说似乎是唯一的方法。
但是我仍然很好奇 - 如果离开MaterialButton
s是否有另一种方法可以解决这个问题
解决方案
尝试android:textAllCaps="false"
在您的 XML 文件中的 Button 声明。如果成功,我建议更改主题中的默认按钮样式,以便在您需要时默认为 false。
例子
<Button
android:id="@+id/button"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
推荐阅读
- apache-spark-sql - Spark SQL 使用覆盖模式将 DF 写入 Teradata 表会丢弃表,尽管 truncate 为 True
- c# - ASPNET - 无法使用此动词类型 GET 发送内容主体
- mercurial - Mercurial 如何忽略包含已添加到源代码管理的文件的子文件夹
- python - 我一般如何将参数传递给 super().__init__()?
- java - Hazelcast 锁不提供同步
- java - scrollPane 不起作用,所以我想知道缺少什么
- ios - 按屏幕高度获取 iPhone 型号 XS Max
- r - R 编程中的 Rosenblatts 感知器实现
- asp.net - 导航栏换行类似于 asp 中的 Visual Studio
- php - PHP Laravel API - 如何限制对多个受信任主机的请求?