<pre>
classMainTestActivity : Activity() { //当某个变量的值可以为null的时候,必须在声明处的类型后添加?来标识该引用可为空。 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_test_activity) kotlin_test.setText("kotlin测试") valtestButton= findViewById(R.id.kotlin_test_btn) as Button testButton.setText("进行kotlin测试") testButton.setOnClickListener { Toast.makeText(this,"kotlin点击事件",Toast.LENGTH_LONG).show() } //变量声明: //可以用关键字var声明为可变的,否则使用只读关键字val //一次赋值(只读)的局部变量: //int类型常量定义 val aConstant: Int =1000 val bConstant: Int =200 val cConstant: Int =300 //可变变量: var dVariable: Int dVariable=5 Log.d("alen","sum is " + sum(aConstant,bConstant)) Log.d("alen","printSum is " + printSum(aConstant,bConstant,cConstant)) stringTemplate() printValue("4","6") printValue("4","") val length= getStringLength("nihao") Log.d("alen","length is " +length) loopForTest1() loopForTest2() whileForTest() Log.d("alen","msg is " + whenForTest("hello")) Log.d("alen","msg is " + whenForTest(1)) mapForTest() setForDataClassTest()} } }
//使用字符串模板 fun stringTemplate(): Unit { var arg: Int =1 val s1= "arg is$arg" Log.d("alen","s1 is " +s1) arg=2 //模板中的任意表达式: var s2= "${s1.replace("is","was")}, but now arg is$arg" Log.d("alen","s2 is " +s2) }
// 带有两个Int参数、返回Int的函数 funsum(arg1: Int,arg2: Int): Int { returnarg1 + arg2 }
// 函数返回无意义的值,Unit返回类型可以省略 funprintSum(arg1: Int,arg2: Int,arg3: Int): Unit { Log.d("alen","sum is " + (arg1 + arg2 + arg3)) }
// 把字符串转为Int fun parseInt(str: String): Int? { // try表达式 try{ if(str !=null) { returnstr.toInt() } } catch (e: NumberFormatException) { return null}return null } } funprintValue(str1: String,str2: String) { val result1: Int? = parseInt(str1) valr esult2: Int? = parseInt(str2) if(result1!=null&&result2!=null) { Log.d("alen","value is " +result1*result2) } else { Log.d("alen"," '$result1' or '$result2' is null") } }
// 打印字符串的长度 fun getStringLength(obj: Any): Int? { if(obj is String) { return obj.length } return null }
// 迭代 fun loopForTest1() { val items=listOf("item1","item2","item3") for(item in items) { Log.d("alen","item is " +item); } }
// 通过索引值进行循环 fun loopForTest2() { val items=listOf("item1","item2","item3") //使用in运算符来检测某个数字是否在指定区间内: for(index in items.indices) { Log.d("alen","item index is$index,value is${items[index]}") } if(-1 !in items.indices) { Log.d("alen","-1 not in range") } // 从9到0开始迭代,步长为3 // 区间表达式由具有操作符形式..的rangeTo函数辅以in和!in形成。 // downTo()函数 倒序迭代数字 // step()函数 以不等于1的任意步长迭代数字 for(x in 9 downTo 0 step 3) { Log.d("alen","x is " +x) } //创建一个不包括其结束元素的区间,可以使用until函数: for(x in 1 until 10) { // i in [1, 10)排除了10 Log.d("alen","until x is " +x) } }
// while循环测试 fun whileForTest() { val items=listOf("apple","item2","banana") items.isEmpty() Log.d("alen","items size is " +items.size) //区间range Log.d("alen","items indices is " +items.indices) if(items.size >2) { Log.d("alen","items index 2 is " +items.get(2)) } Log.d("alen","items last index is " +items.lastIndex) //使用in运算符来判断集合内是否包含某实例: when { "apple"initems-> { Log.d("alen","items contain apple") } "orange"!initems-> { Log.d("alen","items do not contain orange") } } }
// when条件测试 fun whenForTest(obj: Any): String = when(obj) { 1-> { Log.d("alen","test one") "one" } "hello" -> "Greeting" isLong -> "Long" !isString -> "Not a string" else-> "Unkonwn" }
// map测试 fun mapForTest() { val map=mapOf("a"to"1","b"to"2","c"to"3") Log.d("alen","map key is a,value is " +map["a"]) for(item in map) { Log.d("alen","map key is " +item.key + ",value is " +item.value) } }
// set测试 // set // 1. Set中没有重复的对象 // 2. Set中对象不按特定的方式排序 //判断两个对象的是否重复标准是hashCode()和equals()两个参考值, //也就是说只有两个对象的hashCode值一样与equals()为真时,才认为是相同的对象 // fun hashSetOf(vararg elements: T): java.util.HashSet // fun linkedSetOf(vararg elements: T): java.util.LinkedHashSet // sortedSetOf(comparator: java.util.Comparator, vararg elements: T): java.util.TreeSet // fun sortedSetOf(vararg elements: T): java.util.TreeSet funsetForDataClassTest() { // Book是一个data class,用来做data model使用 val bookA= Book("A",20,"AA") val bookB= Book("B",30,"BB") val bookC= Book("C",20,"CC") val bookD= Book("D",50,"DD") val bookE= Book("B",30,"EE") val set=setOf(bookA,bookB,bookC,bookD) for(iteminset) { Log.d("alen","item$item, hashCode${item.hashCode()}") } Log.d("alen","--------------------------") val mutalbeSet=mutableSetOf(bookA,bookB,bookC,bookD,bookE) for(item in mutalbeSet) { Log.d("alen","item is$item, hashCode is${item.hashCode()}") } mutalbeSet.add(Book("甲乙丙丁",50,"EE")) Log.d("alen","remove B value is " +mutalbeSet.remove(bookB)) Log.d("alen","remove E value is " +mutalbeSet.remove(bookE)) Log.d("alen","--------------------------") for(item in mutalbeSet) { Log.d("alen","item is$item, hashCode is${item.hashCode()}") } }
</pre>