Navigation Compopent的使用之fragment之间Bundle数据传递

1、发送string和long型数据

val bundle = bundleOf(
                        "name" to firstName,
                        "mobile" to mobile.toLong()
                    )
findNavController()
         .navigate(R.id.action_exampleFragment_to_verifyFragment,
          bundle)

2、获取string和long型数据

 val name = arguments?.getString("name")
 val mobileNumber = arguments?.getLong("mobile")

运行结果:

输入数据,点击按钮界面跳转时发送数据

数据显示在第二个界面:

注:

第一个界面:

package com.example.mykotlin2

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.core.os.bundleOf
import androidx.navigation.fragment.findNavController


class EnterDetailsFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val rootView = inflater.inflate(R.layout.fragment_enter_details, container, false)

        val etName = rootView.findViewById<EditText>(R.id.et_first_name)
        val etMobileNumber = rootView.findViewById<EditText>(R.id.et_first_mobile)


        val btVerifyDetails = rootView.findViewById<Button>(R.id.btn_verify_details)
        btVerifyDetails.setOnClickListener{

            val firstName = etName.text.toString()
            val mobile = etMobileNumber.text.toString()

            when{
                firstName.isEmpty() -> {
                    Toast.makeText(activity,"Enter Name.",Toast.LENGTH_SHORT).show()
                }

                mobile.isEmpty() -> {
                    Toast.makeText(activity,"Enter Mobile.",Toast.LENGTH_SHORT).show()
                }

                else -> {
                    val bundle = bundleOf(
                        "name" to firstName,
                        "mobile" to mobile.toLong()
                    )
                    findNavController()
                        .navigate(R.id.action_exampleFragment_to_verifyFragment,
                        bundle)
                }
            }


//            findNavController().navigate(R.id.action_exampleFragment_to_verifyFragment)
        }

        return rootView
    }

}

 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16sp"
    tools:context=".EnterDetailsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:gravity="center_horizontal"
        android:text="Enter your details below :"
        android:textColor="#212121"
        android:textSize="22sp"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_name"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:hint="Name">

        <EditText
            android:id="@+id/et_first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>

    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_mobile"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:hint="MobileNumber">

        <EditText
            android:id="@+id/et_first_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>

    </com.google.android.material.textfield.TextInputLayout>


    <Button
        android:id="@+id/btn_verify_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="VERIFY DETAILS"/>

</LinearLayout>

第二个fragment:

package com.example.mykotlin2

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.core.os.bundleOf
import androidx.navigation.fragment.findNavController


class VerifyDetailsFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        // Inflate the layout for this fragment
        val rootView = inflater.inflate(R.layout.fragment_verify_details, container, false)

        val etName = rootView.findViewById<EditText>(R.id.et_first_name)
        val etMobileNumber = rootView.findViewById<EditText>(R.id.et_first_mobile)


        val name = arguments?.getString("name")
        val mobileNumber = arguments?.getLong("mobile")

        etName.setText(name)
        etMobileNumber.setText(mobileNumber.toString())


        return rootView
    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16sp"
    tools:context=".EnterDetailsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:gravity="center_horizontal"
        android:text="Verify details below :"
        android:textColor="#212121"
        android:textSize="22sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="Name"
        android:layout_margin="8dp"
        android:textColor="#212121"
        android:textSize="22sp"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_name"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:hint="Name">

        <EditText
            android:id="@+id/et_first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>

    </com.google.android.material.textfield.TextInputLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="MobileNumber"
        android:layout_margin="8dp"
        android:textColor="#212121"
        android:textSize="22sp"/>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_mobile"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:hint="Name">

        <EditText
            android:id="@+id/et_first_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>

    </com.google.android.material.textfield.TextInputLayout>


    <Button
        android:id="@+id/btn_verify_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="VERIFY DETAILS"/>

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值