Kontlin边用边学

Kontlin就一点好,从java转过来,可以直接转换,碰到不会的转一转,就可以解决很多的问题

学习的路很长(一点一点积累吧)


   kontlin 学习  :https://kotlinlang.org/docs/reference/classes.html                    //英文的
                              http://www.liying-cn.net/kotlin/docs/reference/lambdas.html   // 中文文档


常量的变化

object Constants {

    public static String API_PATH= "http://120.77.52.183:80/api/webapi/";

    //    测试环境
    var API_PATH = ""

    // 请求的参数配置
    var ACCESS_TOKEN = ""
    var OS_TYPE = "Android"
    var USER_NAME = ""
    var APP_NAME = "WisPowerAndroid"
    var APP_KEY = "1234567890"
    var ACCEPT_STRING = "application/json"



class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initStart()
    }


    fun initStart(){
        val handler = Handler()
        handler.postDelayed(
                {
                    val intent =  Intent()
                    intent.setClass(this,LoginActivity::class.java)
                    startActivity(intent)
                },1000)
    }
}


构造类

class LoginRequest(var userName: String, var password: String, var vId: String, var appVersion: String)
{

     fun GETBizParams(): String
    {
        val str = String.format("username=%s&password=%s&VID=%s&ostype=%s&baidu_userId=%s&baidu_channelId=%s&appversion=%s&appname=%s",
                userName,
                password,
                vId,
                Constants.OS_TYPE,
                Constants.BAIDU_USER_ID,
                Constants.BAIDU_CHANNEL_ID,
                appVersion,
                Constants.APP_NAME)
        return str
    }




    fun getResult(handler: Handler, mCall: AskHttpInterface)
    {
        RequestToolNewEx.PUT(Constants.LOGIN_METHOD, GETBizParams(), null,mCall, handler)
    }
}


抽象类和抽象方法


abstract class BaseActivity : AppCompatActivity() {

    abstract fun initContentView(savedInstanceState: Bundle?)



接口的使用

 interface AskHttpInterface {   // 可拓展  抽象类AskHttpAbstract,请求时使用抽象类

    fun OnSuccessful(jsonStr: String)   // 有响应数据
    fun OnErr(e: Exception)   // 请求异常,具体在抽象类中实现
}

成员变量

(有var 和 val 俩个类型  ;var代表该参数是可以重新赋值的,val 相当于final,是不可以重新赋值的)

var acountStr : String  = ""
var passwordStr : String  = ""
var acountEdit : EditText ? = null
var passwordEdit : EditText ? = null
var sibmitTv : TextView ? = null


findviewbyid(这里使用as来进行类型转换)

acountEdit = findViewById(R.id.account) as EditText
passwordEdit = findViewById(R.id.password) as EditText
sibmitTv = findViewById(R.id.login_submit) as TextView

点击事件


sibmitTv!!.setOnClickListener(
        {
            acountStr = acountEdit!!.text.toString().trim()
            passwordStr = passwordEdit!!.text.toString().trim()
            askLoginHttp()
        })


跳转事件

if (result != null)
{
    var intent = Intent(this@LoginActivity,HomeActivity::class.java)
    this@LoginActivity.startActivity(intent)
}



Gson的使用

var result =  Gson().fromJson<Login_Result>(jsonStr, Login_Result::class.java!!)

匿名内部类

Android 自带的 直接  {  }  就可以了  Runnable为例子

runOnUiThread {
    var result =  Gson().fromJson<Login_Result>(jsonStr, Login_Result::class.java!!)

    if (result != null)
    {
        var intent = Intent(this@LoginActivity,HomeActivity::class.java)
        this@LoginActivity.startActivity(intent)
    }else
    {
        var intent = Intent(this@LoginActivity,HomeActivity::class.java)
        this@LoginActivity.startActivity(intent)
    }
}
自定义的(AskHttpInterface为例子)

  request.getResult(handler!!,object : AskHttpInterface{

        override fun OnSuccessful(jsonStr: String){

            runOnUiThread {
                var result =  Gson().fromJson<Login_Result>(jsonStr, Login_Result::class.java!!)

                if (result != null)
                {
                    var intent = Intent(this@LoginActivity,HomeActivity::class.java)
                    this@LoginActivity.startActivity(intent)
                }else
                {
                    var intent = Intent(this@LoginActivity,HomeActivity::class.java)
                    this@LoginActivity.startActivity(intent)
                }
            }

        }

        override fun OnErr(e: Exception) {

        }
    })
}


handler的使用  (这里是使用的when)

protected var handler : AskExceptionHandler? =null
protected var handler : AskExceptionHandler = AskExceptionHandler()
  inner class AskExceptionHandler : Handler() {  // 请求网络异常处理

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                1  //关闭加载进度条
                -> if (dialog != null && dialog!!.isShowing) {
                    dialog!!.dismiss()
                }
                2  // 返回错误码》= 3000 打印错误码
                -> ToastUtil.toasts(this@BaseActivity, msg.obj as String)
                3  // 返回错误码= 2100 || 2102  返回登录界面
//                -> reBackLoginView(msg.obj as String)
                -> ToastUtil.toasts(this@BaseActivity, msg.obj as String)
            }
        }

    }


后面待续边学边做一下笔记...

你可能感兴趣的:(kontlin)