1.配置Android Studio
a.在Project对应的build.gradle文件中添加如下代码:
buildscript {
ext.kotlin_version ='1.2.30' //kotlin版本
ext.anko_version ='0.8.2' //anko是方便kotlin开发android的库,不需要findById()...
repositories {
jcenter()
google()
}
dependencies {
classpath'com.android.tools.build:gradle:3.1.3'
classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath"org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
b.在module对应的build.gradle文件 中添加代码:
添加插件
apply plugin: 'kotlin-android'
applyplugin:'kotlin-android-extensions'
添加依赖库
implementation"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2.创建工程
使用android3.1.3创建kotlin
勾选红色部分,不需要自己配置了上面信息了
引用了anko,简化了好多工作,例如:
按钮的点击事件:
buttonID.onClick { ... }
intent跳转:
startActivity<类名>("key" to value, "key1" to "value1")
打开浏览器:
browse("https://www.baidu.com")
分享:
share("分享", "subject")
发邮件:
email("邮箱地址", "subject", "text")
Toast:
toast("Hello world")
toast(R.string.xx)
longToast("Hello world")
对话框:
alert("内容", "标题") {
positiveButton("确定") {... }
negativeButton("取消") { ... }
}.show()
kotlin实践
如图:
使用kotlin语言实现这个设置界面,首先 创建一个实现 AnkoComponent接口的类
class SettingUI :AnkoComponent<SettingAct>{
override fun createView(ui: AnkoContext<SettingAct>): View = with(ui) {
verticalLayout {
backgroundColor = Color.parseColor("#1c1e21")
relativeLayout {
backgroundColor = Color.BLACK
textView("设置"){
textColor = Color.WHITE
textSize = 20f
}.lparams(wrapContent, wrapContent){
centerInParent()
centerVertically()
}
imageView(R.mipmap.ic_back).lparams(wrapContent, wrapContent){
alignParentLeft()
centerVertically()
}
}.lparams(matchParent, dip(58)) {
setMargins(0,0,0,10)
}
relativeLayout {
backgroundColor = Color.parseColor("#1c1e21")
textView("单位设置"){
textColor = Color.WHITE
textSize = 16f
}.lparams(wrapContent, wrapContent){
centerVertically()
setMargins(dip(10),0,0,0)
}
imageView(R.mipmap.arrow_gray).lparams(wrapContent, wrapContent){
alignParentRight()
centerVertically()
}
onClick {
toast("点击了单位设置")
}
view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){
setMargins(dip(10),0,0,0)
alignParentBottom()
}
}.lparams(matchParent, dip(48))
relativeLayout {
backgroundColor = Color.parseColor("#1c1e21")
var weeklyTv = textView("星期开始日"){
textColor = Color.WHITE
textSize = 16f
}.lparams(wrapContent, wrapContent){
centerVertically()
setMargins(dip(10),0,0,0)
}
textView("周日"){
textColor = Color.WHITE
textSize = 13f
val rightDrawable = ctx.resources.getDrawable(R.mipmap.arrow_gray)
rightDrawable.setBounds(0,0,rightDrawable.minimumWidth,rightDrawable.minimumHeight);
setCompoundDrawables(null,null,rightDrawable,null)
compoundDrawablePadding = dip(3)
}.lparams(wrapContent, wrapContent){
centerVertically()
alignParentRight()
}
onClick {
toast(weeklyTv.text)
}
view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){
setMargins(dip(10),0,0,0)
alignParentBottom()
}
}.lparams(matchParent, dip(48))
relativeLayout {
backgroundColor = Color.parseColor("#1c1e21")
textView("账号管理"){
textColor = Color.WHITE
textSize = 16f
}.lparams(wrapContent, wrapContent){
centerVertically()
setMargins(dip(10),0,0,0)
}
imageView(R.mipmap.arrow_gray).lparams(wrapContent, wrapContent){
alignParentRight()
centerVertically()
}
onClick {
toast("账号管理")
}
view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){
setMargins(dip(10),0,0,0)
alignParentBottom()
}
}.lparams(matchParent, dip(48))
relativeLayout {
backgroundColor = Color.parseColor("#1c1e21")
var weeklyTv = textView("APP版本"){
textColor = Color.WHITE
textSize = 16f
}.lparams(wrapContent, wrapContent){
centerVertically()
setMargins(dip(10),0,0,0)
}
textView("v.${BuildConfig.VERSION_NAME}"){
textColor = Color.WHITE
textSize = 13f
val rightDrawable = ctx.resources.getDrawable(R.mipmap.arrow_gray)
rightDrawable.setBounds(0,0,rightDrawable.minimumWidth,rightDrawable.minimumHeight);
setCompoundDrawables(null,null,rightDrawable,null)
compoundDrawablePadding = dip(3)
}.lparams(wrapContent, wrapContent){
centerVertically()
alignParentRight()
}
onClick {
toast(weeklyTv.text)
}
view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){
setMargins(dip(10),0,0,0)
alignParentBottom()
}
}.lparams(matchParent, dip(48))
button("退出登录"){
textColor = Color.WHITE
textSize = 20f
backgroundColor = Color.parseColor("#ff1353")
}.lparams(dip(300),dip(50)){
setMargins(0,dip(10),0,0)
setGravity(Gravity.CENTER_HORIZONTAL)
}
}
}
}
在SettingAct中调用:
class WidgetsAct : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
SettingUI().setContentView(this)
}
}