Get EditText value on different fragment

后端 未结 2 2020
一向
一向 2021-01-22 08:27

I am trying to get some text from editTexts on different fragments. So what I do first is define my mPager and mPagerAdapter:

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 08:56

    finally I get over an interface, that is the only way I think to get soemthing on already defined fragments.

    My code get like this:

    1st define an interface

    public interface OnEditTextChanged {
    
    public void onEditPressed1(String edit1);
    
    public void onEditPressed2(String edit1);
    
    public void onEditPressed3(String edit1);
    

    Then fragment activity:

    public class a_Dat_Inicio1 extends Fragment {
    
    EditText edit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.a_1dat_inicio1, container, false);
        init (view);
    
        return view;
    }
    
    OnEditTextChanged editListener;
    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        try{
            editListener=(OnEditTextChanged) getActivity();
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
        }
    }
    
    private void init(View view) {
        edit=(EditText) view.findViewById(R.id.editText1);
    
        //cada vez que se modifique texto llamar
        edit.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void afterTextChanged(Editable s) {
                final String edit11 = edit.getText().toString();
                editListener.onEditPressed1(edit11);
    
            }
        });
    

    And finally on our main activity, call the method:

    public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {
    
    String dat1;
    String dat2;
    String dat3;
    
    private static final int NUM_PAGES = 3;
    private PagerAdapter mPagerAdapter;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.a_1dat_viewpager);
    
        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    
    
        //HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD
    
    
    }
    
    
    @Override
    public void onEditPressed1(String edit1) {
    
        if(mPager.getCurrentItem()==0){
            dat1=edit1;
            Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
            bread.show();
        }
    }
    

    HOPE this help someone!!!

    In any way thanks!!!

提交回复
热议问题