kotlin学习笔记之——写一个完整类

 
  

删除不掉的div字段

 
  
 
  
 
  
Kotlin 的类怎么写?
Kotlin怎么继承一个类?
Kotlin 怎么描述跳转方法launch()?
Kotlin怎么利用intent传值?
Kotlin怎么获取EditText的值?

以下一个完整的类基本回答了上面的问题:

class AddUserDeliveryRemarkActivity : BaseNotifityActivity() { //类写法  继承:后面的类

    companion object {//跳转方法  传值
        fun launch(context: Context, user_id: Int?, order_id: Int?) {
            val intent = Intent(context, AddUserDeliveryRemarkActivity::class.java)
            intent.putExtra("user_id", user_id)
            intent.putExtra("order_id", order_id)
            context.startActivity(intent)
        }
    }

    private var lableAdapter: UserDeliveryRemarkLableAdapter? = null
    private var recyclerViewLable: RecyclerView? = null
    private val mData = ArrayList()
    private var score = 5

    override fun getContentLayout(): Int {
        return R.layout.activity_add_user_delivery_remark
    }

    override fun initView() {
        var lable = this.resources.getStringArray(R.array.arrayUserDeliveryRemarkList)//获取string资源文件
        for (i in lable.indices) {
            val bean = HomeMeunBean(lable[i])
            mData.add(bean)
        }
        recyclerViewLable = findViewById(R.id.recyclerViewLable) as RecyclerView
        recyclerViewLable?.layoutManager = GridLayoutManager(mContext, 3)
        lableAdapter = UserDeliveryRemarkLableAdapter()
        recyclerViewLable?.adapter = lableAdapter
        lableAdapter!!.setNewData(mData)

        //确定按钮
        val btnSubmit = findViewById(R.id.btnSubmit)
        val etContent = findViewById(R.id.etContent) as EditText

        //Adapter 点击事件
        lableAdapter?.setOnItemClickListener { adapter, view, position ->
            lableAdapter?.setPosition(position)
            score = mData.size - position
        }

        //按钮 点击事件
        btnSubmit.setOnClickListener{
            addUserDeliveryRemark(score, etContent.text.toString())//获取edittext值
        }
    }

    /**
     * 添加用户备注
     */
    private fun addUserDeliveryRemark(score: Int, delivery_remark: String) {
        showLoading()
        mCall = AppData.addUserDeliveryRemark(intent.getIntExtra("order_id", 0), intent.getIntExtra("user_id", 0), score, delivery_remark, object : CallBackAdapter {

            override fun onSuccess(`object`: String?) {
                ToastUtils.showShort("添加成功")
                finish()
            }

            override fun onFailure(code: Int, message: String?) {
                ToastUtils.showShort(message)
            }

            override fun onCompleted() {
                cancelLoading()
            }
        })
    }

    /**
     * 活动回收前应该收起键盘
     */
    override fun finish() {
        KeyboardUtils.hideSoftInput(this)
        super.finish()
    }

}





你可能感兴趣的:(Kotlin学习记录)