In the class you created the fragment, create a method called public void updateText(String text)
. In this method, do all calculations, etc. required to calculate the number in the fragment with the String being passed in and then set the text to the String you get after all calculations. In the FragmentActivity
, add a textChangedListener
by using editText1.addTextChangedListener(this)
and adding implements TextWatcher
to the class declaration. Add imports and all unimplemented methods and in the recently added onTextChanged
method, call the updateText(String)
method by saying fragment.updateText(String)
, where fragment is the Fragment object you created when you attached it to your FragmentActivity
.
I hope this helps! Let me know if you need something else.
Alternatively, you can change the updateText(String)
method to add or change the parameters to whatever best fits your needs.
Below is a rough example that hopefully will help.
package com.smarticle.example;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class Example extends Activity implements TextWatcher {
ExampleFragment exampleFragment;
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exampleFragment = new ExampleFragment();
editText1 = (EditText) findViewById(R.id.editText1);
editText1.addTextChangedListener(this);
}
@Override
protected void onPause() {
super.onPause();
editText1.removeTextChangedListener(this);
}
public void afterTextChanged(Editable s) {
// I don't implement this, but it is required.
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// I don't implement this, but it is required.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
exampleFragment.updateText(editText1.getText().toString().trim());
}
public class ExampleFragment extends Fragment {
public void updateText(String text) {
// Perform any necessary calculations or actions
}
}
}