如何在声明式属性中设置属性中字符串的默认值
我正在设置一些具有声明式词汇的属性,以制作自定义视图。此属性之一是字符串。我想做的是,如果我不为thiks属性设置XML值,请给它一个默认值,但我不知道该怎么做。
<?xml version="1.0" encoding ="utf-8"?>
<resources>
<declare-styleable name ="MyCustomView">
<attr name ="title" format="string"/>
</declare-styleable>
</resources>
在我的自定义视图中,我这样做:
private var title = ""
init{
val styledAttr = context.obtainStyledAttributes(attrs,R.styleable.MyCustomView)
title = styledAttr.getString(R.styleable.MyCustomView_title)!! //I cant remove "!!" or I get a type mismatch Required String Found String?
myTextView.text = title
}
在某些XML中:
<!--Here I can set title attribute but I want that if I dont set it a value, set a default value-->
<com.example.myproject.MyCustomView
android:layout_width="0dp"
android:layout_height = "wrap_content"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类型不匹配是因为您正在执行此操作:
您正在创建
var
而无需指定其 type ,因此编译器需要 teble it从您分配的价值中。因为您正在分配字符串
,所以最终是这样的:这是 nonnull -null 类型 - 编译器无法(并且不应该!)假设您只需通过传递
字符串
即可使其无效,因此您必须明确指定类型:现在您仍在分配一个非NULL
String
,但是您 can set <代码>标题 null (如果需要)。因此,您不需要!!
(除非您知道有一个很好的理由,否则您应该避免使用 - 您没有解决自己创建的问题,只需将其隐藏直到它进行错误)这只是您遇到的错误的一般解释,因此您知道它的意思以及如何处理 - 您问题的实际解决方案是Darshan在评论中发布的内容:
styledattr#getString 将返回 null 如果您要查找的
stylable
参考不存在attry
- 它很好,因为这意味着您可以获得“无处”的结果,您可以检查。如果结果为 null ,则提供一个默认的回落值,而不是猫王操作员(
?:
)在该行中进行 - 设置title
GetString
,,除非为null ,否则在这种情况下会在猫王之后评估这些内容,然后使用该值。因此,标题
要么从getString
中分配字符串
,或者“默认标题”
也是字符串
。无论哪种方式,它都不会为null,因此title
可以作为非编号字符串
type而不需要更改为字符串?(除非您需要 null值,否则切勿使用NALLABLE类型)
您可以将
getString
的结果分配给temp变量,并且dotitle = if(tempstring!=)无效的) Temptring Qualting其他“ DefaultTitle”
,但希望您能看到Kotlin如何使其无效的内容使其变得非常优雅。这就是为什么nulls在Kotlin中大量出现的一种无用/失败值,因此您可以轻松处理这些情况并提供后备。The type mismatch is because you're doing this:
You're creating a
var
without specifying its type, so the compiler needs to infer it from the value you're assigning. Because you're assigning aString
, it ends up being this:which is a non-null type - the compiler can't (and shouldn't!) assume you want it to be nullable just by passing a
String
, so you have to specify the type explicitly:Now you're still assigning a non-null
String
, but you can settitle
to null if you want. So you don't need the!!
(which you should avoid using unless you know there's a very good reason to - you're not fixing the problem you've created, just hiding it until it goes wrong)That's just a general explanation for that error you're getting, so you know what it's about and how to deal with it - the actual solution to your problem is what DarShan posted in the comments:
styledAttr#getString
will return null if thestyleable
reference you're looking up doesn't exist on thatattr
- which is good, because it means you get a "nothing there" result you can check for. If the result is null, then provide a default fallback value insteadWhich is what the elvis operator (
?:
) is doing in that line - settitle
to the result ofgetString
, unless that's null, in which case evaluate the stuff after the elvis and use that value instead. Sotitle
either gets assigned aString
fromgetString
, or"Default Title"
which is also aString
. Either way, it's not going to be null, sotitle
can stay as the non-nullString
type instead of needing to be changed toString?
(never use a nullable type unless you need null values)You could assign the result of
getString
to a temp variable and dotitle = if (tempString != null) tempString else "DefaultTitle"
but hopefully you can see how Kotlin makes it pretty elegant with its null-handling stuff. That's why nulls show up a lot in Kotlin as a kind of nothing/failure value, so you can easily handle those cases and provide fallbacks