Главный экран разделен на 2 части, по одной для каждого фрагмента.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<FrameLayout
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
fragment_fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fragments.Fragment1" >
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/btn1F1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/tv1F1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/et1F1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Такой же xml и для второго фрагмента, только id элементов отличаются.
MainActivity.java
package com.example.fragments;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements FragmentEventListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.container1).setBackgroundColor(Color.GRAY);
findViewById(R.id.container2).setBackgroundColor(Color.CYAN);
try {
Fragment1 f1 = new Fragment1();
Fragment2 f2 = new Fragment2();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container1, f1);
ft.replace(R.id.container2, f2);
ft.commit();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "False", Toast.LENGTH_LONG).show();
}
}
@Override
public void setFragmentTextViewText(int idTextView,
String text) {
TextView tv = (TextView) findViewById(idTextView);
tv.setText(text);
}
@Override
public void showToast(String text) {
// TODO Auto-generated method stub
}
}
FragmentEventListener.java
package com.example.fragments;
public interface FragmentEventListener {
void setFragmentTextViewText(int idTextView, String text);
void showToast(String text);
}
Fragment1.java
package com.example.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment1, null);
EditText et = (EditText) v.findViewById(R.id.et1F1);
final String str = et.getText().toString();
v.findViewById(R.id.btn1F1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentEventListener listener = (FragmentEventListener) getActivity();
listener.setFragmentTextViewText(R.id.tv1F2, str);
}
});
return v;
}
}
Fragment2.java
package com.example.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class Fragment2 extends Fragment {
public Fragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fragment2, null);
v.findViewById(R.id.btn1F2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentEventListener listener = (FragmentEventListener) getActivity();
EditText et = (EditText) v.findViewById(R.id.et1F2);
String str = et.getText().toString();
listener.setFragmentTextViewText(R.id.tv1F1, str);
}
});
return v;
}
}
Идея такая - при запуске вижу 2 фрагмента. Ввожу текст в EditText первого фрагмента, нажимаю кнопку и TextView второго фрагмента меняется на введенный. Мой код крашится на моменте
EditText et = (EditText) v.findViewById(R.id.et1F2);
String str = et.getText().toString();
Но в фрагмент 1 я вставил его в onCreateView, перед обработчиков клика, и в результате вообще ничего не происходит.
А в фрагменте 2 я вставил его в onClick, и в этом случае программа закрывается с ошибкой.