相关疑难解决方法(0)

如何在片段之间传递值

我很擅长使用Fragments.

我只是想构建一个使用Fragments的简单示例应用程序.我的方案是,我有两个活动,每个活动中有一个片段.第一个片段有一个edittext和一个按钮.第二个片段有一个textview.当我在edittext中输入名称并单击按钮时,第二个片段中的textview应显示在第一个片段的edittext中输入的名称.

我能够将第一个片段的值发送到它的活动,然后从该活动发送到第二个活动.现在我如何在第二个片段中使用此值.

这是Java代码:::

package com.example.fragmentexample;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Fragment_1 extends Fragment{

    OnFragmentChangedListener mCallback;

    // Container Activity must implement this interface
    public interface OnFragmentChangedListener {
        public void onButtonClicked(String name);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

102
推荐指数
8
解决办法
21万
查看次数

标签 统计

android ×1

android-fragments ×1