<pre>
classMainTestActivity : Activity() { ? ? //當(dāng)某個(gè)變量的值可以為null的時(shí)候,必須在聲明處的類型后添加?來標(biāo)識(shí)該引用可為空锈拨。 ? ? 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("進(jìn)行kotlin測試") ? ? ? ? testButton.setOnClickListener { ? ? ? ? ? ? Toast.makeText(this,"kotlin點(diǎn)擊事件",Toast.LENGTH_LONG).show() ? ? ? ? } ? ? ? ? //變量聲明: ? ? ? ? //可以用關(guān)鍵字var聲明為可變的秆麸,否則使用只讀關(guān)鍵字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 //模板中的任意表達(dá)式: var s2= "${s1.replace("is","was")}, but now arg is$arg" Log.d("alen","s2 is " +s2) }
// 帶有兩個(gè)Int參數(shù)允跑、返回Int的函數(shù) funsum(arg1: Int,arg2: Int): Int { ? ? returnarg1 + arg2 }
// 函數(shù)返回?zé)o意義的值,Unit返回類型可以省略 funprintSum(arg1: Int,arg2: Int,arg3: Int): Unit { ? ? Log.d("alen","sum is " + (arg1 + arg2 + arg3)) }
// 把字符串轉(zhuǎn)為Int fun parseInt(str: String): Int? { ? ? // try表達(dá)式 ? ? 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); ? ? } }
// 通過索引值進(jìn)行循環(huán) fun loopForTest2() { ? ? val items=listOf("item1","item2","item3") ? ? //使用in運(yùn)算符來檢測某個(gè)數(shù)字是否在指定區(qū)間內(nèi): ? ? 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 ? ? // 區(qū)間表達(dá)式由具有操作符形式..的rangeTo函數(shù)輔以in和!in形成凑懂。 ? ? // downTo()函數(shù) 倒序迭代數(shù)字 ? ? // step()函數(shù) 以不等于1的任意步長迭代數(shù)字 ? ? for(x in 9 downTo 0 step 3) { ? ? ? ? Log.d("alen","x is " +x) ? ? } ? ? //創(chuàng)建一個(gè)不包括其結(jié)束元素的區(qū)間腥椒,可以使用until函數(shù): ? ? for(x in 1 until 10) { ? ? ? ? // i in [1, 10)排除了10 ? ? ? ? Log.d("alen","until x is " +x) ? ? } }
// while循環(huán)測試 fun whileForTest() { ? ? val items=listOf("apple","item2","banana") ? ? items.isEmpty() ? ? Log.d("alen","items size is " +items.size) ? ? //區(qū)間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運(yùn)算符來判斷集合內(nèi)是否包含某實(shí)例: ? ? 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中沒有重復(fù)的對(duì)象 // 2. Set中對(duì)象不按特定的方式排序 //判斷兩個(gè)對(duì)象的是否重復(fù)標(biāo)準(zhǔn)是hashCode()和equals()兩個(gè)參考值勾笆, //也就是說只有兩個(gè)對(duì)象的hashCode值一樣與equals()為真時(shí)敌蚜,才認(rèn)為是相同的對(duì)象 // 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是一個(gè)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>