Jak utworzyć interfejs, aby uzyskać informacje z fragmentu na działanie Androida?
W ciągu ostatnich dni desperacko próbowałem zbudować aplikację na Androida z prostym fragmentem (którego używam dwa razy). Chcę przekazać zawartość pól „Edycja tekstu” fragmentów nowej aktywności. Po prostu nie wiem, jak zdobyć te treści z fragmentów. Do tej pory mam to:
Mam mojeedit_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>
i odpowiadająceMyEditTextFragment.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;
}
}
Następnie używam tego fragmentu dwukrotnie w moimmain.xml
lubię to:
<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>
a w mojej MainActivity podłączyłem przycisk do nowej aktywności:
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);
}
});
}
}
I myśleć Teraz muszę zdefiniować jakiś rodzaj interfejsu w Fragmentie, ale nie mogę znaleźć sposobu. Przeczytałem kilka przykładów i samouczków (npten), ale w ogóle nie mają dla mnie sensu. Nie rozumiem podanego kodu i po prostu nie rozumiem, jak go dostosować do mojego przypadku użycia.
Więc moje pytanie; Czy ktoś może mi pomóc uzyskać zawartość fragmentu z działania? Przykłady byłyby bardzo mile widziane, ponieważ uderzam tu głową o ścianę ..