亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將活動 1 /片段 A 中包含的數據傳遞到活動 1 / 片段 B,然后將其傳遞給活動 2

如何將活動 1 /片段 A 中包含的數據傳遞到活動 1 / 片段 B,然后將其傳遞給活動 2

Cats萌萌 2022-09-28 16:06:23
我正在開發一個包含2個活動的應用程序。在我的活動1中,我有一個導航拖曳器,其中包含碎片。我在2個片段中有微調器。我想將片段1的數據和片段2的數據傳遞給片段3(仍然在同一活動中),然后編寫一個按鈕,將片段3中收集的數據發送到活動2。我可以將數據從活動 1 傳遞到活動 2,沒有 Pb,但我不知道如何從片段檢索數據,然后將其傳遞給活動 2。我是片段中的新手....我的一個片段的代碼,包括旋轉器:public static TypeInterventionFragment newInstance() {return (new TypeInterventionFragment()); }    @Override    public View  onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View view= inflater.inflate(R.layout.fragment_type_intervention, container, false);        mSpinner1 = (Spinner) view.findViewById(R.id.spinner1);        mSpinner2 = (Spinner) view.findViewById(R.id.spinner2);        mSpinner3 = (Spinner) view.findViewById(R.id.spinner3);}我想從這些微調器中獲取選定的項目,顯示在包含Textview的片段中,然后通過按按鈕將這些數據傳遞給另一個活動。如果你知道如何做,你可能會救我的生命,哈哈,我從2個月開始就在工作感謝很多和我的法國朋友: Merci les amis si vous pouvez m'aider !!!
查看完整描述

3 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

每當您在活動上實例化片段時,都可以保留指向它們的鏈接。假設您的 ActivityA 和片段創建塊如下所示:


public class ActivityA extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        FragmentTransaction fragmentTransactionA = getSupportFragmentManager().beginTransaction();

        fragmentTransactionA.replace(R.id.yourFragmentAContainerID, new FragmentA(), "fragmentA");

        fragmentTransactionA.commit();

        FragmentTransaction fragmentTransactionB = getSupportFragmentManager().beginTransaction();

        fragmentTransactionB.replace(R.id.yourFragmentBContainerID, new FragmentB(), "fragmentB");

        fragmentTransactionB.commit();

    }

}

您可以為活動創建屬性并為其分配新片段的值,而不是僅使用 或 ,從而有效地在 ActivityA 和片段之間創建鏈接。new FragmentA()new FragmentB()


public class ActivityA extends AppCompatActivity {


    private FragmentA fragmentA;

    private FragmentB fragmentB;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        FragmentTransaction fragmentTransactionA = getSupportFragmentManager().beginTransaction();

        fragmentA = new FragmentA();

        fragmentTransactionA.replace(R.id.yourFragmentAContainerID, fragmentA, "fragmentA");

        fragmentTransactionA.commit();

        FragmentTransaction fragmentTransactionB = getSupportFragmentManager().beginTransaction();

        fragmentB = new FragmentB();

        fragmentTransactionB.replace(R.id.yourFragmentBContainerID, fragmentB, "fragmentB");

        fragmentTransactionB.commit();

    }

}

也就是說,現在您可以隨時在活動時從片段中獲取變量。假設您想要獲取片段 A 的“propertyX”和片段 B 的“屬性Y”,您現在可以在更改為 ActivityB 之前調用 和 您的活動,只要您已經擁有這些方法,并且它們是公共的。這只是一個示例,因為我們沒有關于您的問題的所有代碼,因此我冒昧地創建了一個示例類。fragmentA.getPropertyX()fragmentB.getPropertyY()


如果您仍然需要幫助,請進來。


查看完整回答
反對 回復 2022-09-28
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

在片段之間進行通信的推薦方法是創建一個共享的視圖模型對象。

這是來自 Developer.android.com 的鏈接:

與其他片段|通信安卓開發者

您應該尋找的是使用活動作為片段之間的橋梁。使用接口是常見的做法。


查看完整回答
反對 回復 2022-09-28
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

非常感謝,這是我的主要活動的代碼:


//FOR DESIGN


private Toolbar toolbar;


private DrawerLayout drawerLayout;

private NavigationView navigationView;


//FOR FRAGMENTS

private Fragment fragmentIntro;

private Fragment fragmentPhoto;

private Fragment fragmentProfile;

private Fragment fragmentDossier;

private Fragment fragmentDate;

private Fragment fragmentZone;

private Fragment fragmentTypeIntervention;



//FOR DATAS

private static final int FRAGMENT_INTRO = 0;

private static final int FRAGMENT_PHOTO = 1;

private static final int FRAGMENT_PROFILE = 2;

private static final int FRAGMENT_DOSSIER = 3;

private static final int FRAGMENT_DATE = 4;

private static final int FRAGMENT_ZONE = 5;

private static final int FRAGMENT_TYPE_INTERVENTION = 6;



// FOR STRINGS


private static TextView mFragmentPageNewsTitle;



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    // Configure all views

    this.configureToolBar();

    this.configureDrawerLayout();

    this.configureNavigationView();


    // Show First Fragment

    this.showFirstFragment();

}


