Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

จะส่งค่าระหว่าง Fragments ใน Android ได้อย่างไร?


ตัวอย่างนี้สาธิตวิธีการส่งผ่านค่าระหว่างแฟรกเมนต์ใน Android

ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่

ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml

 <แฟรกเมนต์ android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="app.com.sample .FragmentOne" android:id="@+id/fragment" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" tools:layout="@layout/fragment_fragment_one" /> 

ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java

นำเข้า android.support.v7.app.AppCompatActivity;นำเข้า android.os.Bundle;นำเข้า android.view.Menu;นำเข้า android.view.MenuItem;คลาสสาธารณะ MainActivity ขยาย AppCompatActivity ใช้งาน FragmentOne.OnFragmentInteractionListener{ @Override ป้องกันเป็นโมฆะ onCreate (บันเดิล saveInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); } @แทนที่บูลีนสาธารณะ onCreateOptionsMenu (เมนูเมนู) { getMenuInflater ().inflate (R.menu.menu_main, เมนู); คืนค่าจริง; } @แทนที่บูลีนสาธารณะ onOptionsItemSelected (รายการเมนู) { int id =item.getItemId (); ถ้า (id ==R.id.textUpdate) { คืนค่าจริง; } ส่งคืน super.onOptionsItemSelected(รายการ); } @แทนที่โมฆะสาธารณะ onFragmentInteraction (String userContent) { FragmentTwo fragmentTwo =(FragmentTwo) getSupportFragmentManager ().findFragmentById (R.id.fragment2); fragmentTwo.updateTextField(userContent); }}

ขั้นตอนที่ 4 − สร้างสองส่วน (FragmentOne และ FragmentTwo) และเพิ่มรหัสต่อไปนี้ -

ก) FragmentOne.java

<ก่อนหน้า>นำเข้า android.app.Activity;นำเข้า android.os.Bundle;นำเข้า android.support.v4.app.Fragment;นำเข้า android.view.LayoutInflater;นำเข้า android.view.View;นำเข้า android.view.ViewGroup;นำเข้า android.widget.Button;นำเข้า android.widget.EditText;นำเข้า android.widget.Toast;คลาสสาธารณะ FragmentOne ขยาย Fragment { OnFragmentInteractionListener ส่วนตัว mListener; ส่วนตัว EditText userInput; ข้อมูลผู้ใช้สตริงส่วนตัว; สาธารณะ FragmentOne () { } @ แทนที่มุมมองสาธารณะ onCreateView (LayoutInflater inflater, คอนเทนเนอร์ ViewGroup, Bundle saveInstanceState) { ดูมุมมอง =inflater.inflate (R.layout.fragment_fragment_one, คอนเทนเนอร์, เท็จ); userInput =view.findViewById (R.id.userInput); อัปเดตปุ่ม =view.findViewById (R.id.button); update.setOnClickListener ( View.OnClickListener ใหม่ () { @แทนที่โมฆะสาธารณะ onClick (ดู v) { if (userInput.getText ().toString ().equals ("")) { Toast.makeText (getActivity (), "ผู้ใช้ ต้องกรอกค่าอินพุต", Toast.LENGTH_LONG).show(); return; } userData =userInput.getText().toString(); onButtonPressed(userData); } }); มุมมองย้อนกลับ; } โมฆะสาธารณะ onButtonPressed (สตริง userContent) { ถ้า (mListener !=null) { mListener.onFragmentInteraction (userContent); } } @Override โมฆะสาธารณะ onAttach (กิจกรรมกิจกรรม) { super.onAttach (กิจกรรม); ลอง { mListener =(OnFragmentInteractionListener) กิจกรรม; } จับ (ClassCastException จ) { โยนใหม่ ClassCastException(activity.toString() + " ต้องใช้ OnFragmentInteractionListener"); } } @Override โมฆะสาธารณะ onDetach() { super.onDetach(); mListener =null; } อินเทอร์เฟซสาธารณะ OnFragmentInteractionListener { โมฆะ onFragmentInteraction (String userContent); }}

fragment_fragment_one.xml −

    <ปุ่ม android:id=" @+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/userInput" android:layout_marginTop="20dp" android:padding="16dp " android:elevation="4dp" android:text="อัปเดต" />

ข) FragmentTwo.java

นำเข้า android.os.Bundle;นำเข้า android.support.v4.app.Fragment;นำเข้า android.view.LayoutInflater;นำเข้า android.view.View;นำเข้า android.view.ViewGroup;นำเข้า android.widget.TextView;สาธารณะ คลาส FragmentTwo ขยาย Fragment { TextView ส่วนตัว updateText; FragmentTwo สาธารณะ () { } @ แทนที่มุมมองสาธารณะ onCreateView (LayoutInflater inflater, คอนเทนเนอร์ ViewGroup, Bundle saveInstanceState) { มุมมอง =inflater.inflate (R.layout.fragment_fragment_two, คอนเทนเนอร์, เท็จ); updateText =view.findViewById(R.id.textUpdate); มุมมองย้อนกลับ; } โมฆะสาธารณะ updateTextField (สตริงข้อความใหม่) { updateText.setText (ข้อความใหม่); }}

แฟรกเมนต์_แฟรกเมนต์_สอง −

 

ขั้นตอนที่ 5 − เพิ่มรหัสต่อไปนี้ใน androidManifest.xml

     <หมวดหมู่ android:name=" android.intent.category.LAUNCHER" />   

มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์แล้วคลิก Runicon จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

จะส่งค่าระหว่าง Fragments ใน Android ได้อย่างไร?

จะส่งค่าระหว่าง Fragments ใน Android ได้อย่างไร?