Android Kotlin学习(一)

来源:互联网 发布:linux 复制文件夹命令 编辑:程序博客网 时间:2024/05/21 08:45
 kotlin推出已经很久,之前谷歌宣称kotlin为android开发官方语言时,了解了一点点,最近项目也许会用到kotlin,所以现在学习学习。我使用的开发工具是Android Studio 3.0,不需要自己集成kotlin.首先暂时做一些简单的,一些语法在需要用的时候再去学习,我觉得这样记忆更深刻些1.对控件的实例化2.点击事件3.页面跳转4.toast消息5.函数(方法)6.判断,运算符我做了一个登录页的布局,一般app第一步是登录注册。布局文件login.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"        android:layout_gravity="center"        android:layout_marginTop="30dp"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:background="@null"        android:layout_marginTop="40dp"        android:paddingLeft="30dp"        android:textColor="@color/colorPrimary"        android:textSize="16sp"        android:hint="请输入电话号码"        android:phoneNumber="true"        android:maxLength="11"        android:id="@+id/et_mobile"        />    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:src="@color/colorPrimary"        android:layout_marginTop="5dp"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:background="@null"        android:layout_marginTop="20dp"        android:paddingLeft="30dp"        android:textColor="@color/colorPrimary"        android:textSize="16sp"        android:hint="请输入密码"        android:id="@+id/et_pwd"        android:password="true"        />    <ImageView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:src="@color/colorPrimary"        android:layout_marginTop="5dp"        />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp">        <TextView            android:id="@+id/tv_fot_pwd"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="忘记密码"            android:textSize="14sp"/>        <TextView            android:id="@+id/tv_register"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="立即注册"            android:textSize="14sp"            android:layout_alignParentRight="true"/>    </RelativeLayout>    <TextView        android:id="@+id/tv_login"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登录"        android:paddingRight="36dp"        android:paddingLeft="36dp"        android:paddingTop="8dp"        android:paddingBottom="8dp"        android:background="@color/colorAccent"        android:textSize="16sp"        android:textColor="@color/cr_white"        android:layout_gravity="center_horizontal"        android:layout_marginTop="30dp"        android:gravity="center"        /></LinearLayout>
效果图

login

接着,新建一个LoginActivity类====(注意是kotlin class)
class LoginActivity : AppCompatActivity() {    //定义控件    var et_mobile:EditText?=null    var et_pwd:EditText?=null    lateinit var tv_fot_pwd:TextView//加上 lateinit  不用每次判断是否为null  lateinit 是用来告诉编译器,这个变量后续会妥善处置的。    var tv_register:TextView?=null//每次调用要判断null  (调用时强制[加上?])    var tv_login:TextView?=null    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.login)        initViews()        initListener()    }    /***实例化控件***/    private fun initViews(){        et_mobile = findViewById(R.id.et_mobile) as EditText        et_pwd = findViewById(R.id.et_pwd) as EditText        tv_fot_pwd = findViewById(R.id.tv_fot_pwd) as TextView        tv_register = findViewById(R.id.tv_register) as TextView        tv_login = findViewById(R.id.tv_login) as TextView    }    /***点击事件***/    private fun initListener() {        /***        * 使用了lateinit关键字 不用?判断        * ====> tv_fot_pwd.setonclickListener( new OnclickListener(){ .....})        **/         tv_fot_pwd.setOnClickListener{        }         tv_login?.setOnClickListener{            var mobile = et_mobile?.text.toString()            var pwd = et_pwd?.text.toString()            if((mobile!= "") and (pwd!="")){                Toast.makeText(this, "login success !" , Toast.LENGTH_SHORT).show()            }else{                Toast.makeText(this, "login fail !" , Toast.LENGTH_SHORT).show()            }        }        tv_register?.setOnClickListener(onClickListener)    }    var onClickListener : View.OnClickListener  = View.OnClickListener {        //跳转        val intent = Intent()        //获取intent对象        intent.setClass(this, RegisterActivity::class.java)        // 获取class是使用::反射        startActivity(intent)    }}
现在用到的知识,一个一个的分析1.修饰符   在kotlin中,默认的修饰符为public,所以在页面开头是class LoginActivity.... 省略了public   kotlin有四个修饰符 :public、internal、protected、private2.控件的定义及实例化 (1)定义  var tv_login:TextView?=null //var声明可变属性 ,还有一种为val为只读属性  lateinit var tv_fot_pwd:TextView //加上lateinit关键字 后面不用每次对null处理(2)实例化  tv_login = findViewById(R.id.tv_login) as TextView //控件类型放到后面3.函数  fun initViews (){    //内容     } 使用fun关键字进行定义函数,initViews为函数名称 定义一个有参数的函数  fun add(x:Int,y:Int){}//定义了两个参数,类型为Int名为x和类型为Int名为y的参数的函数 定义个有返回值的函数 fun getMax(x:Int,y:Int) : Int{    return x+y }//定义了两个参数,类型为Int名为x和类型为Int名为y的参数返回值类型为Int的函数4.控件点击事件 有两种方法实现点击事件       1. tv_login?.setOnClickListener{}  ====>(类似于Android)  tv_login?.setOnclickListener(new OnClickListener(){.......})       2. tv_login?.setOnClickListener(onClickListener); ====>(类似于Android点击事件通过内部类的实现方式)         var onClickListener : View.OnClickListener  = View.OnClickListener {               //内容        }5.运算符代码中用到关于两个EditText文本的非null判断        var mobile = et_mobile?.text.toString()//获取EditText控件        var pwd = et_pwd?.text.toString()        if((mobile!= "") and (pwd!="")){            Toast.makeText(this, "login success !" , Toast.LENGTH_SHORT).show()        }else{            Toast.makeText(this, "login fail !" , Toast.LENGTH_SHORT).show()        }        java语法是这样的        if(!mobile.equals("") && !pwd.equals("")){}        kotlin将 “&&” 使用 and         !mobile.equeals("") :在kotlin中这样写不会报错,会提出警告让你改成为!=运算符    6.Toast弹出消息和之前一致        定义一个函数        fun toast(context:Context, message:String, length:Int=Toast.LENGTH_SHORT){             Toast.makeText(context, message , length).show()        }        我们在调用时        //参数全传        toast(this, "toast消息" , Toast.LENGTH_LONG)        //传部分参数        toast(this, "toast消息" )//这样调用并不会报错,原因是 在定义函数时,length这个参数已经初始化  7.页面跳转           //跳转  val intent = Intent()  //获取intent对象  intent.setClass(this, RegisterActivity::class.java) // 获取class是使用::反射 startActivity(intent)  今天就暂时到这里了,下次继续。
原创粉丝点击