嗨,我是 Android 和 Kotlin 环境的新手,我来自原生 iOS 背景。
我想创建一个 Kotlin MultiPlatform 应用程序,它可以在 Android 和 iOS 上运行。
为此,我创建了一个简单的列表视图,如下图所示。
我的源代码:
## MainActivity.kt
package com.ktlistviewdemo.srikanth.ktlistviewdemo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
class MainActivity : AppCompatActivity() {
var cityNamesArray = arrayOf("Melbourne", "Vienna", "Vancouver", "Toronto", "Calgary", "Adelaide", "Perth", "Auckland", "Helsinki", "Hamburg", "Munich", "New York", "Sydney", "Paris", "Cape Town", "Barcelona", "London", "Bangkok")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var adapterObject = ArrayAdapter(this, R.layout.listview_item, cityNamesArray)
var listViewObj: ListView = findViewById(R.id.listview_1)
listViewObj.adapter = adapterObject
}
}
## listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Each List Item is displayed as TextView defined below -->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10sp"
android:textSize="20sp"
android:textStyle="normal"
android:layout_marginLeft="50sp"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
/>
## activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview_1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.constraint.ConstraintLayout>
它完美地在 Android Emulator 上运行,然后现在我想转换为 Kotlin Multi Platform 应用程序。
我想在 Android 和 iOS 上运行这个应用程序。
谢谢