zoukankan      html  css  js  c++  java
  • 第一行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)

    以上

  • 相关阅读:
    java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä'.. 问题解决方法
    Mysql存储引擎federated
    实习心语
    Linux版本CentOS、Ubuntu和Debian的异同
    Ubuntu忘记MySQL密码重设方法
    运行时异常和一般异常
    网络爬虫-正方教务系统登录
    大四心语
    缓存更新的套路
    (String)、toString、String.valueOf的区别
  • 原文地址:https://www.cnblogs.com/MyXcc/p/12881253.html
Copyright © 2011-2022 走看看