初学- 记录下
https://www.cnblogs.com/MyXcc/p/12881253.html
login.setOnClickListener{ println("----点击登录----") var user:String = userInput.text.toString() var pwd:String = pwdInput.text.toString() var str:String = "用户名:$user, 密码:$pwd " Toast.makeText(applicationContext, str, Toast.LENGTH_SHORT).show() // 页面跳转 var intent = Intent() intent.setClass(this,HomeActivity().javaClass) val bundle = Bundle() // 传参 bundle.putString("text", "Kotlin练习跳转传值") intent.putExtras(bundle) startActivity(intent) }
传参简写:
// 页面跳转 HomeActivity:目标页面Activity
var intent: Intent = Intent(this, HomeActivity().javaClass)
// 传参
intent.putExtra("text", "Kotlin练习跳转传值")
startActivity(intent)
参数接收:
class HomeActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_home) // 参数接收 val bundle = this.intent.extras
// 给TextView 赋值 homeTitle.text = bundle?.get("text").toString() } }
Activity 需要在 AndroidManifest.xml中注册
Fragment 跳转 Activity 方法
override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState); listViews.setOnClickListener{ // 页面跳转 var intent: Intent = Intent(this.getActivity(), Page1List().javaClass) startActivity(intent) } }
this.getActivity() 是获取上下文 context