如何在声明式属性中设置属性中字符串的默认值

发布于 2025-02-12 21:38:19 字数 916 浏览 2 评论 0 原文

我正在设置一些具有声明式词汇的属性,以制作自定义视图。此属性之一是字符串。我想做的是,如果我不为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"
/>

I'm setting some attributes with declare-styleable to make a custom view. One of this attributes is a string. What I want to do, is that if I dont set a value in XML for thiks attribute, give it a default value but I don't know how to do it.

<?xml version="1.0" encoding ="utf-8"?>
<resources>
  <declare-styleable name ="MyCustomView">
     <attr name ="title" format="string"/>
  </declare-styleable>
</resources>

In my custom view Im doing this:

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
}

In some 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水波映月 2025-02-19 21:38:19
//I cant remove "!!" or I get a type mismatch Required String Found String?
title = styledAttr.getString(R.styleable.MyCustomView_title)!!

类型不匹配是因为您正在执行此操作:

private var title = ""

您正在创建 var 而无需指定其 type ,因此编译器需要 teble it从您分配的价值中。因为您正在分配字符串,所以最终是这样的:

private var title: String = ""

这是 nonnull -null 类型 - 编译器无法(并且不应该!)假设您只需通过传递字符串即可使其无效,因此您必须明确指定类型:

private var title: String? = ""
//  note the nullable ? ^^^

现在您仍在分配一个非NULL String ,但是您 can set <代码>标题 null (如果需要)。因此,您不需要 !! (除非您知道有一个很好的理由,否则您应该避免使用 - 您没有解决自己创建的问题,只需将其隐藏直到它进行错误)


这只是您遇到的错误的一般解释,因此您知道它的意思以及如何处理 - 您问题的实际解决方案是Darshan在评论中发布的内容:

title = styledAttr.getString(R.styleable.MyCustomView_title) ?: "Default Title"

styledattr#getString 将返回 null 如果您要查找的 stylable 参考不存在 attry - 它很好,因为这意味着您可以获得“无处”的结果,您可以检查。如果结果为 null ,则提供一个默认的回落值,而

不是猫王操作员(?:在该行中进行 - 设置 title GetString ,除非为null ,否则在这种情况下会在猫王之后评估这些内容,然后使用该值。因此,标题要么从 getString 中分配字符串,或者“默认标题” 也是字符串。无论哪种方式,它都不会为null,因此 title 可以作为非编号字符串 type而不需要更改为字符串?(除非您需要 null值,否则切勿使用NALLABLE类型)

您可以将 getString 的结果分配给temp变量,并且do title = if(tempstring!=)无效的) Temptring Qualting其他“ DefaultTitle” ,但希望您能看到Kotlin如何使其无效的内容使其变得非常优雅。这就是为什么nulls在Kotlin中大量出现的一种无用/失败值,因此您可以轻松处理这些情况并提供后备。

//I cant remove "!!" or I get a type mismatch Required String Found String?
title = styledAttr.getString(R.styleable.MyCustomView_title)!!

The type mismatch is because you're doing this:

private var title = ""

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 a String, it ends up being this:

private var title: String = ""

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:

private var title: String? = ""
//  note the nullable ? ^^^

Now you're still assigning a non-null String, but you can set title 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:

title = styledAttr.getString(R.styleable.MyCustomView_title) ?: "Default Title"

styledAttr#getString will return null if the styleable reference you're looking up doesn't exist on that attr - 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 instead

Which is what the elvis operator (?:) is doing in that line - set title to the result of getString, unless that's null, in which case evaluate the stuff after the elvis and use that value instead. So title either gets assigned a String from getString, or "Default Title" which is also a String. Either way, it's not going to be null, so title can stay as the non-null String type instead of needing to be changed to String? (never use a nullable type unless you need null values)

You could assign the result of getString to a temp variable and do title = 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文