@Override

public void onBackPressed() {

    // Handle back click to close menu

    if (this.drawerLayout.isDrawerOpen(GravityCompat.START)) {

        this.drawerLayout.closeDrawer(GravityCompat.START);

    } else {

        super.onBackPressed();

    }

}



@Override

public boolean onNavigationItemSelected(MenuItem item) {


    int id = item.getItemId();


    // Show fragment after user clicked on a menu item

    switch (id){


        case R.id.activity_main_drawer_intro :

            this.showFragment(FRAGMENT_INTRO);

            break;


        case R.id.activity_main_drawer_photo :

            this.showFragment(FRAGMENT_PHOTO);

            break;


        case R.id.activity_main_drawer_profile:

            this.showFragment(FRAGMENT_PROFILE);

            break;

        case R.id.activity_main_drawer_dossier:

            this.showFragment(FRAGMENT_DOSSIER);

            break;


        case R.id.activity_main_drawer_date:

            this.showFragment(FRAGMENT_DATE);

            break;


        case R.id.activity_main_drawer_zone:

            this.showFragment(FRAGMENT_ZONE);

            break;


        case R.id.activity_main_drawer_typeintervention:

            this.showFragment(FRAGMENT_TYPE_INTERVENTION);

            break;


        default:

            break;

    }


    this.drawerLayout.closeDrawer(GravityCompat.START);

    return true;

}






// ---------------------

// CONFIGURATION

// ---------------------


// Configure Toolbar

private void configureToolBar(){

    this.toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);

    setSupportActionBar(toolbar);

}


// Configure Drawer Layout

private void configureDrawerLayout(){

    this.drawerLayout = (DrawerLayout) findViewById(R.id.activity_main_drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

    drawerLayout.addDrawerListener(toggle);

    toggle.syncState();

}


// Configure NavigationView

private void configureNavigationView(){

    this.navigationView = (NavigationView) findViewById(R.id.activity_main_nav_view);

    navigationView.setNavigationItemSelectedListener(this);

}


// ---------------------

// FRAGMENTS

// ---------------------


// Show first fragment when activity is created

private void showFirstFragment(){

    Fragment visibleFragment = getSupportFragmentManager().findFragmentById(R.id.activity_main_frame_layout);

    if (visibleFragment == null){

        // Show Photo Fragment

        this.showFragment(FRAGMENT_INTRO);

        // Mark as selected the menu item corresponding to PhotoFragment

        this.navigationView.getMenu().getItem(0).setChecked(true);

    }

}


// Show fragment according an Identifier

private void showFragment(int fragmentIdentifier){

    switch (fragmentIdentifier){


        case FRAGMENT_INTRO :

            this.showIntroFragment();

            break;


        case FRAGMENT_PHOTO :

            this.showPhotoFragment();

            break;

        case FRAGMENT_PROFILE:

            this.showProfileFragment();

            break;

        case FRAGMENT_DOSSIER:

            this.showClientsFragment();

            break;


        case FRAGMENT_DATE:

            this.showDateFragment();

            break;


        case FRAGMENT_ZONE:

            this.showZoneFragment();

            break;


        case FRAGMENT_TYPE_INTERVENTION:

            this.showTypeInterventionFragment();

            break;


        default:

            break;

    }

}


// ---


// Create each fragment page and show it

private void showIntroFragment() {

    if (this.fragmentIntro == null) this.fragmentIntro = IntroFragment.newInstance();

    this.startTransactionFragment(this.fragmentIntro);

}


private void showPhotoFragment(){

    if (this.fragmentPhoto == null) this.fragmentPhoto = PhotoFragment.newInstance();

    this.startTransactionFragment(this.fragmentPhoto);

}


private void showClientsFragment(){

    if (this.fragmentDossier == null) this.fragmentDossier = DossierFragment.newInstance();

    this.startTransactionFragment(this.fragmentDossier);

}


private void showProfileFragment(){

    if (this.fragmentProfile == null) this.fragmentProfile = ProfileFragment.newInstance();

    this.startTransactionFragment(this.fragmentProfile);

}


private void showDateFragment(){

    if (this.fragmentDate == null) this.fragmentDate = DateFragment.newInstance();

    this.startTransactionFragment(this.fragmentDate);

}


private void showZoneFragment(){

    if (this.fragmentZone == null) this.fragmentZone = ZoneFragment.newInstance();

    this.startTransactionFragment(this.fragmentZone);

}


private void showTypeInterventionFragment(){

    if (this.fragmentTypeIntervention == null) this.fragmentTypeIntervention = TypeInterventionFragment.newInstance();

    this.startTransactionFragment(this.fragmentTypeIntervention);

}




// ---


// Generic method that will replace and show a fragment inside the MainActivity Frame Layout

private void startTransactionFragment(Fragment fragment){

    if (!fragment.isVisible()){

        getSupportFragmentManager().beginTransaction()

                .replace(R.id.activity_main_frame_layout, fragment).commit();

    }

}

}


查看完整回答
反對 回復 2022-09-28
  • 3 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號