android - 箭头在 Kotlin 中的作用是什么?

标签 android kotlin

我开始使用 Kotlin 进行 Android 开发。我对某些概念有疑问。
我在条件语句中使用“->”,但我不知道在这个例子中这意味着什么:

 XmlPullParser.START_TAG -> {...}
 XmlPullParser.TEXT -> textValue = xpp.text
 XmlPullParser.END_TAG -> {...}

全部代码为:

        val factory = XmlPullParserFactory.newInstance()
        factory.isNamespaceAware = true
        val xpp = factory.newPullParser()
        xpp.setInput(xmlData.reader())
        var eventType = xpp.eventType
        var currentRecord = FeedEntry()
        while (eventType != XmlPullParser.END_DOCUMENT) {
            val tagName = xpp.name.toLowerCase()    
            when (eventType) {

              XmlPullParser.START_TAG -> {
                    Log.d(TAG, "parse: Starting tag for " + tagName)
                    if (tagName == "entry") {
                        inEntry = true
                    }
                }

                XmlPullParser.TEXT -> textValue = xpp.text

                   XmlPullParser.END_TAG -> {
                    Log.d(TAG, "parse: Ending tag for " + tagName)
                    if (inEntry) {
                        when (tagName) {
                            "entry" -> {
                                applications.add(currentRecord)
                                inEntry = false
                                currentRecord = FeedEntry()  
                            }

                            "name" -> currentRecord.name = textValue
                            "artist" -> currentRecord.artist = textValue
                            "releasedate" -> currentRecord.releaseDate = textValue
                            "summary" -> currentRecord.summary = textValue
                            "image" -> currentRecord.imageURL = textValue
                        }
                    }
                }
            }

最佳答案

这只是 syntax对于 Kotlin when expression .

基本上,它会检查 when () 中的条件,如果匹配则执行 -> 之后的 block 。 您可以“内联”必须执行的代码将其包装在花括号中。

另见 this example (Kotlin Playground)并将 aString 更改为另一个值。

关于android - 箭头在 Kotlin 中的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57568836/

相关文章:

java - 在 Android 中将 protobuf(由 grpc 生成)转换为 JSON

android - 安装kotlin后无法打开Android studio项目

android - 火力地堡 Firestore : Is there a callback when there is no internet connection?

java - 从 Activity 调用 finish()

java - 只有加密图像应保存在文件中并解密图像以供预览

android - 折叠工具栏布局展开点击事件而不是滚动

Android EditText MultiLine 与句子大写

kotlin - Kotlin Flow 的 GroupBy 运算符

android - 按照开发人员网站上的选项卡布局 android 示例并尝试添加第四个选项卡

android - ClassCastException : android. 无法转换 os.BinderProxy