第一行Kotlin系列(二)Intent隱式顯式跳轉及向下傳值


1.Intent顯式跳轉頁面

val button5 = findViewById<Button>(R.id.mButton5)
 
button5.setOnClickListener {
            val intent = Intent()
            intent.setClass(this, ThirdActivity::class.java)
            startActivity(intent)
        }

跳轉方式一

intent.setClass(this, ThirdActivity::class.java)

  // 獲取class是使用::反射

跳轉方式二

intent.setClass(this, ThirdActivity().javaClass)

2.Intent隱式跳轉調用系統撥號

val button6 = findViewById<Button>(R.id.mButton6)
 
button6.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL)
            val url = Uri.parse("tel:10086")
            intent.data = url
            startActivity(intent)
        }

3.Intent跳轉頁面並向下一頁傳值 

val button7 = findViewById<Button>(R.id.mButton7)
 
override fun onClick(v: View?) {
        when (v?.id) {
            R.id.mButton1 ->
                Toast.makeText(this, "java", Toast.LENGTH_LONG).show()
            R.id.mButton2 ->
                Toast.makeText(this, "java", Toast.LENGTH_LONG).show()
            R.id.mButton7 -> {
                val intent = Intent(this@MainActivity, GetIntentData::class.java)
                val bundle = Bundle()
                bundle.putString("text", "Kotlin練習")
                intent.putExtras(bundle)
                startActivity(intent)
            }
        }
    }

注意 使用when 當有多行代碼時使用“{ }”括起來

接收Activity頁面代碼

private fun initView() {
        val bundle = this.intent.extras
        val str = bundle?.get("text").toString()
        val mTvText = findViewById<TextView>(R.id.mTvText)
        mTvText.text = str
    }
mTvText.text = str 相當於java中 mTvText.setText(str)

以上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM