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

จะส่งข้อมูลจากส่วนย่อยหนึ่งไปยังส่วนอื่นใน Android ได้อย่างไร


ตัวอย่างนี้สาธิตวิธีการส่งข้อมูลจากแฟรกเมนต์หนึ่งไปยังอีกแฟรกเมนต์ใน Android

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

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

  <แฟรกเมนต์ android:name ="com.example.myapplication.FirstFragment" android:id ="@+id/frag_1" android:layout_width ="fill_parent" แอนดรอยด์:layout_height ="fill_parent" />   <แฟรกเมนต์ android:name ="com.example.myappli cation.SecondFragment" android:id ="@+id/frag_2" android:layout_width ="fill_parent" android:layout_height ="fill_parent" /> 

ในโค้ดด้านบนนี้ เราได้นำ Fragment มาส่งข้อมูลระหว่าง Fragment สองส่วน

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

<รุ่นก่อนหน้า>นำเข้า android.os.Bundle;นำเข้า android.support.v4.app.FragmentActivity; MainActivity คลาสสาธารณะขยาย FragmentActivity ใช้ OnButtonPressListener { @Override public void onCreate (บันเดิลที่บันทึกไว้InstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); } @ แทนที่โมฆะสาธารณะ onButtonPressed (String msg) { // TODO วิธีสร้างอัตโนมัติ stub SecondFragment Obj =(SecondFragment) getSupportFragmentManager ().findFragmentById (R.id.frag_2); Obj.onFragmentInteraction(msg); }}

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

<รุ่นก่อนหน้า>นำเข้า android.annotation.SuppressLint;นำเข้า android.app.Activity;นำเข้า android.content.Context;นำเข้า android.os.Bundle;นำเข้า android .support.annotation.NonNull นำเข้า android.support.annotation.Nullable นำเข้า android.support.v4.app.Fragment นำเข้า android.view.LayoutInflater นำเข้า android.view.View นำเข้า android.view.ViewGroup นำเข้า android .widget.TextView คลาสสาธารณะ FirstFragment ขยาย Fragment { @Override public View onCreateView (LayoutInflater inflater, คอนเทนเนอร์ ViewGroup, Bundle saveInstanceState) { ViewGroup root =(ViewGroup) inflater.inflate (R.layout.fragment, null); init(รูท); ส่งคืนรูต; } OnButtonPressListener buttonListener; @แทนที่โมฆะสาธารณะ onAttach (กิจกรรมกิจกรรม) { super.onAttach (กิจกรรม); ลอง { buttonListener =(OnButtonPressListener) getActivity(); } จับ (ClassCastException จ) { โยนใหม่ ClassCastException(activity.toString() + " ต้องใช้ onButtonPressed"); } } เป็นโมฆะ init (รูทของ ViewGroup) { TextView แต่ =(TextView) root.findViewById (R.id.text); but.setOnClickListener (View.OnClickListener ใหม่ () { @Override public void onClick (View v) { // TODO วิธีสร้างอัตโนมัติ stub buttonListener.onButtonPressed ("ข้อความจากส่วนแรก"); } }); }}

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

<รุ่นก่อนหน้า>นำเข้า android.annotation.SuppressLint;นำเข้า android.os.Bundle;นำเข้า android.support.annotation.NonNull;นำเข้า android.support.annotation Nullable;นำเข้า android.support.v4.app.Fragment;นำเข้า android.util.Log;นำเข้า android.view.LayoutInflater;นำเข้า android.view.View;นำเข้า android.view.ViewGroup;นำเข้า android.widget.TextView;คลาสสาธารณะ SecondFragment ขยาย Fragment { TextView textView; ดูมุมมอง; @Nullable @Override สาธารณะ มุมมอง onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup คอนเทนเนอร์, @Nullable Bundle ที่บันทึกไว้InstanceState) { view =inflater.inflate (R.layout.fragment, คอนเทนเนอร์, เท็จ); มุมมองย้อนกลับ; } โมฆะสาธารณะ onFragmentInteraction(String uri) { Log.d("sai",uri); textView =view.findViewById (R.id.text); textView.setText(uri); }}

ขั้นตอนที่ 6 − เพิ่มโค้ดต่อไปนี้ใน res/layout/ fragment.xml

 

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

อินเทอร์เฟซสาธารณะ OnButtonPressListener { โมฆะสาธารณะ onButtonPressed (String msg);}

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

จะส่งข้อมูลจากส่วนย่อยหนึ่งไปยังส่วนอื่นใน Android ได้อย่างไร

ตอนนี้คลิกที่มุมมองข้อความส่วนแรก มันจะแสดงผลลัพธ์ดังที่แสดงด้านล่าง –

จะส่งข้อมูลจากส่วนย่อยหนึ่งไปยังส่วนอื่นใน Android ได้อย่างไร