[代码]java代码:
TextView username;
username=(TextView)findViewById(R.id.user);
username.setText("我是来自APKBUS的文本");
[代码]java代码:
@BindView(R.id.user)
TextView username;
username.setText("我是来自APKBUS的文本");
[代码]java代码:
user.text="我是来自APKBUS的文本"
[代码]java代码:
//activity_login就是我们的布局
import kotlinx.android.synthetic.main.activity_login.*
Anko Layout
[代码]java代码:
verticalLayout {
val textView=textView("我是来自APKBUS的文本")
val name = editText("EditText")
button("Button") {
onClick { toast("${name.text}!") }
}
}
[代码]java代码:
val textView=textView("我是来自APKBUS的文本"){
textSize = sp(17).toFloat()
textColor=context.resources.getColor(R.color.red)
}.lparams{
margin=dip(10)
height= dip(40)
width= matchParent
}
在上面创建UI过程中,我们直接把创建UI的代码写在onCreate方法中了,当然,还有一种写法。我们创建一个内部类实行AnkoComponent接口,并重写createView方法,该方法返回一个View,也就是我们创建的布局。修改如下
[代码]java代码:
inner class UI : AnkoComponent {
override fun createView(ui: AnkoContext): View {
return with(ui){
verticalLayout {
val textView=textView("我是来自APKBUS的文本"){
textSize = sp(17).toFloat()
textColor=context.resources.getColor(R.color.red)
}.lparams{
margin=dip(10)
height= dip(40)
width= matchParent
}
val name = editText("EditText")
button("Button") {
onClick { view ->
toast("Hello, ${name.text}!")
}
}
}
}
}
}
然后在onCreate方法中加一句代码,即可创建我们的布局页面了。如下
[代码]java代码:
UI().setContentView(this@LoginActivity)
现在我们编译运行,发现效果和布局文件写的界面是一样的。但是它的性能是有优势的,其实吧并没有发觉性能优势。不管怎样,这种DSL确实便于阅读,也很容易上手,在上面的代码中,你可能注意到了dip(10),它表示将10dp转换为像素的意思,是Anko的扩展函数,说的扩展函数,如果阅读过Anko的源码我们发现里面大量的使用扩展函数,这也是Kotlin语言的优势之一。确实很强大,例如dip扩展(摘取View扩展)
[代码]java代码:
inline fun View.dip(value: Int): Int = context.dip(value)
fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
在上面resources.displayMetrics.density和我们Java getResources().getDisplayMetrics().density是一个效果,不过看着你会不会感觉比Java书写舒服多了,反正我是这么感觉的。在上面的我们给Button加了一个点击事件,我们发现它支持lambda表达式。我们想显示一个Toast,只需要toast("内容")就可以了,是不是又很简洁。其实它也是扩展函数,实现
[代码]java代码:
inline fun AnkoContext<*>.toast(message: CharSequence) = ctx.toast(message)
fun Context.toast(message: CharSequence) = Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
当然创建dialog依然也很简单,如下
[代码]java代码:
alert ("我是Dialog"){
yesButton { toast("yes")}
noButton { toast("no")}
}.show()
[代码]java代码:
doAsync {
//后台执行代码
uiThread {
//UI线程
toast("线程${Thread.currentThread().name}") }
}
如果你使用Kotlin开发Android一段时间后,会发现它给我们减少了很多的代码量,当然更多的优势及用法需要我们自己去探索。相信经过探索后它会让你大吃一惊。
[代码]xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fitsSystemWindows="true" > <RelativeLayout android:id="@+id/login_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="center" > <FrameLayout android:id="@+id/username_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="55dp" android:gravity="center" > <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="@dimen/default_edittext_height" android:layout_marginTop="5dp" android:inputType="number" android:paddingRight="60dp" android:maxLength="20" android:paddingLeft="55dp" > </EditText> <ImageView android:layout_width="22dp" android:layout_height="21dp" android:layout_marginStart="8dp" android:layout_gravity="left|center_vertical" android:background="@drawable/login_usr_in_img" android:visibility="visible" /> <TextView android:id="@+id/contry_sn" android:layout_width="40dp" android:layout_height="50dp" android:layout_gravity="left|center_vertical" android:layout_marginTop="4dp" android:gravity="center" android:text="+62" android:textColor="@android:color/black" android:textSize="18sp" android:visibility="invisible" /> <Button android:id="@+id/bt_username_clear" android:layout_width="23dp" android:layout_height="23dp" android:background="@drawable/button_clear" android:layout_gravity="right|center_vertical" android:layout_marginRight="10dp" android:visibility="invisible" /> </FrameLayout> <FrameLayout android:id="@+id/usercode_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/username_layout" android:layout_marginTop="6dp" android:gravity="center" > <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="@dimen/default_edittext_height" //40 android:inputType="textPassword" android:paddingRight="60dp" android:maxLength="20" android:paddingLeft="55dp" > </EditText> <ImageView android:layout_width="24dp" android:layout_height="22dp" android:layout_marginStart="7dp" android:layout_gravity="left|center_vertical" android:background="@drawable/login_code_in_img" /> <Button android:id="@+id/bt_pwd_eye" android:layout_width="23dp" android:layout_height="23dp" android:background="@drawable/button_eye_n" android:layout_gravity="right|center_vertical" android:layout_marginRight="10dp" /> <Button android:id="@+id/bt_pwd_clear" android:layout_width="23dp" android:layout_height="23dp" android:background="@drawable/button_clear" android:visibility="invisible" android:layout_gravity="right|center_vertical" android:layout_marginRight="33dp" /> </FrameLayout> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="@dimen/default_button_height" android:layout_below="@id/usercode_layout" android:layout_marginTop="30dp" android:background="#ff336699" android:textColor="@android:color/white" android:gravity="center" android:text="登录" /> <Button android:id="@+id/login_error" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/login" android:layout_below="@id/login" android:background="#00000000" android:text="忘记密码" android:textSize="16sp" /> <Button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/login" android:layout_below="@id/login" android:background="#00000000" android:gravity="left|center_vertical" android:text="注册" android:textSize="16sp" android:visibility="visible" /> </RelativeLayout> <RelativeLayout android:id="@+id/remember_layout000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="1dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" > </RelativeLayout> </RelativeLayout>
[代码]java代码:
lateinit var et_account: EditText
lateinit var et_password: EditText
inner class LoginUi : AnkoComponent {
override fun createView(ui: AnkoContext) = with(ui) {
verticalLayout {
backgroundColor = context.resources.getColor(android.R.color.white)
gravity = Gravity.CENTER_HORIZONTAL
imageView(R.mipmap.ic_launcher).lparams {
width = dip(100)
height = dip(100)
topMargin = dip(64)
}
linearLayout {
gravity = Gravity.CENTER_VERTICAL
orientation = HORIZONTAL
backgroundResource = R.drawable.bg_frame_corner
imageView {
image = resources.getDrawable(R.mipmap.ic_username)
}.lparams(width = wrapContent, height = wrapContent) {
leftMargin = dip(12)
rightMargin = dip(15)
}
et_account = editText {
hint = "登录账户"
hintTextColor = Color.parseColor("#666666")
textSize = 16f
background = null
}
}.lparams(width = dip(300), height = dip(40)) {
topMargin = dip(45)
}
linearLayout {
orientation = HORIZONTAL
backgroundResource = R.drawable.bg_frame_corner
gravity = Gravity.CENTER_VERTICAL
imageView {
image = resources.getDrawable(R.mipmap.ic_password)
}.lparams {
leftMargin = dip(12)
rightMargin = dip(15)
}
et_password = editText {
hint = "登录密码"
hintTextColor = Color.parseColor("#666666")
textSize = 16f
background = null
}
}.lparams {
width = dip(300)
height = dip(40)
topMargin = dip(10)
}
button("登录") {
gravity = Gravity.CENTER
background = resources.getDrawable(R.drawable.bg_login_btn)
textColor = Color.parseColor("#ffffff")
onClick {
if (et_account.text.toString().isNotEmpty() && et_password.text.toString().isNotEmpty())
startActivity() else toast("请输入账户或者密码")
}
}.lparams(width = dip(300), height = dip(44)) {
topMargin = dip(18)
}
linearLayout {
orientation = HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
checkBox("注册") {
textColor = Color.parseColor("#666666")
textSize = 16f
leftPadding = dip(5)
}
textView("忘记密码") {
textColor = Color.parseColor("#1783e3")
gravity = Gravity.RIGHT
textSize = 16f
}.lparams(width = matchParent)
}.lparams(width = dip(300)) {
topMargin = dip(18)
}
textView("Copyright © Code4Android") {
textSize = 14f
gravity = Gravity.CENTER or Gravity.BOTTOM
}.lparams {
bottomMargin = dip(35)
weight = 1f
}
}
}
}
看到上面的代码怎么样,看起来还不错吧,即使现在你不会写,但是你也能读懂它。在上面我们给登录按钮设置一个打开MainActivity的事件。
[代码]java代码:
startActivity("account" to et_account.text.toString(),"password" to et_password.text.toString())