美文网首页
Kotlin for Android

Kotlin for Android

作者: smallgrey | 来源:发表于2019-07-24 10:26 被阅读0次

    Kotlin Android 环境搭建
    打开 Settings ( Mac 为 Preferences) 面板,在右侧找到 Plugins 选项 (快捷键 Ctrl+, Mac 下为 command+),搜索框输入 "Kotlin" 查找,
    点击 Search in repositories(仓库中搜索),然后安装即可,安装完成之后需要重启 Android Studio。
    参照:http://www.runoob.com/kotlin/otlin-android-setup.html
    Button的点击事件
    在Kotlin中when替代了原来的switch
    例:

        when (v!!.id){      
                        R.id.gm02_back -> {     
                            //返回上一个页面       
                            finish()        
                        }       
                        R.id.gm02_add -> {      
                            toast("add")        
                
                            var count = db.queryGoods()!!.size      
                            var id = count.toString()       
                            var goodsModel = GoodsModel(id,"name","info","category")        
                            goodsList.add(goodsModel)       
                            db.saveGoods(goodsList)     
                            toast(db.queryGoods()!!.size.toString())        
                        }       
                        R.id.gm02_refresh -> {      
                            toast("refresh")        
                            var count = db.queryGoods()!!.size      
                            var id = count.toString()       
                            toast(db.queryGoods()!!.size.toString())        
                        }       
                        R.id.gm02_edit -> {     
                            toast("edit")       
                        }       
                        else -> {       
                
                        }       
                    }       
    

    用Intent传值与跳转页面
    例:

        var gm02intent =  Intent(this@GM01Activity,GM02Activity().javaClass)        
        gm02intent.putExtra("username",userName)        
        gm02intent.putExtra("password",password)        
        startActivity(gm02intent)       
    

    用Intent接收数据
    例:

        username = intent.getStringExtra("username")        
        password = intent.getStringExtra("password")        
    

    字符串判断为null或者是空
    例:

        var userName:Stirng? = null     
        userName.isNullOrEmpty()        
    

    用Retrofit第三方进行网络请求
    Get例:
    1 在app build.grade中配置文件

          dependencies {  
              …   
              //Retrofit库 
              implementation 'com.squareup.retrofit2:retrofit:2.4.0'  
              // Okhttp库  
              implementation 'com.squareup.okhttp3:okhttp:3.11.0' 
              // 用Gson解析json的转换器  
              implementation 'com.squareup.retrofit2:converter-gson:2.0.2'    
          }   
    

    2 创建request文件(interface)
    3 创建response文件,response中的属性要与api中返回的内容一一对应,因为是用Gson解析的,所以api的返回值要做成键值对类型的
    4 Retrofit调用
    例:

                private fun request(){  
                    //创建Retrofitt对象 
                    val retrofit = Retrofit.Builder()   
                            .baseUrl("http://172.23.17.179:8080/")      
                            .addConverterFactory(GsonConverterFactory.create())   //添加Gson的解析方式,在build.gradle中有添加   
                            .build()    
                
                    //创建网络请求接口 的实例  
                    val request = retrofit.create(LoginRequestInterface::class.java)    
                
                    //对发送请求 进行封装    
                    val call = request.getCall(userName,password)    //调用request请求传递参数  
                
                    //发送网络请求(异步)    
                    call.enqueue(object : Callback<LoginResponse> { 
                        //请求成功时回调   
                        override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) { 
                            //处理返回的数据结果 
                            if (!response.body()?.show().isNullOrEmpty()&&userName.equals("user")&&password.equals("its")){ 
                                var gm02010100intent =  Intent(this@GM01010100Activity,GM02010100Activity().javaClass)  
                                gm02010100intent.putExtra("username",userName)  
                                gm02010100intent.putExtra("password",password)  
                                startActivity(gm02010100intent) 
                            } else {    
                                //用户名和密码不正确,弹出对话框   
                                showOneButtonDialog(this@GM01010100Activity,resources.getString(R.string.GM01010100_6),resources.getString(R.string.GM01010100_7))  
                            }   
                        }   
                
                        override fun onFailure(call: Call<LoginResponse>, t: Throwable) {   
                       //网络请求失败弹出对话框    
                            showOneButtonDialog(this@GM01010100Activity,resources.getString(R.string.GM01010100_6),resources.getString(R.string.GM01010100_5))  
                        }   
                    })  
                }   
    

    相关文章

      网友评论

          本文标题:Kotlin for Android

          本文链接:https://www.haomeiwen.com/subject/mwmtrctx.html