如何在Android中使用Kotlin將變數從Activity傳送到Fragment?
本例演示瞭如何在Android中使用Kotlin將變數從Activity傳送到Fragment。
步驟1 − 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。
步驟2 − 將以下程式碼新增到res/layout/activity_main.xml。
<?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:gravity="center" android:orientation="vertical" android:padding="2dp" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20sp" android:hint="ENTER YOUR MESSAGE" /> <Button android:id="@+id/btnSendData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send data" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout>
步驟3 − 將以下程式碼新增到src/MainActivity.kt
import android.os.Bundle import android.widget.Button import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentTransaction class MainActivity : AppCompatActivity() { lateinit var editText: EditText lateinit var button: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" val fragmentManager: FragmentManager = supportFragmentManager val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction() val myFragment = MyFragment() button = findViewById(R.id.btnSendData) editText = findViewById(R.id.editText) button.setOnClickListener { val bundle = Bundle() bundle.putString("message", editText.text.toString()) myFragment.arguments = bundle fragmentTransaction.add(R.id.frameLayout, myFragment).commit() } } }
步驟4 − 建立一個新的空白Fragment並新增以下程式碼
MyFragment.kt
import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.Fragment class MyFragment : Fragment() { private lateinit var textView:TextView override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view: View = inflater.inflate(R.layout.fragment_my, container, false) textView = view.findViewById<View>(R.id.textView) as TextView val bundle = arguments val message = bundle!!.getString("message") textView.text = message return view } }
fragment_my.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:padding="4dp"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@android:color/holo_orange_dark" android:textSize="36sp" android:textStyle="bold" /> </LinearLayout>
步驟5 − 將以下程式碼新增到androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案的其中一個活動檔案,然後點選執行圖示 (工具欄中的圖示)。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕。
廣告