如何创建一个接口以从Fragment获取信息到Android活动?

kra*_*r65 3 java xml android android-edittext android-fragments

在过去的几天里,我一直在拼命尝试用一个简单的片段构建一个Android应用程序(我使用了两次).我想将片段的EditText-boxes的内容传递给新活动.我只是无法弄清楚如何从片段中获取这些内容.到目前为止我所拥有的是:

我有我的edit_text_fragment.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:orientation="vertical" >
    <EditText 
        android:id="@+id/my_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="my hint" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

和相应的MyEditTextFragment.java:

public class MyEditTextFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edit_text_fragment, container, false);
        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我在这里使用这个片段两次main.xml:

<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:orientation="vertical">
    <fragment 
        android:id="@+id/detailfragment_placeholder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="com.example.fragmenttester5.MyEditTextFragment" />
    <fragment 
        android:id="@+id/detailfragment_placeholder2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="com.example.fragmenttester5.MyEditTextFragment" />
    <Button 
        android:id="@+id/submit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit all of it" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的MainActivity中,我将按钮连接到一个新活动:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button submitButton = (Button) findViewById(R.id.submit_button);
        submitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v){
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                intent.putExtra("result1", "the_result_from_the_first_editText");
                intent.putExtra("result2", "the_result_from_the_second_editText");
                startActivity(intent);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在需要在Fragment中定义某种界面,但我找不到如何.我读了几个例子和教程(比如这个),但对我来说根本就没有意义.我不明白给出的代码,我只是不明白如何根据我的用例调整它.

所以我的问题; 任何人都可以帮我从活动中获取片段的内容吗?这个例子非常受欢迎,因为我只是在这里撞墙.

Rid*_*lly 14

你是对的,这是将数据从Fragment传递到活动的标准方法.

基本上,您定义了ListenerActivity实现的接口,并且Activity将自身注册为具有Fragment的侦听器.

这是一个简单的例子:

分段

class MyFragment extends Fragment {

    interface Listener {
        public void somethingHappenedInFragment(Object... anyDataYouWantToPassToActivity);
    }

    private Listener mListener;

    public void setListener(Listener listener) {
        mListener = listener;
    }

    // ... your code ...

    // Now here you pass the data to the activity
    mListener.somethingHappenedInFragment(some, data);

    // ... more of your code
 }
Run Code Online (Sandbox Code Playgroud)

活动

public MyActivity extends Activity implements MyFragment.Listener {

    // ... your code ...

    // creating the Fragment
    MyFragment f = new MyFragment();

    // register activity as listener
    f.setListener(this);

    // ... more of your code

    // implementation of MyFragment.Listener interface
    @Override
    public void somethingHappenedInFragment(Object... anyDataYouWantToPassToActivity) {
        // here you have the data passed from the fragment.
        for (Object o : anyDataYouWantToPassToActivity {
            System.out.println(o.toString();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)