fun demo() {val string1: String = "string1"val string2: String? = nullval string3: String? = "string3"println(string1.length) //7println(string2?.length) //nullprintln(string3?.length) //7}
if(view instanceof TextView) {TextView textView = (TextView) view;textView.setText("text");}
if(view is TextView) {TextView textView = view as TextViewtextView.setText("text")}
if(view instanceof TextView) {((TextView) view).setText("text");}
if(view is TextView) {(view as TextView).setText("text")}
if(view is TextView) {view.setText("text")}
val a: A? = A()if (a != null) {println(a?.b)}
fun testWhen(obj: Any) {when(obj) {is Int -> {println("obj is a int")println(obj + 1)}is String -> {println("obj is a string")println(obj.length)}else -> {println("obj is something i don‘t care")}}}fun main(args: Array<String>) {testWhen(98)testWhen("98")}
fun testWhen(int: Int) {when(int) {in 10 .. Int.MAX_VALUE -> println("${int} 太大了我懒得算")2, 3, 5, 7 -> println("${int} 是质数")else -> println("${int} 不是质数")}}fun main(args: Array<String>) {(0..10).forEach { testWhen(it) }}
0 不是质数1 不是质数2 是质数3 是质数4 不是质数5 是质数6 不是质数7 是质数8 不是质数9 不是质数10 太大了我懒得算
(0 until container.childCount).map { container.getChildAt(it) }.filter { it.visibility == View.GONE }.forEach { it.visibility = View.VISIBLE }
for(int i = 0; i < container.childCount - 1; i++) {View childView = container.getChildAt(i);if(childView.getVisibility() == View.GONE) {childView.setVisibility(View.VISIBLE);}}
async {val response = URL("https://www.baidu.com").readText()uiThread {textView.text = response}}
object Log {fun i(string: String) {println(string)}}fun main(args: Array<String>) {Log.i("test")}
class Person(var name: String)val person = Person("张三");
class Person(var name: String = "张三")val person = Person()
data class Column(var subId: String?,var subTitle: String?,var subImg: String?,var subContentnum: Int?,)
android {compileSdkVersion 23buildToolsVersion "23.0.2"defaultConfig {applicationId "com.zll.demo"minSdkVersion 15targetSdkVersion 23versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘}}}
override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val homeFragment = HomeFragment()val columnFragment = ColumnFragment()val mineFragment = MineFragment()setContentView(tabPages {backgroundColor = R.color.whitedividerColor = R.color.colorPrimarybehavior = ByeBurgerBottomBehavior(context, null)tabFragment {icon = R.drawable.selector_tab_homebody = homeFragmentonSelect { toast("home selected") }}tabFragment {icon = R.drawable.selector_tab_searchbody = columnFragment}tabImage {imageResource = R.drawable.selector_tab_photoonClick { showSheet() }}tabFragment {icon = R.drawable.selector_tab_minebody = mineFragment}})}
override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)verticalLayout {textView {text = "这是标题"}.lparams {width = matchParentheight = dip(44)}textView {text = "这是内容"gravity = Gravity.CENTER}.lparams {width = matchParentheight = matchParent}}}
class Preference<T>(val context: Context, val name: String?, val default: T) : ReadWriteProperty<Any?, T> {val prefs by lazy {context.getSharedPreferences("xxxx", Context.MODE_PRIVATE)}override fun getValue(thisRef: Any?, property: KProperty<*>): T = with(prefs) {val res: Any = when (default) {is Long -> {getLong(name, 0)}is String -> {getString(name, default)}is Float -> {getFloat(name, default)}is Int -> {getInt(name, default)}is Boolean -> {getBoolean(name, default)}else -> {throw IllegalArgumentException("This type can‘t be saved into Preferences")}}res as T}override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = with(prefs.edit()) {when (value) {is Long -> putLong(name, value)is String -> putString(name, value)is Float -> putFloat(name, value)is Int -> putInt(name, value)is Boolean -> putBoolean(name, value)else -> {throw IllegalArgumentException("This type can‘t be saved into Preferences")}}.apply()}}
class EntranceActivity : BaseActivity() {private var userId: String by Preference(this, "userId", "")override fun onCreate(savedInstanceState: Bundle?) {testUserId()}fun testUserId() {if (userId.isEmpty()) {println("userId is empty")userId = "default userId"} else {println("userId is $userId")}}}
fun ImageView.displayUrl(url: String?) {if (url == null || url.isEmpty()) {imageResource = R.mipmap.ic_launcher} else {Glide.with(context).load(ColumnServer.SERVER_URL + url).into(this)}}...val imageView = findViewById(R.id.avatarIv) as ImageViewimageView.displayUrl(url)
val onlyTv = findViewById(R.id.onlyTv) as TextView
val onlyTv = find<TextView>(R.id.onlyTv)val onlyTv: TextView = find(R.id.onlyTv)
inline fun <reified T : View> Activity.find(id: Int): T = findViewById(id) as T
import kotlinx.android.synthetic.main.activity_main.*...onlyTv.text="不需要声明直接可以用"
Intent intent = new Intent(LoginActivity.this, MainActivity.class);intent.putExtra("name", "张三");intent.putExtra("age", 27);startActivity(intent);
startActivity<MainActivity>()startActivity<MainActivity>("name" to "张三", "age" to 27)startActivityForResult<MainActivity>(101, "name" to "张三", "age" to 27)
Toast.makeText(context, "this is a toast", Toast.LENGTH_SHORT).show();
context.toast("this is a toast")
toast("this is a toast")
longToast("this is a toast")
class A(val b: B)class B(val c: C)class C(val content: String)
C c = new C("content");B b = new B(c);A a = new A(b);
@kotlin.internal.InlineOnlypublic inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
public fun <T> T.apply(block: T.() -> Unit): T {block()return this}
val textView = TextView(context).apply {text = "这是文本内容"textSize = 16f}
layout.addView(TextView(context).apply {text = "这是文本内容"textSize = 16f})
val a = A().apply {b = B().apply {c = C("content")}}
原文:http://www.cnblogs.com/baiqiantao/p/8ffb4e6df798972b966e603dd32477f8.html