1
Hello!
I received the following exercise:
• Fragment 1 must have two text boxes where the user can enter two numbers that will be added, and a button to trigger the addition
• Fragment 2 must have a Textview in the center of the screen to display the result of the addition.
I can make the first fragment:
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private EditText campo1, campo2;
private TextView campo3;
private double n1, n2, n3;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public frag1() {
}
public static frag1 newInstance(String param1, String param2) {
frag1 fragment = new frag1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
campo1 = (EditText) getActivity().findViewById(R.id.num1);
campo2 = (EditText) getActivity().findViewById(R.id.num2);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_frag1, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void somar() {
n1 = Double.parseDouble(campo1.getText().toString());
n2 = Double.parseDouble(campo2.getText().toString());
n3 = n1 + n2;
}
Now, how do I send the value of the N3 variable to a Textview in another fragment?
Matheus, this post is an answer or a new question?
– Sergio